134

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"),
                ),
              ],
            ),
          ),
        ));
1

11 Answers 11

325

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: ...
)
Sign up to request clarification or add additional context in comments.

6 Comments

how to handle barrierDismissible.. like when click outside adding different activity
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 you can enclose the dialog builder widget with a WillPopScope. Check this answer: stackoverflow.com/questions/45916658/…
If you are using showModalBottomSheet then you need to turn the isDismissible: false
showCupertinoDialog also has this property.
|
60

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: () {
                },
              ),
            ],
          )
        )
      },
    );

4 Comments

It is impossible to use onWillPop: (){} now. The appropriate variant is onWillPop: () => Future.value(false)
Or onWillPop: () async => false,
@ValentinaKonyukhova Yes you are right. I have updated my answer. Thanks
thanks this solution to prevent all dismiss method
19

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),
          );
        });

Comments

7

barrierDismissible: false,

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

Comments

7
  1. Phone navigation bar action disabled

canPop: false

  1. disable outer touch in AlertDialog

barrierDismissible: false

use this code

void alert() {
        showDialog(
          context: context,
          barrierDismissible: false, // <-- Set this to false.
          builder: (_) => PopScope(
            canPop: false, // <-- Prevents dialog dismiss on press of back button.
            child: AlertDialog(),
          ),
        );
      }

Comments

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: () {
                },
              ),
            ],
          )
        )
      },
    );

1 Comment

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

Just add this property in your showDialog barrierDismissible: false to prevent it from closing from outside touch.

showDialog(
   barrierDismissible: false,
   ...
)

Comments

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 */);

2 Comments

Thanks. This is what I need with getX package
Bad advice. You absolutely do not need to use GetX. Also, just avoid using GetX, in general.
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
            )
          )
        )

Comments

0

Just add barrierDismissible: false, in your showDialog

Comments

0

just add barrierDismissible: false, in you showGeneralDialog

 showGeneralDialog(
      context: context,
      barrierDismissible: false,
      barrierLabel: StringHelpers.dismiss,
      barrierColor: ColorConstants.blackColor.withValues(alpha: 0.5),
      pageBuilder: (context, animation, secondaryAnimation) {
        return Align(
          alignment: Alignment.bottomCenter,
          child: Material(
            color: Colors.transparent,
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: UiSizeConfig.isTablet
                  ? MediaQuery.of(context).size.height * 0.18
                  : MediaQuery.of(context).size.height * 0.35,
              decoration: BoxDecoration(
                color:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.