Flutter shared routes go_router with go_router_builder

242 Views Asked by At

I want to reuse routes for different branches in tabs.

I have a tabs shell route

@TypedStatefulShellRoute<HomeTabBarScreenRoute>(
  branches: <TypedStatefulShellBranch<StatefulShellBranchData>>[
    homeBranch,
    menuBranch,
  ],
)


const homeBranch = TypedStatefulShellBranch<HomeTabData>(
  routes: <TypedRoute<RouteData>>[
    TypedGoRoute<HomeTabRoute>(
      path: '/home',
      routes: [
      ],
    ),
  ],
);

class HomeTabData extends StatefulShellBranchData {
  const HomeTabData();
}

class HomeTabRoute extends GoRouteData {
  const HomeTabRoute();

  @override
  Widget build(final BuildContext context, final GoRouterState state) => const HomePage();
}

const menuBranch = TypedStatefulShellBranch<MenuTabData>(
  routes: <TypedRoute<RouteData>>[
    TypedGoRoute<MenuTabRoute>(path: '/menu'),
  ],
);

class MenuTabData extends StatefulShellBranchData {
  const MenuTabData();
}

class MenuTabRoute extends GoRouteData {
  const MenuTabRoute();

  @override
  Widget build(final BuildContext context, final GoRouterState state) => const MenuPage();
}

And also a web screen route.

@TypedGoRoute<WebScreenRoute>(path: '/url')
class WebScreenRoute extends GoRouteData {
  final String? url;
  final int? articleId;

  WebScreenRoute({this.url, this.articleId});

  static final GlobalKey<NavigatorState> $parentNavigatorKey = _rootNavigatorKey;

  @override
  Widget build(final BuildContext context, final GoRouterState state) {
    if (url != null) {
      return WebPage(
        params: WebScreenParamsUrl(url!),
      );
    } else if (articleId != null) {
      return WebPage(
        params: WebScreenParamsArticle(articleId!),
      );
    }

    throw UnimplementedError();
  }
}

All I want is to have an ability to use /home/url and /menu/url paths at the same time for all possible tabs. Is that event possible with the current state of go_router and go_router_builder? I haven't found a way to set same WebScreenRoute as subroutes for the branches, since builder starts generating WebScreenRoute's classes with the same name but different internal paths. I understand that /url comes from the root path, but TypedGoRoute requires paths to start with a /, and I'm stuck. I want to implement this using go_router and don't want to write a hacky solution on top of the navigator 2.0.

0

There are 0 best solutions below