[go_router] Nested stateful navigation with ShellRoute (#2650)

Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). 
An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). 

Other examples of using `StatefulShellRoute` are also available in these repositories: 
* [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router.
* [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example.

<br/>
Below is a short example of how a `StatefulShellRoute` can be setup:

```dart
StatefulShellRoute(
  /// Each separate stateful navigation tree (i.e. Navigator) is represented by
  /// a StatefulShellBranch, which defines the routes that will be placed on that
  /// Navigator. StatefulShellBranch also makes it possible to configure
  /// things like an (optional) Navigator key, the default location (i.e. the
  /// location the branch will be navigated to when loading it for the first time) etc.
  branches: <StatefulShellBranch>[
    StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[
      GoRoute(
        path: '/a',
        builder: (BuildContext context, GoRouterState state) =>
        const RootScreen(label: 'A'),
        routes: <RouteBase>[
          GoRoute(
            path: 'details',
            builder: (BuildContext context, GoRouterState state) =>
            const DetailsScreen(label: 'A'),
          ),
        ],
      ),
    ]),
    /// The default location of a branch will by default be the first of the
    /// configured routes. To configure a different route, provide the
    /// defaultLocation parameter.
    StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[
      GoRoute(
        path: '/b',
        builder: (BuildContext context, GoRouterState state) =>
        const RootScreen(label: 'B'),
        routes: <RouteBase>[
          GoRoute(
            path: 'details',
            builder: (BuildContext context, GoRouterState state) =>
            const DetailsScreen(label: 'B'),
          ),
        ],
      ),
    ]),
  ],
  /// Like ShellRoute, the builder builds the navigation shell around the
  /// sub-routes, but with StatefulShellRoute, this navigation shell is able to
  /// maintain the state of the Navigators for each branch. The navigation shell
  /// could for instance use a BottomNavigationBar or similar.
  builder: (BuildContext context, StatefulShellRouteState state, Widget child) =>
      ScaffoldWithNavBar(shellState: state, body: child),
)
```

This fixes issue flutter/flutter#99124.
It also (at least partially) addresses flutter/flutter#112267.
24 files changed
tree: 3ca3b9d878af33a73826f5b7929249ad581ea89b
  1. .ci/
  2. .github/
  3. packages/
  4. script/
  5. third_party/
  6. .ci.yaml
  7. .cirrus.yml
  8. .clang-format
  9. .gitattributes
  10. .gitignore
  11. .gitmodules
  12. .metadata
  13. analysis_options.yaml
  14. AUTHORS
  15. CODEOWNERS
  16. CONTRIBUTING.md
  17. customer_testing.bat
  18. customer_testing.sh
  19. LICENSE
  20. README.md
README.md

Flutter Packages

Build Status Release Status OpenSSF Scorecard

This repo is a companion repo to the main flutter repo. It contains the source code for Flutter's first-party packages (i.e., packages developed by the core Flutter team). Check the packages directory to see all packages.

These packages are also available on pub.

Issues

Please file any issues, bugs, or feature requests in the main flutter repo. Issues pertaining to this repository are labeled “package”.

Contributing

If you wish to contribute a new package to the Flutter ecosystem, please see the documentation for developing packages. You can store your package source code in any GitHub repository (the present repo is only intended for packages developed by the core Flutter team). Once your package is ready you can publish to the pub repository.

If you wish to contribute a change to any of the existing packages in this repo, please review our contribution guide, and send a pull request.

Packages

These are the packages hosted in this repository:

PackagePubPointsPopularityIssuesPull requests
animationspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
camerapub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
cross_filepub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
css_colorspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
cupertino_iconspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
espressopub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
extension_google_sign_in_as_googleapis_authpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
file_selectorpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
flutter_adaptive_scaffoldpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
flutter_imagepub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
flutter_lintspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
flutter_markdownpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
flutter_plugin_android_lifecyclepub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
flutter_template_imagespub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
go_routerpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
go_router_builderpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
google_maps_flutterpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
google_sign_inpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
image_pickerpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
in_app_purchasepub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
ios_platform_imagespub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
local_authpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
metrics_centerpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
multicast_dnspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
palette_generatorpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
path_providerpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
pigeonpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
pointer_interceptorpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
plugin_platform_interfacepub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
quick_actionspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
rfwpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
shared_preferencespub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
standard_message_codecpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
url_launcherpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
video_playerpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
web_benchmarkspub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
webview_flutterpub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label
xdg_directoriespub packagepub pointspopularityGitHub issues by-labelGitHub pull requests by-label