I have a login page, and after login homepage basically. The homepage has BottomNavigationBar
, like Instagram, and It's obvious that it's not GoRoute
, instead of this it's ShellRoute
. So, ShellRoute
has a key
, but it has not path
parameter, and because of this I can not use context
. go or push methods.
I supposed that ShellRoute
has a path
but it's not.
Things to keep in mind while using context.go()
from ShellRoute
to GoRoute
parentNavigatorKey
prop in each GoRoute
context.go()
to replace page , context.push()
to push page to stack
final _parentKey = GlobalKey<NavigatorState>();
final _shellKey = GlobalKey<NavigatorState>();
|_ GoRoute
|_ parentNavigatorKey = _parentKey 👈 Specify key here
|_ ShellRoute
|_ GoRoute // Needs Bottom Navigation
|_ parentNavigatorKey = _shellKey
|_ GoRoute // Needs Bottom Navigation
|_ parentNavigatorKey = _shellKey
|_ GoRoute // Full Screen which doesn't need Bottom Navigation
|_parentNavigatorKey = _parentKey
Refer detailed code and explaination of bottom NavigationBar using ShellRoute
and GoRouter
here
You should
After doing these, you will understand why you don't need to add "path" parameter to your ShellRoute.
final GoRouter router = GoRouter(
navigatorKey: _rootNavigatorKey,
initialLocation: '/home',
routes: <RouteBase>[
/// Application shell
ShellRoute(
navigatorKey: _shellNavigatorKey,
builder: (BuildContext context, GoRouterState state, Widget child) {
return ScaffoldWithNavBar(child: child);
},
routes: <RouteBase>[
/// The first screen after login.
GoRoute(
path: '/home',
builder: (BuildContext context, GoRouterState state) {
return const HomeScreen();
},
),
/// Second screen on the navigation bar
GoRoute(
path: '/discover',
builder: (BuildContext context, GoRouterState state) {
return const DiscoverScreen();
},
///Post Detail Screen inside of Discover Screen
routes: <RouteBase>[
GoRoute(
path: 'post_detail',
parentNavigatorKey: _rootNavigatorKey,
builder: (BuildContext context, GoRouterState state) {
return const PostDetailScreen();
},
),
],
),
],
),
],
);
You can use context.go('/discover')
in your ScaffoldWithNavBar() widget, when user tap the one of the bottom navigation bar item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With