flutterradio-button

I want to add radio button in flutter


In my problem, I want to add a one radio button when I tap on Radio button, it can be selected but when I tap again on that Radio button not unselect in flutter.

 Radio(
    value: StringConstant.radioDesc,
    groupValue: selectCheckBox,
    onChanged: (value) {
         setState(() {
          if (selectCheckBox == value) {
              selectCheckBox = null;
           } else {
              selectCheckBox = value;
           }
          });
    },
  )

Solution

  • If you want to unselect radio button after selection then you need to set toggleable = true, by default its false refer toggleable, learn more about Radio Button

    toggleable : Set to true if radio button is allowed to be returned to an indeterminate state by selecting it again when selected.

    Refer below code:

    Radio(
        toggleable: true,
        value: StringConstant.radioDesc,
        groupValue: selectCheckBox,
        onChanged: (value) {
             setState(() {
              if (selectCheckBox == value) {
                  selectCheckBox = null;
               } else {
                  selectCheckBox = value;
               }
              });
        },
      )