How to check whether Alert Dialog is open in flutter

13,101

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

Share:
13,101

Related videos on Youtube

Jagraj Singh
Author by

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, 2022

Comments

  • Jagraj Singh
    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
    Jagraj Singh over 4 years
    I 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
    CopsOnRoad over 4 years
    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.
  • Jagraj Singh
    Jagraj Singh over 4 years
    I don't want any action button in my dialog.
  • CopsOnRoad
    CopsOnRoad over 4 years
    OK, can you tell me your use case then? I will be able to assist you better.
  • Jagraj Singh
    Jagraj Singh over 4 years
    so 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
    CopsOnRoad over 4 years
    OK, that makes sense, now how are you poping up the dialog?
  • Jagraj Singh
    Jagraj Singh over 4 years
    By touching the space around the alert ( Modal Barrier) it automatically gets pop as I am not setting dismissible property .
  • 最白目
    最白目 almost 4 years
    with all due respect, a bool flag? Is this all we can come up with?
  • CopsOnRoad
    CopsOnRoad almost 4 years
    @最白目 yes it's a flag to keep track of dialog visibility.
  • bl4ckr0se
    bl4ckr0se over 3 years
    the problem is that the user can dismiss the AlertDialog with back button.
  • bl4ckr0se
    bl4ckr0se over 3 years
    @CopsOnRoad tha main problem is the back button and you put the solution without that?
  • CopsOnRoad
    CopsOnRoad over 3 years
    @bl4ckr0se Simply override back button using WillPopScope and change bool value in it.
  • marti-B
    marti-B over 3 years
    Yes, and still the "then" will run after it
  • Damith Thiwanka
    Damith Thiwanka over 1 year
    This works!!!! Thankas