skip to Main Content

I push a Dialog and put a button on it to close the dialog using Navigator.of(context).pop() But when I press the button, the dialog does not close but the parent widget closes sequentially according to the widget tree. It seems that this only happens on iOS.
video for you to visualize

In parent widget:

showDialog<bool>(
          context: context,
          builder: (ct) => MyDialog(
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });

In MyDialog:

void close() {
   Navigator.of(context).pop(false);
}
  1. I tried adding rootNavigator: true as some tutorials.
  2. I tried passing an additional BuildContext to the Dialog:
showDialog<bool>(
          context: context,
          builder: (ct) => MyDialog(contx:xt
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });

and pop :

void close() {
   Navigator.of(contx).pop(false);
}

Things seem to be getting better but the problem sometimes comes back.

2

Answers


  1. Give this approach a try and let me know if it works for your use case.

    Add

    useRootNavigator : true

    in showDialog

    showDialog<bool>(
     useRootNavigator : true,
          context: context,
          builder: (ct) => MyDialog(
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });
    
    Login or Signup to reply.
  2. You can try to pop dialog with the same BuildContext you opened it

    Eg: using contextA to showDialog, then use Navigator.of(contextA).pop() to close it

    Many BuildContext with the same name "context" may confuse usage

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search