Repository navigation
Find an outer navigation controller by its scope - #2
Open
andrei-uni wants to merge 1 commit into
Open
andrei-uni wants to merge 1 commit into
andrei-uni wants to merge 1 commit into
Conversation
context.navigation and NavigationScope.of return only the closest controller, so a screen inside a nested NavigationView could not open a route in an outer view without passing the outer controller down by hand. - NavigationController.scope identifies a controller, compared with ==. - NavigationScope.find and maybeFind return the closest controller above a context with the given scope, the closest controller included. find throws a FlutterError when there is none, maybeFind returns null. - With listen: true the widget depends on the found controller only. - The example gets a checkout in the cart tab: a nested navigator whose last step closes the whole flow in the navigator of the tab. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a way to reach an outer navigator from a nested one.
Why
context.navigationandNavigationScope.ofreturn only the closest controller. A screen inside a nestedNavigationView(a sign in flow, a tab of a shell) cannot open or close a route in an outer view unless the outer controller is passed down by hand, through the fields of the routes or an inherited widget of the application.Added
NavigationController.scopeis an optional value that identifies the controller. It is compared with==, so an enum value works.NavigationScope.findandNavigationScope.maybeFindreturn the closest controller above the context with that scope.Example
The tab controllers of the example get
scope: tab. The cart tab gets a checkout: a route with a navigator of its own and two steps. The last step closes the whole checkout in the navigator of the tab, not only the step:Behaviour
NavigationScopeplaced without a view is found too, the same wayoffinds it.findthrows aFlutterErrorwhen nothing matches,maybeFindreturnsnull.listen: truethe widget depends on the found controller only, not on the controllers between it and the context.Design notes
findis a separate method, not ascopeparameter ofof.ofkeeps returning the closest controller, and a futurerootparameter ofof(likeNavigator.of(context, rootNavigator: true)) cannot be combined with a scope by mistake.NavigationViewandNavigationScopeneed no new parameters;NavigationView.ofandmaybeOfare unchanged, and there is no new getter onBuildContext, in line with the README: an application adds its own shortcut in its extension type.Navigator.of(context, rootNavigator: true).Checks
make analyze: no format changes and no analyzer issues in the package or the example.make test: package 230/230, example 12/12, line coverage 99.35%.pubspec.yaml:listen: true.find.flutter pub publish --dry-run: 0 warnings.🤖 Generated with Claude Code