Trouble with flutter radio button

后端 未结 2 391
一整个雨季
一整个雨季 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条回答
  • 2021-01-12 17:49

    Try this code

    int _groupValue = -1;
    
    @override
    Widget build(BuildContext context) {
      return Column(
        children: <Widget>[
          _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:

    0 讨论(0)
  • 2021-01-12 17:58

    Try this for horizontal radio button list:

    int _groupValue = -1;
    
    Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Text('Gender', style: TextStyle(fontSize: 18)),
                    Container(
                      child: Column(
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Expanded(
                                flex: 1,
                                child: RadioListTile(
                                  value: 0,
                                  groupValue: _groupValue,
                                  title: Text("Male"),
                                  onChanged: (newValue) =>
                                      setState(() => _groupValue = newValue),
                                  activeColor: Colors.red,
                                  selected: false,
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: RadioListTile(
                                  value: 1,
                                  groupValue: _groupValue,
                                  title: Text("Female"),
                                  onChanged: (newValue) =>
                                      setState(() => _groupValue = newValue),
                                  activeColor: Colors.red,
                                  selected: false,
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
    
    0 讨论(0)
提交回复
热议问题