我添加了 AlertDialog
, 其中一个 Checkbox
是,但如果我点击 Checkbox
,它没有钩子(Hook)。我还添加了另一个 Checkbox
低于AlertDialog
这个通过点击它会得到一个钩子(Hook)。我认为它与 setState()
有关系要做,但我不知道。有人知道解决方案吗?提前致谢
ListTile(
title: Text("Test"),
trailing: Icon(Icons.fitness_center),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Test"),
content: Column(
children: <Widget>[
Row(
children: <Widget>[
Checkbox(
value: checkBoxValueTheraband,
onChanged: (bool value) {
setState(() {
checkBoxValueTheraband = value;
exerciseChooser();
});
},
),
Text("Theraband"),
],
),),);});})
最佳答案
您在 showDialog 中使用的 setState 不是它“拥有”的,这意味着它不会重建其中的任何内容,并且实际上会更新“拥有”它的父级的状态。相反,你给它自己的StatefulBuilder
有自己的 StateSetter setState 作为参数。现在,当使用 setState 时,它将调用构建器并更改此小部件中任何内容的状态。
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(
children: <Widget>[
Row(
children: <Widget>[
Checkbox(
value: checkBoxValueTheraband,
onChanged: (bool value) {
setState(() {
checkBoxValueTheraband = value;
exerciseChooser();
});
},
),
Text("Theraband"),
]),
]);
}
)
关于flutter - 在 AlertDialog 中获取复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61548703/