我正在尝试在包含Switch的flutter中显示showCupertinoModalPopup,它可以正确显示,但不更新Switch。为什么在CupertinoActionSheet上进行更新后,Switch为何不保持新状态?
https://github.com/flutter/flutter/issues/62264
Future<int> countAsReturnVisit(CalendarEvent event) async {
bool remember = false;
var res = await showCupertinoModalPopup(
context: context,
builder: (BuildContext cnt) {
return Material(
color: Colors.transparent,
child: CupertinoActionSheet(
message: Wrap(
children: <Widget>[
Text(
'Do you want to count \'Completed\' events as part of Return Visits?'
.i18n),
Container(
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Remember this selection'.i18n),
Switch(
value: remember,
onChanged: (value) {
setState(() {
remember = value;
});
},
activeTrackColor: Colors.lightBlue[200],
activeColor: Colors.blue,
),
],
),
),
],
),
actions: <Widget>[
CupertinoActionSheetAction(
child: Text('No'.i18n),
onPressed: () {
Navigator.pop(context, 0);
},
),
],
cancelButton: CupertinoActionSheetAction(
isDefaultAction: true,
child: Text('Yes'.i18n),
onPressed: () {
Navigator.pop(context, 1);
},
),
),
);
},
) as int;
if (res != null && res == 1) {
setCalendarAlwaysCountAsRrtuVist();
}
return res;
}
答案 0 :(得分:1)
setState将仅更新当前的StatefulWidget的Widget Build函数。
您应该使用StatefulBuilder。
对于您的情况,只需将StatefulBuilder添加为Switch小部件的父级,并在要更新StatefulBuilder的子级时使用StateSetter。 只会更新在StateFulBuilder构建器功能下定义的窗口小部件树。
查看此问题的答案:63008037
有关StatefulBuilder的详细信息,请转到StateFulBuilder文档页面。