【问题标题】:Flutter checkbox not deselecting after select选择后颤振复选框未取消选择
【发布时间】:2022-02-15 20:11:25
【问题描述】:

我有一个 API,其中包含一个值数组列表,可以成功地将值传递给我的单选按钮。当我选择每个选择值解析但我无法取消选择选择单选按钮。下面是我的颤振代码:


ListView.builder(    shrinkWrap:true,
                     physics:const NeverScrollableScrollPhysics(),
                     itemCount:dataOptions == null? 0: dataOptions.length,
                     itemBuilder:(BuildContext context,int index) {
                                                            return Container(
                                                                padding:
                                                                    const EdgeInsets
                                                                            .only(
                                                                        bottom:
                                                                            0.0),
                                                                child: Card(
                                                                  elevation: 0,
                                                                    child:
                                                                        ListTile(
                                                                  title: Text(
                                                                      dataOptions[index]['option_title']),
                                                                  leading:
                                                                      Radio(
                                                                    value: dataOptions[index]["option_price"],

                                                                        //below will be the default value.
                                                                        groupValue: dataOptions[index]["option_id"],
                                                                    onChanged: (value) {
                                                                      setState(
                                                                          () {
                                                                            dataOptions[index]["option_id"] = value;
                                                                        debugPrint("radioSel:$value");
                                                                      });
                                                                    },
                                                                    activeColor: Colors.green,
                                                                  ),
                                                                  trailing:
                                                                      Text(dataOptions[index]['option_price'].toString(), style:TextStyle(fontFamily: 'Montserrat',
                                                                      color: colorPink, fontWeight: FontWeight.bold,
                                                                      fontSize: 15,
                                                                    ),
                                                                  ),
                                                                )));
                                                          })


【问题讨论】:

  • 请解释您要达到的目标。您希望能够一次选择多个单选按钮并取消选择它们吗?还是您想始终一次选择一个?

标签: flutter radio-button radio-group radiobuttonlist


【解决方案1】:

由于groupValue 存储当前选定的值,并且您将单个dataOptions[index]["option_id"] 用作groupValue,因此所有单选按钮彼此独立。

如果这是有意的,请选中复选框,因为它们旨在用于此行为。 CheckBox

如果选择了组中的另一个小部件,则要取消选择 Radio 小部件,请执行以下操作。

//Set default option if prefered
String? _groupValue;

Radio(
  value: dataOptions[index]["option_price"],
  groupValue: _groupValue,
  onChanged: (value) {
    setState(() {
      _groupValue = value;
    });
  },
),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多