Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter prevent close dialog on back pressed

Tags:

flutter

I am using the showDialog function to build a dialog box, but I need to avoid that when the user presses the back button, the dialog box does not close, this is my code:

showDialog(
      barrierDismissible: false,
      context: context,

      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Hello"),
          content: new SingleChildScrollView(
            child: Container(),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog

            new FlatButton(
              child: new Text("Close"),
              onPressed: () {
              },
            ),
          ],
        );
      },
    );

How can I get it to not close?

like image 874
Victor Ortiz Avatar asked Jan 15 '20 16:01

Victor Ortiz


People also ask

How do I stop alert dialog from closing the Flutter?

Run the application on an Android Emulator or Android device and you would get the following screen. Try clicking on the greyed out area, and the alert would not close.

How do I stop showDialog on Flutter?

If you don't want to return any result after showDialog is closed, you can use it. Navigator. pop(context);

How do I use showDialog in Flutter?

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.


3 Answers

You can also do it by setting onWillPop: () => Future.value(false) inside WillPopScope. Right now onWillPop:(){} as mentioned in Accepted answer gives warning.

Warning Message: This function has a return type of 'Future', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'.

So, use onWillPop: () => Future.value(false) or onWillPop: () async {return false;}, instead.

showDialog(
  barrierDismissible: false,
  context: context,
  builder: (BuildContext context) {
    return WillPopScope(
      child: AlertDialog(
        ................
      ),
      onWillPop: () => Future.value(false),
    );
  },
);
like image 76
NaKib Avatar answered Sep 20 '22 01:09

NaKib


You need to use a WillPopScope. It will use the function on onWillPop to determine if the dialog closes or not. In this case always false, so the user can't use the back button to close the dialog.

showDialog(
  barrierDismissible: false,
  context: context,
  builder: (BuildContext context) {
    // return object of type Dialog
    return WillPopScope(
      child: AlertDialog(
        title: new Text("Hello"),
        content: new SingleChildScrollView(
          child: Container(),
        ),
        actions: <Widget>[
          // usually buttons at the bottom of the dialog

          new FlatButton(
            child: new Text("Close"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      ),
      onWillPop: () async {
        return false;
      },
    );
  },
);
like image 38
Carlos Javier Córdova Reyes Avatar answered Sep 22 '22 01:09

Carlos Javier Córdova Reyes


You need to enclose your AlertDialon on a WillPopScope like this:

showDialog(
      barrierDismissible: false,
      context: context,

      builder: (BuildContext context) {
        // return object of type Dialog
        return WillPopScope(
            onWillPop: (){},
            child:AlertDialog(
            title: new Text("Hello"),
            content: new SingleChildScrollView(
              child: Container(),),
            actions: <Widget>[
              // usually buttons at the bottom of the dialog
              new FlatButton(
                child: new Text("Close"),
                onPressed: () {
                },
              ),
            ],
          )
        )
      },
    );

The WillPopScope provides you a onWillPop parameter, where you can pass a function of what you want when the child pop. In this case, the parameter receive an empty function so it wont pop.

like image 30
Ademir Villena Zevallos Avatar answered Sep 22 '22 01:09

Ademir Villena Zevallos