Radio List tile should be unchecked initially

104 views Asked by At

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;
             });
          }
        },
       ),
    )
2

There are 2 answers

0
blackkara On BEST ANSWER

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),
  ],
)
0
powerman23rus On

You have to specify nullable groupValue and when you will initialise it with some value your RadioListTile will redraw respectively

Gender? myGroupValue = null;
// other code
Center(
  child: Container(
        width: 400,
        height: 200,
        child: Column(
          children: [
            RadioListTile< Gender?>(
                title: Text('Man'),
                groupValue: myGroupValue,
                value: Gender.man,
                onChanged: (Gender? value) {}),
            RadioListTile<String?>(
                title: Text('Woman'),
                groupValue: myGroupValue,
                value: Gender.woman,
                onChanged: (Gender? value) {}),
          ],
        ),
      ),
    ),