mohammed nabil
mohammed nabil

Reputation: 107

How to pass data from alert dialog after selecting a radio button to main screen

I have implemented 4 radio button tiles in alert dialog and want to pass data on OK Button after selecting any of the radio button and show it to the main screen or it is possible way to store the data by shared preference and then send

class _MethodState extends State<Method> {
  int _crtIndex = 1;
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text("Methods"),
      content: ListView(
        children: <Widget>[
          RadioListTile(value: 1, groupValue: _crtIndex,
              title: Text("A"),
              activeColor: Colors.teal,
              onChanged: (val){
                setState(() {
                  _crtIndex = val;


                });
              }),
          RadioListTile(value: 2, groupValue: _crtIndex,
              title: Text("B"),
              activeColor: Colors.teal,
              onChanged: (val){
                setState(() {
                  _crtIndex = val;
                });
              }),
          RadioListTile(value: 3, groupValue: _crtIndex,
              title: Text("C"),
              activeColor: Colors.teal,
              onChanged: (val){
                setState(() {
                  _crtIndex = val;
                });
              }),
          RadioListTile(value: 4, groupValue: _crtIndex,
              title: Text("D"),
              activeColor: Colors.teal,
              onChanged: (val){
                setState(() {
                  _crtIndex = val;
                });
              }),
actions: <Widget>[
        new FlatButton(
          child: new Text('OK'),
          onPressed: () {
// pass data
            Navigator.of(context).pop();
          },
        ),

Upvotes: 1

Views: 1405

Answers (2)

Sharad Paghadal
Sharad Paghadal

Reputation: 2154

You can use this https://pub.dev/packages/shared_preferences

onPressed: () async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.setInt('radio.value', _crtIndex);
  Navigator.of(context).pop();
}

And to get it in anywhere in your app

getSelectedValue() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int radioValue = prefs.getInt('radio.value');
//  check if radioValue is null - no button is pressed yet, otherwise you will have selected radio value here.
}

Upvotes: 1

Sharad Paghadal
Sharad Paghadal

Reputation: 2154

You can pass function as a parameter while opening AlertDialog from main screen, and call this function on pressing OK in AlertDialog.

Here is an example for this.

class Method extends StatefulWidget {
    Function functionFromMain;
    Method({Key key, @required this.functionFromMain}) : super(key: key);
}

Now as per your code add below line on pressing OK.

onPressed: () {
    widget.functionFromMain(_crtIndex);
    Navigator.of(context).pop();
},

Now while opening this Method widget in your Main class,

Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => Method(functionFromMain: yourFunction,)));

Upvotes: 0

Related Questions