How to work with NavigationBar in go_router? | Flutter

642

This is an outstanding feature request for go_router that I hope to resolve in the coming weeks. Stay tuned.

Share:
642
Florian Leeser
Author by

Florian Leeser

Updated on November 28, 2022

Comments

  • Florian Leeser
    Florian Leeser over 1 year

    I am currently struggling refactoring my routing code with go_router.

    I already got some simple routes like /signin & /signup, but the problem comes in when I try to make the routing work with a BottomNavigationBar that has multiple screens. I would like to have a separate route for each of them like /home, /events & /profile.

    I figured out that I somehow have to return the same widget with a different parameter to prevent the whole screen to change whenever a BottomNavigationBarItem is pressed and instead only update the part above the BottomNavigationBar which would be the screen itself.

    I came up with a pretty tricky solution:

    GoRoute(
      path: '/:path',
      builder: (BuildContext context, GoRouterState state) {
        final String path = state.params['path']!;
    
        if (path == 'signin') {
          return const SignInScreen();
        }
    
        if (path == 'signup') {
          return const SignUpScreen();
        }
    
        if (path == 'forgot-password') {
          return const ForgotPasswordScreen();
        }
    
        // Otherwise it has to be the ScreenWithBottomBar
    
        final int index = getIndexFromPath(path);
    
        if (index != -1) {
          return MainScreen(selectedIndex: index);
        }
    
        return const ErrorScreen();
      }
    )
    

    This does not look very good and it makes it impossible to add subroutes like /profile/settings/appearance or /events/:id.

    I would like to have something easy understandable like this:

    GoRoute(
      path: '/signin',
      builder: (BuildContext context, GoRouterState state) {
        return const SignInScreen();
      }
    ),
    GoRoute(
      path: '/signup',
      builder: (BuildContext context, GoRouterState state) {
        return const SignUpScreen();
      }
    ),
    GoRoute(
      path: '/home',
      builder: (BuildContext context, GoRouterState state) {
        return const ScreenWithNavBar(selectedScreen: 1);
      }
    ),
    GoRoute(
      path: '/events',
      builder: (BuildContext context, GoRouterState state) {
        return const ScreenWithNavBar(selectedScreen: 2);
      },
      routes: <GoRoute>[
        GoRoute(
          path: ':id',
          builder: (BuildContext context, GoRouterState state) {
            return const EventScreen();
          }
        )
      ]
    )
    

    Is there any way to achieve the behavior?

  • Florian Leeser
    Florian Leeser about 2 years
    I saw you closed the feature request, so how would I achieve this goal now, since I can only read about a new Navigation Stack inside of every Tab, which is not what I would like to do.
  • csells
    csells about 2 years
    The go_router package issues are listed here: github.com/flutter/flutter/…