如何 select 特定单选按钮并在 ListView.builder Flutter 中获取其值

How to select specific radio button and get its value in ListView.builder Flutter

我想用单选按钮创建一个名为 JSON 的数据列表。但是,当我 select 任何单选按钮时,它是 select 每个单选按钮而不是 select 一个单选按钮。 而且,如何在单击按钮时获取 selected 单选按钮的值

我是 Flutter 新手。是否有解决方案可以解决此类问题?

这是我的代码

Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: <Widget>[
        Container(
            height: 270,
            color: Colors.white,
            child: CustomScrollView(
              slivers: [
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (context, i) {
                      return Column(
                        children: <Widget>[
                          Container(
                              width: 450,
                              padding: EdgeInsets.all(7.0),
                              margin: EdgeInsets.only(right: 8.0, left: 8.0, top: 10.0),
                              decoration: BoxDecoration(
                                border: Border.all(color: const Color(0xffededed)),
                              ),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                mainAxisSize: MainAxisSize.max,
                                children: <Widget> [
                                  Text(modelOptions[i].optionName.toString(),
                                    style: TextStyle(
                                      fontSize: 18,
                                      color: Colors.amber,
                                    ),
                                  ),
                                ],
                              ),
                          ),
                          Container(
                              margin: EdgeInsets.only(left: 8.0, right: 8.0),
                              padding: EdgeInsets.all(7.0),
                              decoration: BoxDecoration(
                                border:
                                    Border.all(color: const Color(0xffededed)),
                              ),
                              child: ListView.builder(
                                  shrinkWrap: true,
                                  physics: NeverScrollableScrollPhysics(),
                                  itemCount: modelOptions[i].modelOptionValues.length,
                                  itemBuilder: (context, j) {
                                    return
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.start,
                                        mainAxisSize: MainAxisSize.min,
                                      children: <Widget>[
                                        Radio(
                                            groupValue: selectedRadio,
                                            value: modelOptions[i].modelOptionValues[j].optionValueID,
                                            activeColor: Colors.orangeAccent,
                                            onChanged:(value) {
                                              setState(() {
                                                value = modelOptions[i].modelOptionValues[j].optionValueID.toString();
                                                selectedRadio = value;
                                                print("check radio: ${selectedRadio}");
                                                showToastMessage('${selectedRadio} is selected');  //get the selected radio button's value.
                                              });
                                            }),
                                              });
                                            }),
                                        Text(
                                          modelOptions[i].modelOptionValues[j].valueName,
                                          style: TextStyle(
                                            color: Colors.black, fontSize: 16,
                                        ),),
                                      ],
                                    );
                                  }))
                        ],
                      );
                    },
                    childCount: modelOptions.length,
                  ),
                ),
                
              ],
            )),
      ],
    ));
  }
                                        Radio(
                                            groupValue: selectedRadio,
                                            activeColor: Colors.orangeAccent,
                                            onChanged: (val) {
                                              setState(() {
                                                selectedRadio = val;
                                                showToastMessage(
                                                    "' the value ?? ' is selected"); //get the selected radio button's value.
                                              });
                                            }),

您正在为 Radio 的值使用一个真实来源 selectedRadio。您应该更新索引的值。此外,您将值传递给 groupValue 参数而不是 value 参数。 groupValue 是您...切换组的方式。