MSARKrish
MSARKrish

Reputation: 4094

Radio Button widget not working inside AlertDialog Widget in Flutter

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

Upvotes: 16

Views: 21150

Answers (3)

Anup
Anup

Reputation: 19

custom language popup You need to create separate statefulwidget class to handle state of radio button. Refer this example

Upvotes: 0

b.john
b.john

Reputation: 803

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

Upvotes: 66

Harsha pulikollu
Harsha pulikollu

Reputation: 2436

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).

Upvotes: 14

Related Questions