-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 #4251
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f8f433
First attempt at re-implementing support for branch preload
tolo 6b441e9
Added tests and docs.
tolo 39ed0f2
Merge remote-tracking branch 'upstream/main' into statefulshellroute-…
tolo ca61845
Bumped version and updated changelog.
tolo 0b00343
Merge remote-tracking branch 'upstream/main' into statefulshellroute-…
tolo d55ab83
Refactoring
tolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,10 @@ final GlobalKey<NavigatorState> _rootNavigatorKey = | |
GlobalKey<NavigatorState>(debugLabel: 'root'); | ||
final GlobalKey<NavigatorState> _tabANavigatorKey = | ||
GlobalKey<NavigatorState>(debugLabel: 'tabANav'); | ||
final GlobalKey<NavigatorState> _tabB1NavigatorKey = | ||
GlobalKey<NavigatorState>(debugLabel: 'tabB1Nav'); | ||
final GlobalKey<NavigatorState> _tabB2NavigatorKey = | ||
GlobalKey<NavigatorState>(debugLabel: 'tabB2Nav'); | ||
|
||
// This example demonstrates how to setup nested navigation using a | ||
// BottomNavigationBar, where each bar item uses its own persistent navigator, | ||
|
@@ -80,71 +84,68 @@ class NestedTabNavigationExampleApp extends StatelessWidget { | |
|
||
// The route branch for the third tab of the bottom navigation bar. | ||
StatefulShellBranch( | ||
// To enable preloading of the initial locations of branches, pass | ||
// 'true' for the parameter preload. | ||
preload: true, | ||
// StatefulShellBranch will automatically use the first descendant | ||
// GoRoute as the initial location of the branch. If another route | ||
// is desired, specify the location of it using the defaultLocation | ||
// parameter. | ||
// defaultLocation: '/c2', | ||
// defaultLocation: '/b1', | ||
routes: <RouteBase>[ | ||
StatefulShellRoute( | ||
StatefulShellRoute.indexedStack( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why turning this to index stack? this example is for custom stateful_shell_route? |
||
builder: (BuildContext context, GoRouterState state, | ||
StatefulNavigationShell navigationShell) { | ||
// Just like with the top level StatefulShellRoute, no | ||
// customization is done in the builder function. | ||
return navigationShell; | ||
return TabbedRootScreen(navigationShell: navigationShell); | ||
}, | ||
navigatorContainerBuilder: (BuildContext context, | ||
StatefulNavigationShell navigationShell, | ||
List<Widget> children) { | ||
// Returning a customized container for the branch | ||
// Navigators (i.e. the `List<Widget> children` argument). | ||
// | ||
// See TabbedRootScreen for more details on how the children | ||
// are managed (in a TabBarView). | ||
return TabbedRootScreen( | ||
navigationShell: navigationShell, children: children); | ||
}, | ||
// This bottom tab uses a nested shell, wrapping sub routes in a | ||
// top TabBar. | ||
branches: <StatefulShellBranch>[ | ||
StatefulShellBranch(routes: <GoRoute>[ | ||
GoRoute( | ||
path: '/b1', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const TabScreen( | ||
label: 'B1', detailsPath: '/b1/details'), | ||
routes: <RouteBase>[ | ||
StatefulShellBranch( | ||
navigatorKey: _tabB1NavigatorKey, | ||
routes: <GoRoute>[ | ||
GoRoute( | ||
path: 'details', | ||
path: '/b1', | ||
builder: | ||
(BuildContext context, GoRouterState state) => | ||
const DetailsScreen( | ||
label: 'B1', | ||
withScaffold: false, | ||
), | ||
const TabScreen( | ||
label: 'B1', detailsPath: '/b1/details'), | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
builder: | ||
(BuildContext context, GoRouterState state) => | ||
const DetailsScreen( | ||
label: 'B1', | ||
withScaffold: false, | ||
), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
]), | ||
StatefulShellBranch(routes: <GoRoute>[ | ||
GoRoute( | ||
path: '/b2', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const TabScreen( | ||
label: 'B2', detailsPath: '/b2/details'), | ||
routes: <RouteBase>[ | ||
]), | ||
StatefulShellBranch( | ||
navigatorKey: _tabB2NavigatorKey, | ||
// To enable preloading for all nested branches, set | ||
// preload to 'true'. | ||
preload: true, | ||
routes: <GoRoute>[ | ||
GoRoute( | ||
path: 'details', | ||
path: '/b2', | ||
builder: | ||
(BuildContext context, GoRouterState state) => | ||
const DetailsScreen( | ||
label: 'B2', | ||
withScaffold: false, | ||
), | ||
const TabScreen( | ||
label: 'B2', detailsPath: '/b2/details'), | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
builder: | ||
(BuildContext context, GoRouterState state) => | ||
const DetailsScreen( | ||
label: 'B2', | ||
withScaffold: false, | ||
), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
]), | ||
]), | ||
], | ||
), | ||
], | ||
|
@@ -376,23 +377,20 @@ class DetailsScreenState extends State<DetailsScreen> { | |
/// Builds a nested shell using a [TabBar] and [TabBarView]. | ||
class TabbedRootScreen extends StatefulWidget { | ||
/// Constructs a TabbedRootScreen | ||
const TabbedRootScreen( | ||
{required this.navigationShell, required this.children, super.key}); | ||
const TabbedRootScreen({required this.navigationShell, super.key}); | ||
|
||
/// The current state of the parent StatefulShellRoute. | ||
final StatefulNavigationShell navigationShell; | ||
|
||
/// The children (branch Navigators) to display in the [TabBarView]. | ||
final List<Widget> children; | ||
|
||
@override | ||
State<StatefulWidget> createState() => _TabbedRootScreenState(); | ||
} | ||
|
||
class _TabbedRootScreenState extends State<TabbedRootScreen> | ||
with SingleTickerProviderStateMixin { | ||
late final int branchCount = widget.navigationShell.route.branches.length; | ||
late final TabController _tabController = TabController( | ||
length: widget.children.length, | ||
length: branchCount, | ||
vsync: this, | ||
initialIndex: widget.navigationShell.currentIndex); | ||
|
||
|
@@ -404,9 +402,9 @@ class _TabbedRootScreenState extends State<TabbedRootScreen> | |
|
||
@override | ||
Widget build(BuildContext context) { | ||
final List<Tab> tabs = widget.children | ||
.mapIndexed((int i, _) => Tab(text: 'Tab ${i + 1}')) | ||
.toList(); | ||
final List<Tab> tabs = | ||
List<Tab>.generate(branchCount, (int i) => Tab(text: 'Tab ${i + 1}')) | ||
.toList(); | ||
|
||
return Scaffold( | ||
appBar: AppBar( | ||
|
@@ -416,10 +414,7 @@ class _TabbedRootScreenState extends State<TabbedRootScreen> | |
tabs: tabs, | ||
onTap: (int tappedIndex) => _onTabTap(context, tappedIndex), | ||
)), | ||
body: TabBarView( | ||
controller: _tabController, | ||
children: widget.children, | ||
), | ||
body: widget.navigationShell, | ||
); | ||
} | ||
|
||
|
@@ -441,6 +436,11 @@ class TabScreen extends StatelessWidget { | |
|
||
@override | ||
Widget build(BuildContext context) { | ||
/// If preloading is enabled on the top StatefulShellRoute, this will be | ||
/// printed directly after the app has been started, but only for the route | ||
/// that is the initial location ('/b1') | ||
debugPrint('Building TabScreen - $label'); | ||
|
||
return Center( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why picking
lazy
overpreload
? It seems to me latter is more descriptive. I am also a bit concern that lazy may have some negative impression, which we should avoid using in public APIThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason I changed it to lazy was due to suggestions/feedback in #2650, but I agree that perhaps preload is a more fitting name after all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think preload is a more suited name.