首页 > 解决方案 > 如何从字符串数组中对单选按钮列表进行分组。单击时单选按钮指向所有项目。在所有项目中选择单选列表磁贴

问题描述

我正在使用 flutter-SQLite-Dart 开发移动应用程序。使用单选列表磁贴,我分配了一个单选按钮并传递了一个索引值。执行应用程序时,我可以单击所有单选按钮。我需要使单选按钮列表只能单击一次,当我选择了一个单选按钮时,不应选择其他单选按钮。这是要求。

return InkWell(
            onTap: press,
            child: Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                        height: 60.0,
                        child: Theme(
                          data: Theme.of(context).copyWith(
                              unselectedWidgetColor: Colors.grey,
                              disabledColor: Colors.blue),
                          child: Column(children: [
                        
                            RadioListTile(
                              title: Text("${sample[index]}",
                                  style:
                                  TextStyle(color: Colors.black)),
                              groupValue: id,
                              value: index,
                              activeColor: Colors.green,
                              onChanged: (val) {
                                setState(() {
                                  id = val;
                                  debugPrint('val answer$val');
                                });
                              },
                              toggleable: true,
                            ),
                          ]),
                        )),
                  ),
                  ],
              ),
            ),
          );

应用程序的输出

在此处输入图像描述

标签: flutterdartradio-buttonradio-groupradiobuttonlist

解决方案


试试下面的代码,你会用 groupValue 声明一个字符串。


String _groupValue;

return InkWell(
            onTap: press,
            child: Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                        height: 60.0,
                        child: Theme(
                          data: Theme.of(context).copyWith(
                              unselectedWidgetColor: Colors.grey,
                              disabledColor: Colors.blue),
                          child: Column(children: [
                        
                            RadioListTile(
                              title: Text("${sample[index]}",
                                  style:
                                  TextStyle(color: Colors.black)),
                              groupValue: _groupValue,
                              value: index,
                              activeColor: Colors.green,
                              onChanged: (val) {
                                setState(() {
                                   _groupValue = val;
                                  debugPrint('val answer$val');
                                });
                              },
                          
                            ),
                          ]),
                        )),
                  ),
                  ],
              ),
            ),
          );



推荐阅读