How to set a radio button checked by default in Flutter?
Asked Answered
B

4

17

By default Flutter shows all the radio buttons empty (unchecked).

How to set a radio button checked by default?

I'm posting this question to document my solution, that may help some one, and also start a topic about this, because I didn't find anything about it here.

Below the radio button code:

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",
      ),
    ],
  ),
Bethezel answered 19/2, 2019 at 20:6 Comment(0)
G
18

add an initial state

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) {
Grub answered 20/2, 2019 at 0:7 Comment(3)
Hi, How about if we need to retrieve _radioValue from storage? i tried it, but not working, because load SharedPreferences need to use awaitMix
with the preference, you can use a Future method and execute it from initStateGrub
I try it, but not pointing to saved value, please see #57041374Mix
H
5

I will give a simple example to understand :

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';
                      });
                    },
                  )
                ],
              ),
Hexapod answered 5/5, 2021 at 18:14 Comment(0)
O
3

This is how I have done my radio button

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"),
              ],
            ),
          ],
        ),
Ovary answered 30/12, 2021 at 19:18 Comment(0)
R
2

You only need to set the groupValue equal to value; The radio will be selected. If you want to set a default selected radio. you just need to make equal operation in initState method.

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

If you want to set the clicked radio button selected. You need to change the groupValue in the onChanged method and call setState method.

 onChanged: (value) {
                    setState(() {
                        groupValue = value;
                    });
                  }
Raneeraney answered 23/11, 2023 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.