jb3rndt / PersistentBottomNavBarV2

A highly customizable persistent bottom navigation bar for Flutter
https://pub.dev/packages/persistent_bottom_nav_bar_v2
BSD 3-Clause "New" or "Revised" License
47 stars 48 forks source link

navigatorObservers reporting a unknow route #142

Closed lucasfcardozo closed 3 months ago

lucasfcardozo commented 3 months ago

Version

5.2.1

What platforms are you seeing the problem on?

No response

What happened?

I've set a NavigatorConfig.initialRoute for my tabs along with navigatorObservers. All tabs have an initialRoute other than "/". However, my AnalyticsNavigatorObserver.didPush is being called to the '/' route. Where is this coming from?

Steps to reproduce

  1. Just run the code

Code to reproduce the problem

import 'package:flutter/material.dart';
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) => MaterialApp(
    title: 'Flutter Demo',
    home: const MyHomePage(),
  );
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) => PersistentTabView(
    navBarHeight: 48,
    tabs: [
      PersistentTabConfig(
        screen: const ScreenPage(title: "Home", contentMessage: "Home"),
        item: ItemConfig(
          icon: Icon(Icons.home),
          title: "Home",
        ),
        navigatorConfig: NavigatorConfig(
          initialRoute: "/tab_home/first",
          navigatorObservers: [AnalyticsNavigatorObserver()],
        ),
      ),
      PersistentTabConfig(
        screen: const ScreenPage(title: "Messages", contentMessage: "Messages"),
        item: ItemConfig(
          icon: Icon(Icons.message),
          title: "Messages",
        ),
        navigatorConfig: NavigatorConfig(
          initialRoute: "/tab_messages/first",
          navigatorObservers: [AnalyticsNavigatorObserver()],
        ),
      ),
      PersistentTabConfig(
        screen: const ScreenPage(title: "Settings", contentMessage: "Settings"),
        item: ItemConfig(
          icon: Icon(Icons.settings),
          title: "Settings",
        ),

        navigatorConfig: NavigatorConfig(
          initialRoute: "/tab_settings/first",
          navigatorObservers: [AnalyticsNavigatorObserver()],
        ),
      ),
    ],
    handleAndroidBackButtonPress: false,
    backgroundColor: Theme.of(context).colorScheme.background,
    navBarBuilder: (config) => Style2BottomNavBar(navBarConfig: config),
  );
}

class ScreenPage extends StatelessWidget {
  final String title;
  final String contentMessage;

  const ScreenPage({super.key, required this.title, required this.contentMessage});

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: Text(title),
    ),
    body: Center(
      child: Text(contentMessage),
    ),
  );
}

class AnalyticsNavigatorObserver extends NavigatorObserver {

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    print("didPush");
    print("new route: ${route.settings.name}");
    print("previous route: ${previousRoute?.settings.name}");
    print("------------------------------------------------");
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    print("didPop");
    print("new route: ${route.settings.name}");
    print("previous route: ${previousRoute?.settings.name}");
    print("------------------------------------------------");
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    print("didRemove");
    print("new route: ${route.settings.name}");
    print("previous route: ${previousRoute?.settings.name}");
    print("------------------------------------------------");
  }

  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    print("didReplace");
    print("new route: ${newRoute?.settings.name}");
    print("previous route: ${oldRoute?.settings.name}");
    print("------------------------------------------------");
  }
}

Relevant log output

I/flutter (10230): didPush
I/flutter (10230): new route: / <<<< ??????????
I/flutter (10230): previous route: null

Screenshots

No response

jb3rndt commented 3 months ago

Hi, thank you for reporting this. Actually, the initialRoute never got passed to the internal navigator for each tab. This is fixed with the just released verion 5.2.2.

P.S.: You can specify the routes on your MaterialApp if they are common between tabs because the Navigators for each tab will fall back to the root Navigator