Home > Software design >  Radio List tile should be unchecked initially
Radio List tile should be unchecked initially

Time:11-16

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 set it to be initially unchecked so that 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;
                              });
                            }
                          },
                        ),
                  )

CodePudding user response:

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) {}),
          ],
        ),
      ),
    ),

CodePudding user response:

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),
  ],
)
  • Related