Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.59k stars 405 forks source link

[Web] Navigating back does not pop sub-route as expected (with `AutoLeadingButton` arrow visible) in example #1837

Open cybex-dev opened 10 months ago

cybex-dev commented 10 months ago

Hi Milad, thanks for your awesome work.

This issue and conversation follows from Issue https://github.com/Milad-Akarie/auto_route_library/issues/1207. As a reference, please see PR https://github.com/Milad-Akarie/auto_route_library/pull/1836 as I'll be referring to this refactored web_demo to use as an example.

Having reviewed the issue listed above, I can understand and appreciate the complexity for both web and mobile routing. However, it seems that web routing may require additional work and consideration.

Summary

AutoLeadingButton via AutoRouter.of(context).popTop() or AutoRouter.of(context).pop() does not navigate to previous route with 2 or more in the stack (be they in the same or sub routers)

Reproduce Steps

Consider the your example (with my rework) in the following discussion.

Perform the following steps to highlight 2 issues:

  1. Launch web_demo (assuming fresh launch, you are navigated to /#/login)
  2. Click login button (redirects by replacing to / as per AppRouter.onNavigation guard)
    • Note: the absence of a visible AutoLeadingButton action, as expected.
    • Note 2: let's ignore the addition pop buttons for now.
  3. Click Navigate to user/2 button (redirect to /#/user/2 as expected)
    • Note: the presence of AutoLeadingButton action, as expected.
  4. Issue 1
    1. Click on leading action AutoLeadingButton back arrow, observe no change.
    2. Click leading action Pop button, observe no change.
      • Note: printRouterStack shows 2 active routes (MainWebRoute, and UserRoute) but also pop() returns true. This value originates from UserRoute.popTop() calling Navigator.maybePop() (with RoutePopDisposition.doNotPop)which returns true, but no change is observed nor route popped.
      • The leading actions Pop until root and Force pop correctly pop the current router (UserRoute) from the stack as expected, going back to MainWebRoute with no AutoLeadingButton arrow visible, as expected.
  5. (following from step 3) Click on Posts -> action, this navigates to /#/verify as per the Verify Guard for sub-router UserRoute)
    • Note: Current stack now shows (MainWebRoute, UserRoute, VerifyRoute respectively)
  6. Issue 2
    1. Click on leading action AutoLeadingButton back arrow, pops correctly and returns to UserRoute (on page /#/user/2 with RoutePopDisposition.pop)
    2. Note the presence of AutoLeadingButton, clicking back which fails to pop the current route UserRoute. In Navigator.maybePop(), several recursive calls are made:
      1. UserProfileRoute with RoutePopDisposition.bubble, return false.
      2. Next, UserRoute with RoutePopDisposition.doNotPop, returns true.
        • Note however we still have MainWebRoute in the stack, which cannot be returned to here via conventional pop() with current router or root (AutoRouter.of(context).root) router. To pop, we must either call leading actions Pop until root or Force pop. Both are unexpected in this simple navigational requirement.

Comments

I've grouped these issues together as I believe they stem from the same root cause - admittedly I haven't dived into the mechanics just yet.

Impact

This significantly affects navigation for web apps, making navigation via URL or in-app stack navigation problematic and inconsistent.

CoderJerry commented 9 months ago
 final result = await AutoRouter.of(context).pop(Ï);
   if (result == false) {
             AutoRouter.of(context).navigationHistory.back();
   }

Try this