How to check whether Alert Dialog is open in flutter
Solution 1
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.
Solution 2
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"),) );
}
Solution 3
I had the same problem. Maybe my solution would do any better for someone:
_isOpen = true;
showDialog(
context: context,
child: AlertDialog(
title: Text("Some title!"),
content: Text("Some content!"),
)).then((_) => _isOpen = false);
The then
will run when the alert closes.
Solution 4
In your case its better to use a full screen dialog then u can create frosted glass effect and in center u can add alert box like container and decorate it. To make modal barier like effect wrap outter frosted glass container with inkWell or gesture detector and on tap pop the screen
Related videos on Youtube
Jagraj Singh
Knowledge Seeker. Bachelors of Technology in Computer Science. Also a freelance Software Developer. ♥️ 3D Printing and Internet of Things ♥️
Updated on September 15, 2022Comments
-
Jagraj Singh about 1 year
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.
-
Jagraj Singh over 4 yearsI am not using any cancel button in my alert , so how do I check that alert is poped up? and could you please explain me about WillPopScope more?
-
CopsOnRoad over 4 yearsYou can do it using
showGeneralDialog
and putdismissible
property tofalse
, using this will ensure you that you are the only one who is handling pop up, like in your any action buttons in dialog. -
Jagraj Singh over 4 yearsI don't want any action button in my dialog.
-
CopsOnRoad over 4 yearsOK, can you tell me your use case then? I will be able to assist you better.
-
Jagraj Singh over 4 yearsso I want to add a frosted glass background behind my alert , so what i want to do is before pushing alert dialog i want to change my screen to frosted glass bg and then after poping up alert i want to remove that bg using setState .That's why I want to listen to the alert dialog. Hope this all makes sense to you! Thanks !
-
CopsOnRoad over 4 yearsOK, that makes sense, now how are you poping up the dialog?
-
Jagraj Singh over 4 yearsBy touching the space around the alert ( Modal Barrier) it automatically gets pop as I am not setting dismissible property .
-
最白目 almost 4 yearswith all due respect, a bool flag? Is this all we can come up with?
-
CopsOnRoad almost 4 years@最白目 yes it's a flag to keep track of dialog visibility.
-
bl4ckr0se over 3 yearsthe problem is that the user can dismiss the AlertDialog with back button.
-
bl4ckr0se over 3 years@CopsOnRoad tha main problem is the back button and you put the solution without that?
-
CopsOnRoad over 3 years@bl4ckr0se Simply override back button using
WillPopScope
and change bool value in it. -
marti-B over 3 yearsYes, and still the "then" will run after it
-
Damith Thiwanka over 1 yearThis works!!!! Thankas