Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button widget not working inside AlertDialog Widget in Flutter

Tags:

flutter

dart

I want user to select the option given in Radio Button before moving to second page in My Flutter Application. I'm showing Radio button widget inside Alertdialog it shows but radio button not changed after selecting.

Everything State Class

floatingActionButton: FloatingActionButton(
      child: Icon(Icons.create),
      onPressed: () {
        return showDialog(
            context: context,
            builder: (context) => AlertDialog(
              title: Text("Select Grade System and No of Subjects"),
              actions: <Widget>[
                    Radio(value: 0, groupValue: groupValue, onChanged: selectRadio),
                    Radio(value: 1, groupValue: groupValue, onChanged: selectRadio),
                    ],
            ));
      },
    ),

selectRadio Function

void selectRadio(int value)
{
setState(() {
  groupValue=value;
});
}
like image 773
MSARKrish Avatar asked Feb 17 '19 15:02

MSARKrish


2 Answers

I had the same issue. I solved it by using this:

showDialog<void>(
  context: context,
  builder: (BuildContext context) {
    int selectedRadio = 0;
    return AlertDialog(
      content: StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: List<Widget>.generate(4, (int index) {
              return Radio<int>(
                value: index,
                groupValue: selectedRadio,
                onChanged: (int value) {
                  setState(() => selectedRadio = value);
                },
              );
            }),
          );
        },
      ),
    );
  },
like image 160
b.john Avatar answered Sep 20 '22 15:09

b.john


As I said the above comment showDialog creates new context and that setState on the calling widget therefore won't affect the dialog You can create new stateful widget naming MyDialog.Checkout this gist such that you can get it(it uses dropdown but you can implement radio widget in same way).

like image 45
Harsha pulikollu Avatar answered Sep 18 '22 15:09

Harsha pulikollu