如何在Flutter中设置默认选中的单选按钮?

问题描述 投票:0回答:4

默认情况下,Flutter 将所有单选按钮显示为空(未选中)。

如何设置默认选中的单选按钮?

我发布这个问题是为了记录我的解决方案,这可能会对某些人有所帮助,并且还开始一个有关此问题的主题,因为我在这里没有找到任何相关内容。

单选按钮代码下方:

class _ProductTypeScreen extends State<ProductType> {

  String _radioValue; //Initial definition of radio button value
  String choice;

  void radioButtonChanges(String value) {
    setState(() {
      _radioValue = value;
      switch (value) {
        case 'one':
          choice = value;
          break;
        case 'two':
          choice = value;
          break;
        case 'three':
          choice = value;
          break;
        default:
          choice = null;
      }
      debugPrint(choice); //Debug the choice in console
    });
  }

  // Now in the BuildContext... body widget:

  @override
  Widget build(BuildContext context) {
  //First of the three radio buttons

  Row(
    children: <Widget>[
      Radio(
        value: 'one',
        groupValue: _radioValue,
        onChanged: radioButtonChanges,
      ),
      Text(
        "One selected",
      ),
    ],
  ),
flutter radio-button flutter-layout
4个回答
16
投票

添加初始状态

class _ProductTypeScreen extends State<ProductType> {

  String _radioValue; //Initial definition of radio button value
  String choice;

  // ------ [add the next block] ------ 
  @override
  void initState() {
    setState(() {
      _radioValue = "one";
    });
    super.initState();
  }
  // ------ end: [add the next block] ------  

  void radioButtonChanges(String value) {
    setState(() {
      _radioValue = value;
      switch (value) {
        case 'one':
          choice = value;
          break;
        case 'two':
          choice = value;
          break;
        case 'three':
          choice = value;
          break;
        default:
          choice = null;
      }
      debugPrint(choice); //Debug the choice in console
    });
  }

  @override
  Widget build(BuildContext context) {

4
投票

我举一个简单的例子来理解:

int _radioSelected = 1;**
String _radioVal;

     child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Male'),
                  Radio(
                    value: 1,
                    groupValue: _radioSelected,
                    activeColor: Colors.blue,
                    onChanged: (value) {
                      setState(() {
                        _radioSelected = value;
                        _radioVal = 'male';
                      });
                    },
                  ),
                  Text('Female'),
                  Radio(
                    value: 2,
                    groupValue: _radioSelected,
                    activeColor: Colors.pink,
                    onChanged: (value) {
                      setState(() {
                        _radioSelected = value;
                        _radioVal = 'female';
                      });
                    },
                  )
                ],
              ),

0
投票

这就是我完成单选按钮的方式

int? _radioSelected;
String _radioVal = "";

Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Row(
              children: [
                Radio(
                  value: 1,
                  groupValue: _radioSelected,
                  activeColor: Colors.blue,
                  onChanged: (value) {
                    setState(() {
                      _radioSelected = value as int;
                      _radioVal = 'Check In';
                      print(_radioVal);
                    });
                  },
                ),
                const Text("Check In"),
              ],
            ),
            Row(
              children: [
                Radio(
                  value: 2,
                  groupValue: _radioSelected,
                  activeColor: Colors.red,
                  onChanged: (value) {
                    setState(() {
                      _radioSelected = value as int;
                      _radioVal = 'Check Out';
                      print(_radioVal);
                    });
                  },
                ),
                const Text("Check Out"),
              ],
            ),
          ],
        ),

0
投票

只需设置groupValue等于value即可;将选择收音机。如果您想设置默认选择的收音机。只需要在initState方法中进行等价操作即可。

@override
  void initState() {
    setState(() {
      groupValue = value;
    });
    super.initState();
}

如果要将单击的单选按钮设置为选中。您需要在 onChanged 方法中更改 groupValue 并调用 setState 方法。

 onChanged: (value) {
                    setState(() {
                        groupValue = value;
                    });
                  }
© www.soinside.com 2019 - 2024. All rights reserved.