14

I have problem with showDialog, when i press nothing happens but if i use Navigator.pushNamed(context, "/screen1") it works. I can not run Navigator.pop(context), it does not return any errors.

_showDialog(BuildContext context) {
return showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: new Text("Alert Dialog title"),
        actions: <Widget>[
          new FlatButton(
            child: new Text("Back"),
            onPressed: () {
              //Navigator.pushNamed(context, "/screen1");
              Navigator.pop(context);
            },
          ),
        ],
      );
    });}

In my build() :

IconButton(
iconSize: 30.0,
onPressed: () => _showDialog(context),
icon: Icon(
  Icons.clear,
  color: Colors.white,
 ),

)

0

6 Answers 6

27

had the same issue .having useRootNavigator: false, in showDialog params solved my issue .

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain what it does?
The useRootNavigator argument is used to determine whether to push the dialog to the Navigator furthest from or nearest to the given context. By default, useRootNavigator is true and the dialog route created by this method is pushed to the root navigator. It can not be null.
13

The above answer should be accepted for this question, just elaborating the above answer

return showDialog(
context: context,
useRootNavigator: false, //this property needs to be added 
builder: (BuildContext context) {....});

1 Comment

It worked in my case as well, but I don't understand what is it for. Can you elaborate a little bit more?
9

Use pop() two times:-

Navigator.of(context).pop(); Navigator.of(context).pop();

Reason: first pop function dismiss the dialog and Second pop function close the screen

Comments

4

Try calling Navigator.of(context).pop(); instead of Navigator.pop(context);

4 Comments

What's the difference between those two ?
@DineshBalasubramanian AFAIK, both are supposed to do some work. Maybe one of them is added in latest version of the SDK.
please kindly explain how this answer solve the problem?
See source of flutter, both is same static void pop<T extends Object>(BuildContext context, [ T result ]) { Navigator.of(context).pop<T>(result); }
4

For closing dialogs, you can also use:

Navigator.pop(context, true);

Source: https://docs.flutter.io/flutter/widgets/Navigator/pop.html

1 Comment

You only need to use true or false if previous screen is expecting some result.
2

For those who have nested/multiple Navigators, you can also use the pop method as shown below (notice the named parameter rootNavigator set to true):

Navigator.of(context, rootNavigator: true).pop();

As suggested by others, I tried setting the useRootNavigator in the showDialog as false but this way the barrierColor wasn't covering the whole screen (it was only covering the screen displayed by Navigator object the dialog was in).

In summary, you can use the way showed above if you have nested navigators and want to achieve having the barrierColor covering the whole screen.

1 Comment

Dude this saved me. I just had a situation with an opened custom dropdown with an internal button that opens an alertdialog. Had trouble with the navigator dismissing the opened dropdown instead of the alertdialog. Thanks!

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.