我在 flutter 上使用 go_router,并且有一个根路由和一个 shell 路由,其中所有子路由上都包含一个底部导航栏。我想推送一个位于 shell 路由之外的页面,并且我希望将其推送到根导航器上,因为它必须位于底部导航栏上方。
我已经检查了无数的 go_router 文档,但没有选项可以做到这一点。
我已经实现了一种方法来删除所有路由并在根导航器上推送页面,但是当弹出该页面时,它会转到一个空页面。但我希望它转到 shell 路由上的页面时所在的位置。
最佳答案
我也遇到了同样的问题。尝试将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();
},
),
],
),
],
)
],
);
然后从您的代码调用:
context.push('/login')
关于flutter - 使用 go_router 时如何从子路由推送到根导航器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77084066/