auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

Popping a dialog will pop the page but not the dialog

Open yamamz opened this issue 2 years ago • 15 comments

 return await showDialog<bool>(
      context: context,
      barrierDismissible: barrierDismissible,
      useRootNavigator: useRootNavigator,
      builder: (BuildContext context) {
        return AlertDialog(
            title: Text(
              title!,
              style: TextStyle(
                  color: Theme.of(context).primaryColor,
                  fontWeight: FontWeight.bold),
            ),
            content: Wrap(
              children: [
                Text(
                  body!,
                  style: textTheme.bodyText2,
                ),
              ],
            ),
            actions: <Widget>[
              TextButton(
                child: Text(positive,
                    style: TextStyle(
                        color: Theme.of(context).primaryColor,
                        fontWeight: FontWeight.bold)),
                onPressed: () {
                  Navigator.of(context).pop(false);
                  if (onPressPostive != null) {
                    onPressPostive();
                  }
                },
              ),
            ],
          ),
        );
      },
    ); return await showDialog<bool>(
      context: context,
      barrierDismissible: barrierDismissible,
      useRootNavigator: useRootNavigator,
      builder: (BuildContext context) {
        return PointerInterceptor(
          child: AlertDialog(
            title: Text(
              title!,
              style: TextStyle(
                  color: Theme.of(context).primaryColor,
                  fontWeight: FontWeight.bold),
            ),
            content: Wrap(
              children: [
                Text(
                  body!,
                  style: textTheme.bodyText2,
                ),
              ],
            ),
            actions: <Widget>[
              TextButton(
                child: Text(positive,
                    style: TextStyle(
                        color: Theme.of(context).primaryColor,
                        fontWeight: FontWeight.bold)),
                onPressed: () {
                  Navigator.of(context).pop(false);
                  if (onPressPostive != null) {
                    onPressPostive();
                  }
                },
              ),
            ],
        
        );
      },
    );

yamamz avatar Oct 20 '21 11:10 yamamz

Facing similar issue, but in my case I am launching the dialog immediately when the page finishes building. I noticed odd behavior in some cases when looking at the _history in the Navigator, specifically when using 'context.router.replace(...)' to navigate my page. The dialog route was not always in the _history, only my page route, so the pop was popping my page and not the dialog.

I'm not exactly sure if the replace is possibly updating the nav _history after the dialog is launched, but I do know that if I put a 500ms delay before launching the dialog, everything looks ok, the _history has my page and dialog on top. Also, when I tried to nav to my page with a 'push' instead of 'replace', it looked ok as well.

Not sure if that's very helpful, but just some observations I noticed.

mpiparo avatar Oct 20 '21 22:10 mpiparo

I add some delay when launching a dialog. sometimes it works sometimes won't. I will your other suggestions thank you.

yamamz avatar Oct 21 '21 04:10 yamamz

From what I can tell it's a timing issue between navigating to the route and when the dialog is added to the nav stack. If I nav to my page with a 'replace' or a 'pushAndPopUntil' method for example, and add the dialog too quickly (in my case I'm using a Bloc listener on very basic splash page, so the page builds quick and listener fired with the dialog immediately added). What seems to be happening is the nav operation, i.e. pushAndPopUntil, has not yet completed updating its internal _history stack, while the dialog is being added, so somewhere along the way it's inadvertently popping the dialog.

Which explains in my case why a delay before launching the dialog helps, or navigating to my page with a simple push (that contains no pop operation) also works.

But I do not know if this is related to any minor timing overhead added with auto_route or if it's a deeper flutter Navigator issue?

What I can see though is that immediately before the dialog is launched, the auto_route stack looks accurate, but the Navigator _history is not accurate since I can see pages in the list that have yet to be removed.

mpiparo avatar Oct 21 '21 19:10 mpiparo

I have the same when I use a replace operation for the pages and I use showDialog in the addPostFrameCallback of the second page. I could fix it using popAndPush instead, but I think this is a bug that should be fixed.

p02diada avatar Oct 27 '21 09:10 p02diada

@p02diada @yamamz popping happens asynchronously, Try awaiting for it

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

Milad-Akarie avatar Oct 27 '21 14:10 Milad-Akarie

@Milad-Akarie the issue is the dialog is not added in the stack that id why when popping page that host page will pop not the dialog. i try awaiting but no luck. same issue occur

yamamz avatar Oct 27 '21 20:10 yamamz

Same here. no luck. By the time I get to the pop the dialog is not on the stack. I'm doing a pushAndPopUntil to get to the host page, and it appears that if that process is not complete by the time the dialog is added, the dialog is not on the stack. It's possible the dialog is pushed onto the stack and being popped mistakenly as part of the still executing pushAndPopUntil, or it's never being added at all, which would really be odd. I also tried an await on the context.router.pushAndPopUntil, but no luck.

mpiparo avatar Oct 27 '21 21:10 mpiparo

Facing same issue when use 3.0.4

suli1 avatar Oct 28 '21 10:10 suli1

Same issue here 3.0.4

Marcos1305 avatar Oct 29 '21 13:10 Marcos1305

When changed previous navigation from AutoRouter.of(context).pushAndPopUntil(); to AutoRouter.of(context).push() works fine

Marcos1305 avatar Oct 29 '21 13:10 Marcos1305

same here. when debugging and changing the pushAndPopUntil() to push() the issue doesn't occur - which may support my thinking that the 'pop' part of the pushAndPopUntil operation is popping the dialog inadvertently.

mpiparo avatar Oct 29 '21 13:10 mpiparo

Same error, but in my case, I use AutoRouter.of(context).replace() and i cant close dialog in the new page, if change to AutoRouter.of(context).push() all work fine, and I can close dialog. Also, I revert the plugin version to 2.4.2, and it all was fixed.

Upd: Checked with .pushAndPopUntil(); and get the same result, I can't close the dialog on the new page.

ShelMax avatar Nov 05 '21 20:11 ShelMax

But push requires a route parameter, isn't it?

itaishalom avatar Mar 10 '22 19:03 itaishalom

I also had the same issue. I was passing popup Context wrong. The solution in my case was that I passed the current page's context to showDialog(any kind of dialog or modalSheet).

Showing popup reference code: await showSimpleDialog( context: context, body: Container()); // context is coming from the current page, from where the popup is being called Exiting popup: onPressed: () async { context.popRoute(); }, // Must use the same context that we passed!

I hope it helps someone! Thank you!

shohibexlab avatar Jun 02 '22 02:06 shohibexlab

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Aug 14 '22 08:08 github-actions[bot]