使用 go_router 时如何从子路由推送到根导航器

Zem*_*aye 4 flutter gorouter flutter-navigation flutter-go-router

我在 flutter 上使用 go_router,并且有一个根路由和一个 shell 路由,其中​​所有子路由上都包含一个底部导航栏。我想推送一个位于 shell 路由之外的页面,并且我希望将其推送到根导航器上,因为它必须位于底部导航栏上方。

我已经检查了无数的 go_router 文档,但没有选项可以做到这一点。

我已经实现了一种方法来删除所有路由并在根导航器上推送页面,但是当弹出该页面时,它会转到一个空页面。但我希望它转到 shell 路由上的页面时所在的位置。

Lui*_*is 7

我遇到了同样的问题。尝试将parentNavigatorKeys 和navigatorKeys 添加到您的GoRouter。

final GlobalKey<NavigatorState> _rootNavigator = GlobalKey(debugLabel: 'root');
final GlobalKey<NavigatorState> _shellNavigator =
    GlobalKey(debugLabel: 'shell');

final GoRouter _router = GoRouter(
  navigatorKey: _rootNavigator,
  routes: <RouteBase>[
    GoRoute(
      parentNavigatorKey: _rootNavigator,
      path: '/login',
      builder: (context, state) => const LoginScreen(),
    ),
    ShellRoute(
      navigatorKey: _shellNavigator,
      builder: (context, state, child) => BottomNav(
        child: child,
      ),
      routes: [
        GoRoute(
          parentNavigatorKey: _shellNavigator,
          path: '/',
          builder: (BuildContext context, GoRouterState state) {
            return const HomeScreen();
          },
          routes: <RouteBase>[
            GoRoute(
              name: 'nested_view',
              path: 'nested-view',
              builder: (BuildContext context, GoRouterState state) {
                return const NestedScreen();
              },
            ),
          ],
        ),
      ],
    )
  ],
);
Run Code Online (Sandbox Code Playgroud)

然后从您的代码调用:

context.push('/login')
Run Code Online (Sandbox Code Playgroud)