admin管理员组文章数量:1025202
Some applications might require circular (recursive) routing with the full stack state preservation, for instance a user might have posts, post details page might have a reference to the author, and the author page would be the same user page which displays the posts in the first place, and it would be needed to preserve the full user -> post -> user -> post -> user
navigation stack to provide the proper back navigation journey.
With the legacy Navigator APIs it is relatively straightforward to just push the pages on top of the stack as many times as possible, but how can this be done with Navigator 2.0 declarative APIs (in the case of go_router
, for instance)?
To provide the baseline, a straightforward definition of the GoRouter
for this example without the recursive navigation support would be the following:
GoRouter(
routes: [
GoRoute(
path: '/users/:user_id',
builder: (context, state) => UserPage(id: state.pathParameters['user_id'] as String),
routes: [
GoRoute(
path: 'posts/:post_id',
builder: (context, state) => PostPage(id: state.pathParameters['post_id'] as String),
routes: [
// Cannot really recursively declare the `user -> post -> ...` stack?
],
),
],
),
],
);
Some applications might require circular (recursive) routing with the full stack state preservation, for instance a user might have posts, post details page might have a reference to the author, and the author page would be the same user page which displays the posts in the first place, and it would be needed to preserve the full user -> post -> user -> post -> user
navigation stack to provide the proper back navigation journey.
With the legacy Navigator APIs it is relatively straightforward to just push the pages on top of the stack as many times as possible, but how can this be done with Navigator 2.0 declarative APIs (in the case of go_router
, for instance)?
To provide the baseline, a straightforward definition of the GoRouter
for this example without the recursive navigation support would be the following:
GoRouter(
routes: [
GoRoute(
path: '/users/:user_id',
builder: (context, state) => UserPage(id: state.pathParameters['user_id'] as String),
routes: [
GoRoute(
path: 'posts/:post_id',
builder: (context, state) => PostPage(id: state.pathParameters['post_id'] as String),
routes: [
// Cannot really recursively declare the `user -> post -> ...` stack?
],
),
],
),
],
);
Share
Improve this question
asked Mar 11 at 12:04
nyariannyarian
4,3951 gold badge22 silver badges52 bronze badges
1 Answer
Reset to default 1In go_router, you can still have same behavior as the legacy Navigator api
GoRouter(
routes: [
GoRoute(
path: '/users/:user_id',
builder: (context, state) => UserPage(key: state.pageKey, id: state.pathParameters['user_id'] as String),
),
GoRoute(
path: 'posts/:post_id',
builder: (context, state) => PostPage(key: state.pageKey, id: state.pathParameters['post_id'] as String),
),
],
);
To push a page in the stack, use context.push(route_name); this has same function as Navigator.of(context).push. The context.go replaced the whole stack.
Some applications might require circular (recursive) routing with the full stack state preservation, for instance a user might have posts, post details page might have a reference to the author, and the author page would be the same user page which displays the posts in the first place, and it would be needed to preserve the full user -> post -> user -> post -> user
navigation stack to provide the proper back navigation journey.
With the legacy Navigator APIs it is relatively straightforward to just push the pages on top of the stack as many times as possible, but how can this be done with Navigator 2.0 declarative APIs (in the case of go_router
, for instance)?
To provide the baseline, a straightforward definition of the GoRouter
for this example without the recursive navigation support would be the following:
GoRouter(
routes: [
GoRoute(
path: '/users/:user_id',
builder: (context, state) => UserPage(id: state.pathParameters['user_id'] as String),
routes: [
GoRoute(
path: 'posts/:post_id',
builder: (context, state) => PostPage(id: state.pathParameters['post_id'] as String),
routes: [
// Cannot really recursively declare the `user -> post -> ...` stack?
],
),
],
),
],
);
Some applications might require circular (recursive) routing with the full stack state preservation, for instance a user might have posts, post details page might have a reference to the author, and the author page would be the same user page which displays the posts in the first place, and it would be needed to preserve the full user -> post -> user -> post -> user
navigation stack to provide the proper back navigation journey.
With the legacy Navigator APIs it is relatively straightforward to just push the pages on top of the stack as many times as possible, but how can this be done with Navigator 2.0 declarative APIs (in the case of go_router
, for instance)?
To provide the baseline, a straightforward definition of the GoRouter
for this example without the recursive navigation support would be the following:
GoRouter(
routes: [
GoRoute(
path: '/users/:user_id',
builder: (context, state) => UserPage(id: state.pathParameters['user_id'] as String),
routes: [
GoRoute(
path: 'posts/:post_id',
builder: (context, state) => PostPage(id: state.pathParameters['post_id'] as String),
routes: [
// Cannot really recursively declare the `user -> post -> ...` stack?
],
),
],
),
],
);
Share
Improve this question
asked Mar 11 at 12:04
nyariannyarian
4,3951 gold badge22 silver badges52 bronze badges
1 Answer
Reset to default 1In go_router, you can still have same behavior as the legacy Navigator api
GoRouter(
routes: [
GoRoute(
path: '/users/:user_id',
builder: (context, state) => UserPage(key: state.pageKey, id: state.pathParameters['user_id'] as String),
),
GoRoute(
path: 'posts/:post_id',
builder: (context, state) => PostPage(key: state.pageKey, id: state.pathParameters['post_id'] as String),
),
],
);
To push a page in the stack, use context.push(route_name); this has same function as Navigator.of(context).push. The context.go replaced the whole stack.
本文标签: Fluttercircularrecursive routes with Navigator 20 (gorouter)Stack Overflow
版权声明:本文标题:Flutter, circularrecursive routes with Navigator 2.0 (go_router) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744796619a2117225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论