10

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

)

Serl
  • 240
  • 1
  • 14
mfv
  • 171
  • 1
  • 1
  • 8

6 Answers6

22

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

Yusuf Fathi
  • 260
  • 3
  • 6
  • 3
    Can you please explain what it does? – Pratik Butani Mar 06 '21 at 06:22
  • 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. – Ayoub Boumzebra Apr 05 '23 at 11:10
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

Rituraj Shakti
  • 425
  • 6
  • 4
8

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) {....});
Prashant
  • 191
  • 2
  • 6
  • 1
    It worked in my case as well, but I don't understand what is it for. Can you elaborate a little bit more? – V Nikoyan Jun 24 '21 at 16:36
5

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

shakil.k
  • 1,623
  • 5
  • 17
  • 27
4

For closing dialogs, you can also use:

Navigator.pop(context, true);

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

Rohan Taneja
  • 9,687
  • 3
  • 36
  • 48
0

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.

satler
  • 75
  • 3
  • 11