Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check active entries on parent navigator for hasActiveRouteBelow #146428

Conversation

VictorOhashi
Copy link
Contributor

@VictorOhashi VictorOhashi commented Apr 8, 2024

This should fix cases when we have nested navigators.

Fix: #144687

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: routes Navigator, Router, and related APIs. labels Apr 8, 2024
@VictorOhashi VictorOhashi force-pushed the fix/nested-nav-hasActiveRouteBelow branch from 11abed4 to 9e722c6 Compare April 8, 2024 14:38
@github-actions github-actions bot added the f: material design flutter/packages/flutter/material repository. label Apr 8, 2024
@VictorOhashi VictorOhashi marked this pull request as ready for review April 8, 2024 17:00
@VictorOhashi
Copy link
Contributor Author

@chunhtai Can you have a look?

@Piinks
Copy link
Contributor

Piinks commented May 8, 2024

(Triage) Hey @VictorOhashi it looks like there are some failing checks here, can you take a look?

@VictorOhashi VictorOhashi force-pushed the fix/nested-nav-hasActiveRouteBelow branch from 89c6b9c to 1e86a53 Compare May 12, 2024 21:45
@VictorOhashi VictorOhashi force-pushed the fix/nested-nav-hasActiveRouteBelow branch from 7d505ef to f13fd5b Compare May 12, 2024 21:55
@VictorOhashi
Copy link
Contributor Author

(Triage) Hey @VictorOhashi it looks like there are some failing checks here, can you take a look?

Fixed

@@ -3790,6 +3816,11 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
@override
void didChangeDependencies() {
super.didChangeDependencies();
final NavigatorState? parentNavigator = context.findAncestorStateOfType<NavigatorState>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't trigger rebuild when it changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to proper get the NavigatorState?

Can be something like this?

NavigatorState? get _parentNavigator => context.findAncestorStateOfType<NavigatorState>();

or there are some InheritedWidget that expose the NavigatorState?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is currently no way to listen for navigator changes as this pr is the first time that we expose property that requires navigator look up.

/// Whether there is at least one active route underneath this route.
@protected
bool get hasActiveRouteBelow {
if (_navigator == null) {
return false;
}

final bool hasActiveParentRouteBelow = _hasActiveParentRouteBelow(_navigator!._parentNavigator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in nested case, it seem weird that you tap the inner appbar but then the outer navigator gets pop. I think this behavior may have different expectation based on use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have one structure like this:

  • MaterialApp
    • Navigator (MainShellRoute)
      • MainScreen
      • Navigator (AnotherShellRoute)
      • AnotherScreen

When AnotherScreen is popped, since his parent Navigator (AnotherShellRoute) don't have any child page it should be popped as well, no?

And what would you recommend to do in cases where we have multiple Navigators?
Today my work around is force adding a BackButton to every page that can't include it auto, which for me is not desirable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a user I would expect the back button should only pop content in the area that it surrounds. If AnotherShellRoute only taken part of the screen and there isn't a route in AnotherShellRoute to be popped, there shouldn't be a back button in AnotherShellRoute.

It would be a different story if MainScreen is just an empty container and AnotherShellRoute take up the whole screen though, but in that case why would you need the MainShellRoute in the first place?

Copy link
Contributor

@chunhtai chunhtai May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also why I am hesitating on this approach. I worry people may have different expectation based on their use cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My use case AnotherShellRoute is used to add the NavigationRail, something like this:
Screenshot 2024-05-16 at 10 06 34

other possible solution would be changing on go_router package, abstracting this rule on "MaterialPage/CupertinoPage" and Navigator just for the ShellRoute.

Copy link
Contributor

@chunhtai chunhtai May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I as a user felt a bit weird about if there is a back button on page1 and press it pop the entire page includes the sidebar, but I do understand people may have special UX requirement. In this case, I think you should have a special backbutton implement in your code base to handle this use case.

This is just my opinion though, so I may be wrong. but as of now, I am not confident moving forward with this pr since we are just 2 data points. We can revisit this if we have more people asking to change or preserve this behavior.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for me

@chunhtai chunhtai self-requested a review May 15, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[go_router] Back button not appearing when using Shellroute
3 participants