Navigating from a Splash Screen to a StatefulShellRoute.indexedStack
route in go_router
Flutter involves defining a shell route (StatefulShellRoute
) with a builder function that provides a shell widget (HomeScreen
).
In the shell route, the StatefulNavigationShell
widget allows navigation to other branches in a stateful manner using goBranch()
.
To navigate to routes outside the shell route, you can use context.goNamed(nameOfTheRoute)
or context.go(pathOfTheRoute)
.
For example, with three routes "/a", "/b", and "/c" within the shell, you can define them as follows:
StatefulShellRoute.indexedStack(
builder: (BuildContext context, GoRouterState state,
StatefulNavigationShell navigationShell) {
return HomeScreen(navigationShell: navigationShell);
},
branches: <StatefulShellBranch>[
// Route branch for the first tab in the bottom navigation bar
StatefulShellBranch(
navigatorKey: _sectionANavigatorKey,
routes: <RouteBase>[
GoRoute(
// Screen in the first tab
path: '/a',
builder: (BuildContext context, GoRouterState state) =>
const ScreenAShellBody(),
),
],
),
// Route branch for the second tab
StatefulShellBranch(
routes: <RouteBase>[
GoRoute(
// Screen in the second tab
path: '/b',
builder: (BuildContext context, GoRouterState state) =>
const ScreenBShellBody(),
),
],
),
// Route branch for the third tab
StatefulShellBranch(
routes: <RouteBase>[
GoRoute(
// Screen in the third tab
path: '/c',
builder: (BuildContext context, GoRouterState state) =>
const ScreenCShellBody(),
),
],
),
],
)