Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get AlertDialog Callback from method in Flutter?

I have AlertDialog in static method, in that I wants to get callback when user clicks on OK button.

I tried using typedef but can't understand.

Below is My Code:

class DialogUtils{

  static void displayDialogOKCallBack(BuildContext context, String title,
      String message)
  {
    showDialog(
      context: context,
      builder: (BuildContext context) {
         return AlertDialog(
          title: new Text(title, style: normalPrimaryStyle,),
          content: new Text(message),
          actions: <Widget>[
            new FlatButton(
              child: new Text(LocaleUtils.getString(context, "ok"), style: normalPrimaryStyle,),
              onPressed: () {
                Navigator.of(context).pop();
                // HERE I WANTS TO ADD CALLBACK
              },
            ),
          ],
        );
      },
    );
  }
}
like image 972
Sagar Zala Avatar asked Aug 01 '19 10:08

Sagar Zala


People also ask

How do I get AlertDialog to Flutter?

In its on the pressed property, we have to use the showDialog widget of flutter. It takes context and a builder. In builder, we provide the AlertDialog widget with title, content(Description of a title), and actions (Yes or no buttons), and our alert dialog box is ready to use.

How do I get rid of AlertDialog in Flutter?

To safely refer to a flutter: widget's ancestor in its dispose() method, save a reference to the ancestor by calling flutter: inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

How do you check alert dialog is visible or not Flutter?

You can do it using showGeneralDialog and put dismissible property to false , using this will ensure you that you are the only one who is handling pop up, like in your any action buttons in dialog.


1 Answers

You can simply wait for the dialog to be dismissed {returns null} or closed by clicking OK which, in this case, will return true

class DialogUtils {
  static Future<bool> displayDialogOKCallBack(
      BuildContext context, String title, String message) async {
    return await showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: new Text(title, style: normalPrimaryStyle,),
          content:  Text(message),
          actions: <Widget>[
             FlatButton(
              child:  Text(LocaleUtils.getString(context, "ok"), style: normalPrimaryStyle,),
              onPressed: () {
                Navigator.of(context).pop(true);
                // true here means you clicked ok
              },
            ),
          ],
        );
      },
    );
  }
}

And then when you call displayDialogOKCallBack you should await for the result

Example:

onTap: () async {
  var result =
  await DialogUtils.displayDialogOKCallBack();

  if (result) {
   // Ok button is clicked
  }
}
like image 194
Raouf Rahiche Avatar answered Nov 15 '22 07:11

Raouf Rahiche