Best approach to Android navigation back button on Nested Navigator

Viewed 232

I have a Scaffold that holds a Navigator object. Each route has a possibility of holding an appBar.

MainScaffold

Scaffold(
      appBar: AppBarSelector(),
      ...
      body: WillPopScope(...},
          child: Navigator(
            key: _navigatorKey,
            initialRoute: '/',
            onGenerateRoute: (RouteSettings settings) {
              Widget builder;
              switch (settings.name) {
                case '/':
                  ref.read(routeNameProvider.state).state = '/';
                  builder =  HomeScreenBody(
                    automotive: content["stuff"],
                    vehicleTypes: content["more_stuff"]["types"],
                  );
                  break;
                case '/users':
                  ref.read(routeNameProvider.state).state = '/users';
                  builder = UsersScreenBody(
                    stuff: content["stuff"],
                    moreStuff: content["more_stuff"],
                  );
                  break;
                default:
                  throw Exception('Invalid route: ${settings.name}');
              }
              return PageRouteBuilder( ...
                pageBuilder: (BuildContext context, _animation, __animation) => builder,
            ...
      bottomNavigationBar: CustomBottomNavigationBar(navigatorKey: _navigatorKey),
    );

AppBarSelector

class AppBarSelector extends AppBarIcon {

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return selectAppBar(ref);
  }

  Widget selectAppBar(WidgetRef ref) {
    String routeName = ref.watch(routeNameProvider);
    switch (routeName) {
      case '/garage':
        return AppBarIcon(
          titleText: "Garage",
          icon: Icons.add,
          route: VehicleRegistrationScreen(),
        );
        break;
      case '/home/list':
        return AppBarIcon(
          titleText: ref.read(appBarTitleProvider),
          icon: Icons.add,
          route: SomeList(),
        );
        break;
      default:
        return SizedBox(height: 0);
    }
  }

Problem

Whenever I route to a page, the appBar shows/dissapears just fine. But once I press the Android Navigation BackButton there is no routing happening, thus the appBar does not change how it should be and remain after popping the route on a screen that should not have an appBar.

Question

How do I make AppBarSelector() respond to my Android Navigation BackButton?

Approach made

I have tried the persistent_bottom_nav_bar 4.0.2 package but that lakcs things like a persistent appBar or the possibility to have a transparant BottomNavigationBar.

I wonder how other people have dealt with this situation.

0 Answers
Related