Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.59k stars 404 forks source link

Unexpected behavior with AutoRoute or RedirectRoute when handling multiple tabs of the same type #2056

Open SERDUN opened 2 months ago

SERDUN commented 2 months ago

I've encountered an issue with the inability to correctly handle the initial setting in AutoRoute or:

RedirectRoute(
  path: '',
  redirectTo: bottomMenuFlavorManager.getRedirectTo(),
),

when managing multiple tabs of the same type in the navigation tree.

In my case, I have n tabs with WebView components where the structure is identical, but only the URL changes. When more than one instance of EmbeddedScreenPageRoute exists, the redirect no longer functions as expected. This seems to be due to the use of Map<String, AutoRoute>{} in the RouteCollection.

Here’s a simplified version of my routing setup:

AutoRoute.guarded(
  page: MainShellRoute.page,
  onNavigation: onMainShellRouteGuardNavigation,
  path: 'main',
  children: [
    AutoRoute(
      page: MainScreenPageRoute.page,
      path: '',
      children: [
        RedirectRoute(
          path: '',
          redirectTo: bottomMenuFlavorManager.getRedirectTo(),
        ),
        AutoRoute(
          page: KeypadScreenPageRoute.page,
          path: bottomMenuFlavorManager.getFlavorPathIfExists(MainFlavorType.keypad),
        ),
        ...bottomMenuFlavorManager.getEmbeddedFlavors().map((flavor) {
          return AutoRoute(
            usesPathAsKey: true,
            path: flavor.path,
            page: EmbeddedScreenPageRoute.page,
          );
        }),
      ],
    ),
  ],
),

The paths would look like:

embedded/1
keypad
embedded/2
embedded/3

Additionally, I’m using AutoTabsRouter to manage the tabs, which is configured as follows:

final autoTabsRouter = AutoTabsRouter(
  routes: _getRoutes(),
  builder: (context, child) {
    return Scaffold(
      body: child,
    );
  },
);

List<PageRouteInfo> _getRoutes() {
  return bottomMenuFlavorManager.flavors.map<PageRouteInfo<dynamic>>((flavor) {
    switch (flavor.type) {
      case MainFlavorType.keypad:
        return const KeypadScreenPageRoute();
      case MainFlavorType.embedded:
        return EmbeddedScreenPageRoute(id: 'Tab ${flavor.path}');
      default:
        throw Exception('Unknown flavor type');
    }
  }).toList();
}

Could you provide guidance on an alternative approach to achieve this behavior? Or would this require a change in the plugin itself?

Thanks in advance!