-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[go_router] Add ShellRoute #2362
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
Closed
Closed
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
95e2b7e
Add ShellRoute support
johnpryan 0e62fb2
Remove comment
johnpryan a9c61c0
format
johnpryan c4d00cf
Add copyright header to test
johnpryan 5a843b7
Remove StackedRoute, add navigatorKey to configuration
johnpryan 3a1d6b7
Refactor recursive function
johnpryan f25ea0c
Use correct navigator keys, fix test
johnpryan d379f08
format
johnpryan 41ddc31
Add functionality to stack GoRoutes on other Navigators if navigatorKey
johnpryan 552ddd0
update CHANGELOG
johnpryan 854fa49
Delete extra files
johnpryan 2896cbe
Update comment
johnpryan 5aa0a95
improve RouteBase doc comment
johnpryan fdb8751
Update doc comments
johnpryan 83f68c3
Create README.md
johnpryan dfc327e
Revert StackedRouteBuilder
johnpryan 6886ccb
update changelog
johnpryan ec3678d
update changelog
johnpryan 58fef30
always_specify_types, prefer_const_constructors, etc.
johnpryan cc582c6
Format
johnpryan c9a6f24
Add defaultRoute functionality to ShellRoute
johnpryan 62b1d13
Fix issue where the Android back button did not pop ShellRoute
johnpryan 1d5ef0a
Merge branch 'main' into add-new-route-types
johnpryan 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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
| import 'package:go_router/src/route.dart'; | ||
| import 'package:go_router_examples/books/main.dart'; | ||
|
|
||
| final rootNavigatorKey = GlobalKey<NavigatorState>(); | ||
|
|
||
| // This example demonstrates how to configure a nested navigation stack using | ||
| // [ShellRoute]. | ||
| void main() => runApp(App()); | ||
|
|
||
| /// The main app. | ||
| class App extends StatelessWidget { | ||
| /// Creates an [App]. | ||
| App({Key? key}) : super(key: key); | ||
|
|
||
| /// The title of the app. | ||
| static const String title = 'GoRouter Example: Declarative Routes'; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => MaterialApp.router( | ||
| routeInformationProvider: _router.routeInformationProvider, | ||
| routeInformationParser: _router.routeInformationParser, | ||
| routerDelegate: _router.routerDelegate, | ||
| title: title, | ||
| ); | ||
|
|
||
| final GoRouter _router = GoRouter( | ||
| navigatorKey: rootNavigatorKey, | ||
| routes: <RouteBase>[ | ||
| ShellRoute( | ||
| path: '/', | ||
| builder: (context, state, child) { | ||
| return AppScaffold( | ||
| child: child, | ||
| ); | ||
| }, | ||
| routes: [ | ||
| GoRoute( | ||
| path: 'a', | ||
| pageBuilder: (context, state) { | ||
| return FadeTransitionPage( | ||
| key: state.pageKey, | ||
| child: Screen( | ||
| title: 'Screen A', | ||
| detailsPath: '/a/details', | ||
| backgroundColor: Colors.red.shade100), | ||
| ); | ||
| }, | ||
| routes: [ | ||
| GoRoute( | ||
| path: 'details', | ||
| builder: (context, state) { | ||
| return DetailsScreen(label: 'A'); | ||
| }, | ||
| ) | ||
| ], | ||
| ), | ||
| GoRoute( | ||
| path: 'b', | ||
| pageBuilder: (context, state) { | ||
| return FadeTransitionPage( | ||
| key: state.pageKey, | ||
| child: Screen( | ||
| title: 'Screen B', | ||
| detailsPath: '/b/details', | ||
| backgroundColor: Colors.green.shade100, | ||
| ), | ||
| ); | ||
| }, | ||
| routes: [ | ||
| GoRoute( | ||
| path: 'details', | ||
| // Stack this route on the root navigator, instead of the | ||
| // nearest ShellRoute ancestor. | ||
| navigatorKey: rootNavigatorKey, | ||
| builder: (context, state) { | ||
| return DetailsScreen(label: 'B'); | ||
| }, | ||
| ) | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| /// The "shell" of this app. | ||
| class AppScaffold extends StatelessWidget { | ||
| final Widget child; | ||
|
|
||
| const AppScaffold({ | ||
| required this.child, | ||
| Key? key, | ||
| }) : super(key: key); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final selectedIndex = _calculateSelectedIndex(context); | ||
| return Scaffold( | ||
| body: child, | ||
| bottomNavigationBar: BottomNavigationBar( | ||
| items: const <BottomNavigationBarItem>[ | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.home), | ||
| label: 'A Screen', | ||
| ), | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.business), | ||
| label: 'B Screen', | ||
| ), | ||
| ], | ||
| currentIndex: selectedIndex, | ||
| onTap: (idx) => _onItemTapped(idx, context), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| static int _calculateSelectedIndex(BuildContext context) { | ||
| final route = GoRouter.of(context); | ||
| final location = route.location; | ||
| if (location != null) { | ||
| if (location.startsWith('/a')) return 0; | ||
| if (location.startsWith('/b')) return 1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void _onItemTapped(int index, BuildContext context) { | ||
| switch (index) { | ||
| case 0: | ||
| GoRouter.of(context).go('/a'); | ||
| break; | ||
| case 1: | ||
| GoRouter.of(context).go('/b'); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The screen of the second page. | ||
| class Screen extends StatelessWidget { | ||
| final String title; | ||
| final String detailsPath; | ||
| final Color backgroundColor; | ||
|
|
||
| /// Creates a screen with a given title | ||
| const Screen({ | ||
| required this.title, | ||
| required this.detailsPath, | ||
| required this.backgroundColor, | ||
| Key? key, | ||
| }) : super(key: key); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| appBar: AppBar( | ||
| title: const Text('This AppBar is in the inner navigator'), | ||
| ), | ||
| backgroundColor: backgroundColor, | ||
| body: Center( | ||
| child: Column( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| children: [ | ||
| Text( | ||
| title, | ||
| style: Theme.of(context).textTheme.headline4, | ||
| ), | ||
| TextButton( | ||
| onPressed: () { | ||
| GoRouter.of(context).go(detailsPath); | ||
| }, | ||
| child: const Text('View details'), | ||
| ), | ||
| ], | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class DetailsScreen extends StatelessWidget { | ||
| final String label; | ||
|
|
||
| const DetailsScreen({ | ||
| required this.label, | ||
| Key? key, | ||
| }) : super(key: key); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| appBar: AppBar( | ||
| title: const Text('Details Screen'), | ||
| ), | ||
| body: Center( | ||
| child: Text( | ||
| 'Details for $label', | ||
| style: Theme.of(context).textTheme.headline4, | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.