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
},
),
],
);
},
);
}
}
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.
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With