[英]Flutter Go_router, save history of routes
我有这个路由器代码
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return const LoginScreenWidget();
},
routes: <RouteBase>[
GoRoute(
path: 'main',
builder: (BuildContext context, GoRouterState state) {
return const MainScreenWidget();
},
routes: [
GoRoute(
path: 'my-friends',
builder: (BuildContext context, GoRouterState state) {
return const FriendsScreenWidget();
},
routes: [
GoRoute(
path: ':userId',
builder: (BuildContext context, GoRouterState state) {
return FriendProfileScreenWidget(
userId: state.params['userId'],
);
},
routes: [
GoRoute(
path: 'friends',
builder: (BuildContext context, GoRouterState state) {
return ProfileFriendsScreenWidget();
},
),
],
),
],
),
],
),
],
),
],
);
class AppRouterDelegate extends GetDelegate {
@override
Widget build(BuildContext context) {
return Navigator(
onPopPage: (route, result) => route.didPop(result),
pages: currentConfiguration != null
? [currentConfiguration!.currentPage!]
: [GetNavConfig.fromRoute('main')!.currentPage!],
);
}
}
在不同的屏幕上,我 go 越来越深。 它按照我需要的方式工作。 但是我点击后退的时候,并没有通过历史往回移动,而是go回到了开头
context.go('/main')
context.go('/main/my-friends')
context.go('/main/my-friends/${friendsList[index].id}')
context.go('/main/my-friends/${userId}/friends')
context.go('/main/my-friends/${friendsList[index].id}')
我该怎么做才能让我们在点击后退时返回到上一页,而不是返回到初始页面?
后退按钮有这个代码
Navigator.pop(context);
起初我尝试通过常规 Navigator 来完成,然后通过 Navigator 2.0。 但它没有用。 然后找到了这个go路由器,很喜欢,就是解决不了最后一个问题
使用 context.push('routeName') 而不是 context.go('routeName')。 那应该可以解决问题。
您是否尝试过使用 go_router pop 扩展而不是 Navigator.pop(context)?
import 'package:go_router/go_router.dart';
context.pop();
链接到解释推送和 go 之间区别的页面: Go 与推送
您可以使用 context.push('/router')。 这是关于 context.go('/...') 和 context.push('/...') 之间差异的链接。 https://codewithandrea.com/articles/flutter-navigation-gorouter-go-vs-push/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.