skip to Main Content

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

2

Answers


  1. Set toggleable to true

     Radio(
        value: StringConstant.radioDesc,
        groupValue: selectCheckBox,
        toggleable: true,
        onChanged: (value) {
             setState(() {
              if (selectCheckBox == value) {
                  selectCheckBox = null;
               } else {
                  selectCheckBox = value;
               }
              });
        },
      )
    
    Login or Signup to reply.
  2. 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;
               }
              });
        },
      )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search