如何在颤振中创建可点击的复选框

时间:2021-04-04 07:35:29

标签: flutter dart

嗨,我想要一个可点击的复选框,我的代码一行中有两个复选框,我希望它们可以点击

其次,当单击其中任何一个时,第二个应自动禁用。

这是我的代码:

Row(
                             mainAxisAlignment: MainAxisAlignment.spaceBetween,
                             children: [
                               Expanded(child:
                               CheckboxListTile(
                                 title: Text("Yes"),

                                 controlAffinity: ListTileControlAffinity.leading, value: null, onChanged: (bool value) { },
                               ),
                               ),
                               Expanded(child:
                               CheckboxListTile(
                                 title: Text("No"),value:false,onChanged:(bool value) {},
                                 controlAffinity: ListTileControlAffinity.leading,
                               ),
                               )

                             ],
                           ),

我正在努力实现这一目标,但无法做到这一点,我们将不胜感激

2 个答案:

答案 0 :(得分:1)

你应该试试这个

bool firstCheck = false;
bool secondCheck = false;
Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(child:
                CheckboxListTile(
                  title: Text("Yes"),

                  controlAffinity: ListTileControlAffinity.leading, value: firstCheck, onChanged: (bool value) {
                    setState(() {
                      firstCheck = value;
                      secondCheck = !value;
                    });
                },
                ),
                ),
                Expanded(child:
                CheckboxListTile(
                  title: Text("No"),value:secondCheck,onChanged:(bool value) {
                    setState(() {
                      firstCheck = !value;
                      secondCheck = value;
                    });
                },
                  controlAffinity: ListTileControlAffinity.leading,
                ),
                )

              ],
            ),

答案 1 :(得分:0)