Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether Alert Dialog is open in flutter

Tags:

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.

like image 682
Jagraj Singh Avatar asked Apr 11 '19 13:04

Jagraj Singh


People also ask

How do you show alert messages on Flutter?

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.

How do I close alert dialog in Flutter?

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).


2 Answers

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.

like image 106
CopsOnRoad Avatar answered Sep 17 '22 16:09

CopsOnRoad


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"),) );
  }
like image 23
sandeep gurram Avatar answered Sep 17 '22 16:09

sandeep gurram