Search code examples
flutterdartroutesflutter-go-routerflutter-routes

How to use context.go() method for ShellRoute in Flutter/GoRouter


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.


Solution

  • Things to keep in mind while using context.go() from ShellRoute to GoRoute

    1. Specify parentNavigatorKey prop in each GoRoute
    2. Use context.go() to replace page , context.push() to push page to stack

    Code Structure to follow:

    
    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