在我再次重新打开菜单之前,flutter 中的菜单操作中的复选框不会更新
Ckeckbox inside menu actions in flutter is not updated until I reopen the menu again
这里是代码的相关部分:
class QuestionsScreen extends StatefulWidget {
@override
QuestionsScreenState createState() => QuestionsScreenState();
}
class QuestionsScreenState extends State<QuestionsScreen> {
final List<Question> questions = [];
bool offline = false;
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => QuestionsBloc(sl.get<QuestionsRepository>())
..add(QuestionsInitialLoad()),
child: Scaffold(
appBar: AppBar(
title: Text('Whosebug Questions'),
centerTitle: true,
actions: <Widget>[
PopupMenuButton<int>(
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
new PopupMenuItem<int>(value: 1, child: Text('Refresh')),
new PopupMenuItem<int>(
value: 2,
child: new Row(children: <Widget>[
new Text("Offline"),
new Checkbox(
value: offline,
onChanged: (newValue) {
setState(() {
offline = newValue!;
});
}, // <-- leading Checkbox
)
])),
],
onSelected: (int value) {
setState(() {
if (value == 1) {
questions.clear();
QuestionsBloc(sl.get<QuestionsRepository>())
..add
..isFetching = true
..add(QuestionsRestart());
}
});
})
],
),
body: QuestionsBody(questions, offline),
),
);
}
}
如您所见,我在 flutter 中的脚手架应用栏的弹出菜单操作按钮内声明了一个复选框。当我点击它时,更新发生了,但是直到我再次重新打开菜单我才能看到更新。是否可以立即在弹出菜单中看到复选框的更新?谢谢
这是点击 PopupMenuButton
后在开发工具上的显示效果
PopupMenuItem
不在当前上下文中。
我们可以使用 StatefulBuilder 来处理这种情况,在这种情况下,将 CheckBox
换成 StatefulBuilder
。您也可以选择 Row
.
StatefulBuilder(
builder: (context, setStateSB) => Checkbox(
value: offline,
onChanged: (newValue) {
setStateSB(() {
offline = newValue!; // to update checkbox
});
setState(() {
offline = newValue!; //to update the main ui if needed
});
},
)),
这里是代码的相关部分:
class QuestionsScreen extends StatefulWidget {
@override
QuestionsScreenState createState() => QuestionsScreenState();
}
class QuestionsScreenState extends State<QuestionsScreen> {
final List<Question> questions = [];
bool offline = false;
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => QuestionsBloc(sl.get<QuestionsRepository>())
..add(QuestionsInitialLoad()),
child: Scaffold(
appBar: AppBar(
title: Text('Whosebug Questions'),
centerTitle: true,
actions: <Widget>[
PopupMenuButton<int>(
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
new PopupMenuItem<int>(value: 1, child: Text('Refresh')),
new PopupMenuItem<int>(
value: 2,
child: new Row(children: <Widget>[
new Text("Offline"),
new Checkbox(
value: offline,
onChanged: (newValue) {
setState(() {
offline = newValue!;
});
}, // <-- leading Checkbox
)
])),
],
onSelected: (int value) {
setState(() {
if (value == 1) {
questions.clear();
QuestionsBloc(sl.get<QuestionsRepository>())
..add
..isFetching = true
..add(QuestionsRestart());
}
});
})
],
),
body: QuestionsBody(questions, offline),
),
);
}
}
如您所见,我在 flutter 中的脚手架应用栏的弹出菜单操作按钮内声明了一个复选框。当我点击它时,更新发生了,但是直到我再次重新打开菜单我才能看到更新。是否可以立即在弹出菜单中看到复选框的更新?谢谢
这是点击 PopupMenuButton
PopupMenuItem
不在当前上下文中。
我们可以使用 StatefulBuilder 来处理这种情况,在这种情况下,将 CheckBox
换成 StatefulBuilder
。您也可以选择 Row
.
StatefulBuilder(
builder: (context, setStateSB) => Checkbox(
value: offline,
onChanged: (newValue) {
setStateSB(() {
offline = newValue!; // to update checkbox
});
setState(() {
offline = newValue!; //to update the main ui if needed
});
},
)),