我正在使用StatefulShellRoute
带有StatefulShellBranch
es 的 a,如下面的代码所示。
我想要实现以下目标:当我在 的某个路由中时,StatefulShellBranch
我想导航到该 的第一个/默认路由StatefulShellBranch
。例如,当我导航到 时path5
,我想从 内部Widget5
导航到path4
。
我的用例是,里面有一个后退按钮Widget5
,当我直接导航到(即通过深层链接)时,我想在按下后退按钮时path5
“弹出”到其中的第一个/默认路线。StatefulShellBranch
路由器
router = GoRouter(
routes: StatefulShellRoute.indexedStack(
parentNavigatorKey: parentNavigatorKey,
branches: [
StatefulShellBranch(
navigatorKey: key1,
routes: [
GoRoute(
path: path1,
pageBuilder: (context, state) {
return Widget1();
},
routes: <RouteBase>[
GoRoute(
path: path2,
pageBuilder: (context, state) {
return Widget2();
},
),
GoRoute(
path: path3,
pageBuilder: (context, state) {
return Widget3();
},
),
],
),
],
),
StatefulShellBranch(
navigatorKey: key2,
routes: [
GoRoute(
path: path4,
pageBuilder: (context, state) {
return Widget4();
},
),
GoRoute(
path: path5,
pageBuilder: (context, state) {
return Widget5();
},
),
],
),
],
),
GoRoute(
parentNavigatorKey: parentNavigatorKey,
path: path6,
pageBuilder: (context, state) {
return Widget6();
},
));
小部件
// Inside Widget5
class BackButton extends StatelessWidget {
...
onPressed() {
if (router.canPop()) {
router.pop();
} else {
// TODO: Navigate to default route of StatefulShellBranch
final defaultRoute = router
.routerDelegate
.currentConfiguration.???();
}
}
...
}