flutterradiobuttonlist

Radio List tile should be unchecked initially


I have a Radio list tile in which I am setting the gender(male, female and other). Initially, the list tile is checked on 'male'. can I put it to be initially unchecked so the user can check it by himself?

Container(
width: 400
height: 200
RadioListTile<Gender?>(
   title: Text(
     radioLabel == Gender.man
     ? 'MR.': radioLabel ==Gender.women
     ? 'Ms.': 'other',
    ),
    value: radioLabel,
    groupValue: widget.Gender.value,
    onChanged: (Gender? value) {
        if (value != null) {
           setState(() {
                widget.Gender.value = value;
             });
          }
        },
       ),
    )

Solution

  • On initial state, you can decide to choose a radio item or not choose any of them using via groupValue.

    You can try here

    var initial = -1;
    
    setChecked(item) {
      setState(() {
        initial = item;
      });
    }
    
    ListView(
      children: [
        RadioListTile(
            title: Text('spam_ad'),
            value: 0,
            groupValue: initial,
            onChanged: setChecked),
        RadioListTile(
            title: Text('sexual_harassment'),
            value: 1,
            groupValue: initial,
            onChanged: setChecked),
        RadioListTile(
            title: Text('other_harassment'),
            value: 2,
            groupValue: initial,
            onChanged: setChecked),
        RadioListTile(
            title: Text('other'),
            value: 3,
            groupValue: initial,
            onChanged: setChecked),
      ],
    )