为什么FlutterSwitch的onToggle在flutter中不起作用?
我在不同的地方使用了相同的代码,它可以工作,但在这里不起作用。我不明白情况。你能帮我解决这个问题吗? 这不起作用
void showFilters(){
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
showCupertinoModalPopup(
context: context,
builder: (BuildContext ctx) => Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: col.mostDarkGrey
),
height: height,
child: Column(
//mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(
width: MediaQuery.of(context).size.width*.8,
child: Container(
decoration: BoxDecoration(
color: col.mostLightPurpleAccent,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
shape: BoxShape.rectangle,
border: Border.all(
color: col.mostDarkPurpleAccent,
width: 3,
),
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Center(
child: DefaultTextStyle(
style: TextStyle(
color: col.mostDarkPurpleAccent,
fontSize: 18,
fontWeight: FontWeight.w800),
child: Text(
'OPERATÖRLER',
),
),
),
)
),
),
SizedBox(
width: width*.8,
height: height*.45,
child: ListView.builder(
padding: EdgeInsets.only(top: 6),
shrinkWrap: true,
primary: false,
itemCount: MaintenanceApi.maintenanceUsers.length,
itemBuilder: (context, index) {
final employee = MaintenanceApi.maintenanceUsers;
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
color: col.mostLightPurpleAccent,
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
shape: BoxShape.rectangle,
border: Border.all(
color: col.mostDarkPurpleAccent,
width: 3,
),
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 16),
child: Text(
'${employee[index].userName}',
style: TextStyle(fontSize: 16),
),
),
FlutterSwitch(
width: width >= 750 ? width * .08 : width * .2,
height: width >= 750 ? height * .04 : height * .05,
activeColor: col.mostDarkPurpleAccent,
toggleSize: width >= 750 ? width * .04 : width * .08,
value: employee[index].isActive!,
borderRadius: height * .04,
onToggle: (bool val) {
setState(() {
// debugPrint("1"+employee[index].isActive.toString());//false
employee[index].isActive = val;
// debugPrint("2"+employee[index].isActive.toString());//true
employees[index].isActive = employee[index].isActive;
// debugPrint("3"+employees[index].isActive.toString());//true
});
},
),
],
),
),
);
})
),
],
)
)
);
}
我不明白为什么 setState 不起作用。我使用 debugPrint 并调试值正在更改,但没有显示。你们能帮我吗?
I used same code in the different place, it is working but in here is not working. I do not understand the situation. Could you help me to solve this?
This is not working
void showFilters(){
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
showCupertinoModalPopup(
context: context,
builder: (BuildContext ctx) => Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: col.mostDarkGrey
),
height: height,
child: Column(
//mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(
width: MediaQuery.of(context).size.width*.8,
child: Container(
decoration: BoxDecoration(
color: col.mostLightPurpleAccent,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
shape: BoxShape.rectangle,
border: Border.all(
color: col.mostDarkPurpleAccent,
width: 3,
),
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Center(
child: DefaultTextStyle(
style: TextStyle(
color: col.mostDarkPurpleAccent,
fontSize: 18,
fontWeight: FontWeight.w800),
child: Text(
'OPERATÖRLER',
),
),
),
)
),
),
SizedBox(
width: width*.8,
height: height*.45,
child: ListView.builder(
padding: EdgeInsets.only(top: 6),
shrinkWrap: true,
primary: false,
itemCount: MaintenanceApi.maintenanceUsers.length,
itemBuilder: (context, index) {
final employee = MaintenanceApi.maintenanceUsers;
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
color: col.mostLightPurpleAccent,
borderRadius:
BorderRadius.all(Radius.circular(24.0)),
shape: BoxShape.rectangle,
border: Border.all(
color: col.mostDarkPurpleAccent,
width: 3,
),
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 16),
child: Text(
'${employee[index].userName}',
style: TextStyle(fontSize: 16),
),
),
FlutterSwitch(
width: width >= 750 ? width * .08 : width * .2,
height: width >= 750 ? height * .04 : height * .05,
activeColor: col.mostDarkPurpleAccent,
toggleSize: width >= 750 ? width * .04 : width * .08,
value: employee[index].isActive!,
borderRadius: height * .04,
onToggle: (bool val) {
setState(() {
// debugPrint("1"+employee[index].isActive.toString());//false
employee[index].isActive = val;
// debugPrint("2"+employee[index].isActive.toString());//true
employees[index].isActive = employee[index].isActive;
// debugPrint("3"+employees[index].isActive.toString());//true
});
},
),
],
),
),
);
})
),
],
)
)
);
}
I do not understand why setState is not working. I use debugPrint and debug the value is changing but it is not showing. Could you help me guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论