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

[go_router] Add support for preloading branches of StatefulShellRoute (revised solution) #6467

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

tolo
Copy link
Contributor

@tolo tolo commented Apr 5, 2024

Adds support for preloading branches in a StatefulShellRoute. This functionality was initially part of an early implementation of #2650, however it was decided to implement this in a separate PR. The current implementation is a rewrite of the original implementation to better fit the final version of StatefulShellRoute (and go_router in general).

NOTE: this is a revised version of the initial solution (see #4251), containing a substantially simpler implementation made possible thanks to recent refactoring in go_router.

This fixes issue flutter/flutter#127804.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@chunhtai
Copy link
Contributor

I know I have been slacking off at this pr. I am sorry about this. My schedule is quite tight after I came back from vacation. This is still under my radar. I will try find time review this pr within 2 weeks

@tolo
Copy link
Contributor Author

tolo commented Apr 30, 2024

I know I have been slacking off at this pr. I am sorry about this. My schedule is quite tight after I came back from vacation. This is still under my radar. I will try find time review this pr within 2 weeks

No worries @chunhtai, I completely understand, have had far too much on my plate myself the last year 😅

@@ -60,6 +60,9 @@ class NestedTabNavigationExampleApp extends StatelessWidget {
],
),
],
// To enable preloading of the initial locations of branches, pass
// 'true' for the parameter preload.
// preload: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd find it clearer if the last line was uncommented as preload: false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You may be right, I'll consider this.

@unacorbatanegra
Copy link

Any updates on this?

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

Overall LGTM, just some comments

