Trouble with flutter radio button

后端 未结 2 410
一整个雨季
一整个雨季 2021-01-12 17:42

I just want to have normal radio buttons where only one button in the group can be selected. According to tutorials online we need to set the groupvalue variable to the valu

2条回答
  •  猫巷女王i
    2021-01-12 17:49

    Try this code

    int _groupValue = -1;
    
    @override
    Widget build(BuildContext context) {
      return Column(
        children: [
          _myRadioButton(
            title: "Checkbox 0",
            value: 0,
            onChanged: (newValue) => setState(() => _groupValue = newValue),
          ),
          _myRadioButton(
            title: "Checkbox 1",
            value: 1,
            onChanged: (newValue) => setState(() => _groupValue = newValue),
          ),
        ],
      );
    }
    
    Widget _myRadioButton({String title, int value, Function onChanged}) {
      return RadioListTile(
        value: value,
        groupValue: _groupValue,
        onChanged: onChanged,
        title: Text(title),
      );
    }
    

    Output:

提交回复
热议问题