在 AlertDialog 中获取复选框

Get a Checkbox in an AlertDialog

我添加了一个 AlertDialog,其中有一个 Checkbox,但是如果我点击 Checkbox,它不会得到一个钩子。我还在 AlertDialog 下方添加了另一个 Checkbox,单击它会得到一个钩子。我认为它与 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不是"owned",这意味着它不会重建任何东西在其中并实际更新 "owns" 它的父级状态。相反,你给它自己的 StatefulBuilder,它有自己的 StateSetter setState 作为参数。现在,当使用 setState 时,它将调用 builder 并更改此小部件中任何内容的状态。

 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"),

                              ]),

                            ]);

                   }
               )