【问题标题】:radio button onchanged not working flutter单选按钮 onchanged 无法正常工作
【发布时间】:2022-02-03 13:17:51
【问题描述】:

我有单选列表磁贴按钮动态。当我将更改值时,单选按钮不会移动选中(onchanged 不起作用)。这是我的代码。

RadioListTile(
    contentPadding: EdgeInsets.zero,
    value: '${listOption?.id}',
    groupValue: listQuestin?.code,
    activeColor: Colors.blue,
    selected: true,
    title: Align(
        alignment: Alignment(-1.1, 0),
        child: Text('${listOption?.text}', style: s2style)),
    onChanged: (val) {
      setState(() {
        print(val);
        listQuestin?.code = val.toString();

        question["questions[${listQuestin?.id}]"] = {
          "question": listQuestin?.text,
          "value": listQuestin?.code,
          "type": "radio",
          "text": listOption?.text
        };
      });
      // statusselect = val![position]['id'];
    }),

【问题讨论】:

    标签: flutter dart flutter-layout radio-button


    【解决方案1】:

    尝试按部分测试以找出更详细的错误

    https://api.flutter.dev/flutter/material/RadioListTile-class.html

    在使用RadioListTile 时尝试添加SingingCharacter 之类的键,如果在此example 中不使用键,请使用LabeledRadio

    RadioListTile<SingingCharacter>(
          contentPadding: EdgeInsets.zero,
          value: SingingCharacter.newValue,
          groupValue: _character,
          activeColor: Colors.blue,
          selected: true,
          title: const Text('New Value'),
          onChanged: (val) {
            setState(() {
              print(val);
              _character = val;
              /*listQuestin?.code = val.toString();
    
                question["questions[${listQuestin?.id}]"] = {
                  "question": listQuestin?.text,
                  "value": listQuestin?.code,
                  "type": "radio",
                  "text": listOption?.text
                };*/
            });
            // statusselect = val![position]['id'];
          },
        ),
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    您需要注意三件事,selectedvaluegroupValue

    value: data[index], // for each item value
    selected: data[index] == selectedValue, // condition to select tile style/color
    groupValue: selectedValue,// radio button active color
    

    示例代码:

     final data = List.generate(4, (index) => index.toString());
      String? selectedValue;
      void radioTileOnChanged(String? val) {
        setState(() {
          selectedValue = val;
        });
        print(val);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              ListView.builder(
                shrinkWrap: true,
                itemCount: data.length,
                itemBuilder: (context, index) {
                  return RadioListTile<String?>(
                    value: data[index], // for each item value
                    selected: data[index] ==
                        selectedValue, // condition to select tile style/color
                    groupValue: selectedValue, // radio button active color
                    title: Text(data[index]),
                    onChanged: radioTileOnChanged,
                  );
                },
              ),
            ],
          ),
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 1970-01-01
      • 2013-08-26
      • 2013-03-27
      • 2012-12-28
      • 1970-01-01
      • 2021-11-21
      相关资源
      最近更新 更多