112

In Flutter, I write a simple dialog for the loader during async task. When I touch outside dialog dismissed, How can I stop this behaviour?

Code

  showDialog(
    context: context,
    builder: (_) => new Dialog(
          child: new Container(
            alignment: FractionalOffset.center,
            height: 80.0,
            padding: const EdgeInsets.all(20.0),
            child: new Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                new CircularProgressIndicator(),
                new Padding(
                  padding: new EdgeInsets.only(left: 10.0),
                  child: new Text("Loading"),
                ),
              ],
            ),
          ),
        ));
Ranjul Arumadi
  • 109
  • 1
  • 2
  • 11
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
  • 2
    Duplicate of https://stackoverflow.com/questions/50635395/how-to-set-alertdialog-to-dont-close-clicking-outside-in-flutter/50635504#50635504 Answer already available there – Vinoth Kumar Jun 02 '18 at 00:14

8 Answers8

294

There's a property called barrierDismissible that you can pass to showDialog ; which makes dialogs dismissible or not on external click

showDialog(
  barrierDismissible: false,
  builder: ...
)
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • 3
    how to handle `barrierDismissible`.. like when click outside adding different activity –  Apr 11 '19 at 07:03
  • 12
    In Android you are still able to close the dialog by pressing the BACK button. Is there a way to prevent this as well? – Alex Semeniuk Sep 11 '19 at 12:08
  • 8
    Alex you can enclose the dialog builder widget with a WillPopScope. Check this answer: https://stackoverflow.com/questions/45916658/how-to-deactivate-or-override-the-android-back-button-in-flutter – Dpedrinha Sep 29 '19 at 18:59
  • If you are using `showModalBottomSheet` then you need to turn the `isDismissible: false` – Maruf Hassan Mar 31 '21 at 15:53
  • `showCupertinoDialog` also has this property. – Pranav Kasetti Jun 16 '21 at 15:52
54

If you want to prevent dialog close when back button pressed then refer below code. You have to wrap your AlertDialog in WillPopScope widget and make onWillPop property value with function which return Future.value(false).

showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: () => Future.value(false),
            child:AlertDialog(
            title: new Text("Alert Title"),
            content: new SingleChildScrollView(
              child: Container(),),
            actions: <Widget>[
              new FlatButton(
                child: new Text("Close"),
                onPressed: () {
                },
              ),
            ],
          )
        )
      },
    );
Sachin Tanpure
  • 1,068
  • 11
  • 12
18

just Add this Line

barrierDismissible: false,

like as

       showDialog(
        barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text(
              "Classes",
              style: TextStyle(
                  fontSize: 24, color: Colors.black, fontFamily: 'intel'),
            ),
            content: setupAlertDialoadClassList(
                context, listClasses, Icons.class__outlined, 0),
          );
        });
Sandeep Pareek
  • 1,636
  • 19
  • 21
5

barrierDismissible: false,

Use this one as I described below. showDialog( barrierDismissible: false, builder // code //

5

This will disable device navigation

showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: () async => false,
            child:AlertDialog(
            title: new Text("Alert Title"),
            content: new SingleChildScrollView(
              child: Container(),),
            actions: <Widget>[
              new FlatButton(
                child: new Text("Close"),
                onPressed: () {
                },
              ),
            ],
          )
        )
      },
    );
Pius T.K
  • 351
  • 5
  • 9
  • Exactly what I needed because while `barrierDismissible: false,` prevented closing by clicking outside the dialog, you could still close it by hitting the back button. – Ojonugwa Jude Ochalifu May 28 '23 at 19:21
2

Always use top flutter packages like get

Get.generalDialog(pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,) {
      return SimpleDialog(
        ...
      );
    }, barrierDismissible: false /* its default value */);
mostapha
  • 189
  • 1
  • 7
1

Phone navigation bar action disabled and disable outer touch in AlertDialog

showDialog(
  context: context,
  barrierDismissible: false,    // <-- Set this to false.
  builder: (_) => WillPopScope(
    onWillPop: () async => false, // <-- Prevents dialog dismiss on press of back button.
    child: AlertDialog(),
  ),
);
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

If you ar not using a showDialog, otherwise you'r using GestureDetectore, there's a easy way i just did, Just put a GestureDetector inside another one, then set the onTap action if that's your case on both GestureDetector's, with the diference that in one you are gonna put an action, an in the other one you can just leave it empty, just like this.

GestureDetector(
    onTap: () { //The Gesture you dont want to afect the rest
      Navigator.pop(context);
    },
    child: Container(
      color: Colors.transparent,
      child:GestureDetector(
            onTap: () {}, //This way is not going to afect the inside widget
            child: Container() //your widget
            )
          )
        )
Cristian Cruz
  • 479
  • 5
  • 6