packages/go_router/lib/src/route.dart Show resolved Hide resolved
packages/go_router/lib/src/route.dart Outdated Show resolved Hide resolved
@@ -1255,8 +1291,39 @@ class StatefulNavigationShellState extends State<StatefulNavigationShell>
final bool locationChanged =
previousBranchLocation != currentBranchLocation;
if (locationChanged || !hasExistingNavigator) {
_branchNavigators[branch.navigatorKey] = shellRouteContext
.navigatorBuilder(branch.observers, branch.restorationScopeId);
_branchNavigators[branch.navigatorKey] =
Copy link
Contributor

Choose a reason for hiding this comment

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

just realized somewhere in this method should probably remove entries in _branchNavigators the are no longer in the StatefulShellRoute?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, that is definitely necessary to do now. Will have a look at it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chuntai, I added a fix for removing old entries, but when attempting to write a test case, I ran into issues with duplicate GlobalKeys (_shellStateKey) when updating the dynamic RoutingConfig. Or - I should be clear - at first the test case worked, but not because of the code I added, but instead because the entire StatefulShellRoute was replaced/reloaded. But when I added the possibility to set the GlobalKey<StatefulNavigationShellState> _shellStateKey to maintain state, I started to get this error.

When trying to troubleshoot it, I started to wonder whether dynamic RoutingConfig full supports ShellRoutes, at least with state. Added some test cases to test this, and it seems simple StatefulWidgets in a simple routing scenario works, but when adding a (regular) ShellRoute with a stateful shell I also get and issue with duplicate GlobalKeys.

Not sure if I just missed something, but I anyway added a gist with the test cases here: https://gist.github.com/tolo/922fd838cdea30b17121533b0012eda4

Copy link
Contributor

Choose a reason for hiding this comment

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

can you file an issue for it? looks like a bug

Copy link
Contributor

Choose a reason for hiding this comment

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

is the fix in this pr already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is now. 😊 Note though that I disabled two tests that fail due to the issue I mentioned above.

@cedvdb
Copy link
Contributor

cedvdb commented Jul 3, 2024

What's the status here ? It seems like you said that you have a test locally that's failing but it's unrelated to the changes here ?

@tolo
Copy link
Contributor Author

tolo commented Jul 10, 2024

What's the status here ? It seems like you said that you have a test locally that's failing but it's unrelated to the changes here ?

Yes, @chunhtai, have you had a chance to too look at my comment above (#6467 (comment))?

The issue is however only with testing if obsolete branch navigators are removed (which is done dynamic RoutingConfig), so possibly this could be moved to a separate issue/PR. And another issue should probably be opened for fixing the support for dynamic RoutingConfig for (stateful) shell routes. What do you think @chunhtai?

@chunhtai
Copy link
Contributor

will take a look this week

packages/go_router/lib/src/route.dart Outdated Show resolved Hide resolved
@@ -1255,8 +1291,39 @@ class StatefulNavigationShellState extends State<StatefulNavigationShell>
final bool locationChanged =
previousBranchLocation != currentBranchLocation;
if (locationChanged || !hasExistingNavigator) {
_branchNavigators[branch.navigatorKey] = shellRouteContext
.navigatorBuilder(branch.observers, branch.restorationScopeId);
_branchNavigators[branch.navigatorKey] =
Copy link
Contributor

Choose a reason for hiding this comment

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

can you file an issue for it? looks like a bug

@@ -1255,8 +1291,39 @@ class StatefulNavigationShellState extends State<StatefulNavigationShell>
final bool locationChanged =
previousBranchLocation != currentBranchLocation;
if (locationChanged || !hasExistingNavigator) {
_branchNavigators[branch.navigatorKey] = shellRouteContext
.navigatorBuilder(branch.observers, branch.restorationScopeId);
_branchNavigators[branch.navigatorKey] =
Copy link
Contributor

Choose a reason for hiding this comment

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

is the fix in this pr already?

@Shunt22
Copy link

Shunt22 commented Aug 2, 2024

Any status update?
Would love to have this feature in my project.

@tolo
Copy link
Contributor Author

tolo commented Aug 19, 2024

@chunhtai, as mentioned above, I've included the fix for cleaning obsolete branches. But I'm thinking it might be better to postpone that fix until the issues with duplicate GlobalKeys is resolved.


final Widget navigator = widget.shellRouteContext.navigatorBuilder(
branch.navigatorKey,
match!,
Copy link
Contributor

Choose a reason for hiding this comment

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

! can be removed on this line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh? Maybe I'm missing something, but that gives me an error (since match is ShellRouteMatch?). Would have been nice if the assert would have that effect though.

@chunhtai chunhtai self-requested a review August 22, 2024 21:39
@cedvdb
Copy link
Contributor

cedvdb commented Sep 16, 2024

@chunhtai can this PR get some love ? (feedback)

@tolo
Copy link
Contributor Author

tolo commented Sep 17, 2024

FYI, for those interested in StatefulShellRoute evolution, there is a related (draft) PR in the works: #7622. There are some ideas around quality-of-life improvements around StatefulShellRoute in there, so have a look if you're interested.

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM

packages/go_router/lib/src/route.dart Outdated Show resolved Hide resolved
packages/go_router/lib/src/route.dart Outdated Show resolved Hide resolved
hannah-hyj pushed a commit that referenced this pull request Sep 24, 2024
…ple (#7583)

Updated `custom_stateful_shell_route.dart` example to better support
swiping in TabView. Also added code to demonstrate use of PageView
instead of TabView. Note that to be fully effective from a usability
perspective, the PR #6467 (branch preloading) need also
be merged.

This PR addresses: 
* flutter/flutter#150837 
* flutter/flutter#112267

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] page, which explains my
responsibilities.
- [x] I read and followed the [relevant style guides] and ran the
auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages
repo does use `dart format`.)
- [x] I signed the [CLA].
- [x] The title of the PR starts with the name of the package surrounded
by square brackets, e.g. `[shared_preferences]`
- [x] I [linked to at least one issue that this PR fixes] in the
description above.
- [x] I updated `pubspec.yaml` with an appropriate new version according
to the [pub versioning philosophy], or this PR is [exempt from version
changes].
- [x] I updated `CHANGELOG.md` to add a description of the change,
[following repository CHANGELOG style], or this PR is [exempt from
CHANGELOG changes].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[relevant style guides]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[CLA]: https://cla.developers.google.com/
[Discord]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[linked to at least one issue that this PR fixes]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[pub versioning philosophy]: https://dart.dev/tools/pub/versioning
[exempt from version changes]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version
[following repository CHANGELOG style]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style
[exempt from CHANGELOG changes]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog
[test-exempt]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
Copy link
Member

@hannah-hyj hannah-hyj left a comment

Choose a reason for hiding this comment

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

LGTM!

@Shunt22
Copy link

Shunt22 commented Oct 24, 2024

Hi!
Sorry, when will this be live?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants