I am working on my flutter application and I want to check whether the alert dialog is open or not on the screen . Can anyone tell me how to do that, basically I want to do some stuff just before and after alert dialog opens and closes.
Create a Flutter project in Android Studio and replace the following code with main. dart file. To show an alert, you must have to call showDialog() function, which contains the context and itemBuilder function. The itemBuilder function returns an object of type dialog, the AlertDialog.
then((exit) { if (exit == null) return; if (exit) { // user pressed Yes button } else { // user pressed No button } }); The below code will close AlertBox/DialogBox in Flutter. Navigator. of(context).
First thing is you will be showing dialog yourself. So, you can use a bool
value to track it.
Like this.
bool _isDialogShowing = false;
void _showDialog() {
_isDialogShowing = true; // set it `true` since dialog is being displayed
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Title"),
actions: <Widget>[
FlatButton(
child: Text("CANCEL"),
onPressed: () {
_isDialogShowing = false; // set it `false` since dialog is closed
Navigator.of(context).pop();
},
)
],
);
},
);
}
To listen for back button, you can wrap your root widget in WillPopScope
and handle things in onWillPop()
accordingly.
try this !!!
Future _dialog;
_checkAndShowDialog() async {
if (_dialog == null) {
_dialog = showMyDialog();
await _dialog;
_dialog = null;
} else {
//do nothing
}
}
//dialog should return future
Future showMyDialog() {
return showDialog(
context: _context,
child: Container(child: Text("I am dialog"),) );
}
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