5

I'm trying to achieve a page where bottom navigation and app bar are fixed. But when I pressed a button it will go to another page without recreating the app bar and bottom nav bar. For routing I'm using go_router package.

There is an answer using Navigator here. But is there any way to make it using go_router?

Example Page:

example

Example Home Page:

home page example

Tipu Sultan
  • 1,743
  • 11
  • 20
  • I assume what you want to achieve is nested navigation. This is a common scenario and can be used as show in this example [here](https://gorouter.dev/nested-navigation). – Ritesh Singh Aug 15 '22 at 20:27
  • I have read that. But in this example it works only when you are navigate in same page. eg: dashboard/family/f2. But I have to push to a new page without rebuilding the app bar and the bottom nav bar. eg: dashboard/family/f2 is the first page. and I have to push dashboard/home. here dashboard holds the appbar and the bottom nav bar. – Tipu Sultan Aug 16 '22 at 03:25

2 Answers2

1

Use ShellRouter with GoRouter to meet your requirement!


Router

final _rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorKey = GlobalKey<NavigatorState>();

final router = GoRouter(
  initialLocation: '/',
  navigatorKey: _rootNavigatorKey,
  routes: [
    ShellRoute(
      navigatorKey: _shellNavigatorKey,
      pageBuilder: (context, state, child) {
        print(state.location);
        return NoTransitionPage(
            child: ScaffoldAppAndBottomBar(child: child));
      },
      routes: [
        GoRoute(
          parentNavigatorKey: _shellNavigatorKey,
          path: '/home',
          pageBuilder: (context, state) {
            return NoTransitionPage(
              child: Scaffold(
                body: const Center(
                  child: Text("Home"),
                ),
              ),
            );
          },
        ),
        GoRoute(
          path: '/',
          parentNavigatorKey: _shellNavigatorKey,
          pageBuilder: (context, state) {
            return const NoTransitionPage(
              child: Scaffold(
                body: Center(child: Text("Initial")),
              ),
            );
          },
        ),
      ],
    ),
  ],
);

ScaffoldAppAndBottomBar

class ScaffoldAppAndBottomBar extends StatelessWidget {
  Widget child;
  ScaffoldAppAndBottomBar({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: const Text(
            "App Bar",
          ),
          backgroundColor: Colors.amber,
        ),
        body: SafeArea(child: child),
        bottomNavigationBar: Container(
          color: Colors.blue,
          height: 56,
          width: double.infinity,
          child: const Center(child: Text("Bottom Navigation Bar")),
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.red,
          onPressed: () {
            context.go('/home');
          },
          child: const Icon(Icons.home),
        ));
  }
}

Output:

Initially

enter image description here

After pressing floating button

enter image description here


Refer detailed code and explaination of bottom NavigationBar using ShellRoute and GoRouter here

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
0

It is still not solved in official release, but you can check this work in progress https://github.com/flutter/packages/pull/2650. This solves the problem perfectly, hopefully it is merged soon

Kayu
  • 1
  • 1