简体   繁体   English

Flutter Go_router,保存路由历史

[英]Flutter Go_router, save history of routes

I have this code of router我有这个路由器代码

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!],
    );
  }
}

And on different screens I go deeper and deeper.在不同的屏幕上,我 go 越来越深。 And it works the way I need it.它按照我需要的方式工作。 But when I click back, I don't move back through the history, but go back to the beginning但是我点击后退的时候,并没有通过历史往回移动,而是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}')

How do I make it so that when we click back, we return to the previous page, and not to the initial one?我该怎么做才能让我们在点击后退时返回到上一页,而不是返回到初始页面?

back buttons have this code后退按钮有这个代码

Navigator.pop(context);

At first I tried to do it through a regular Navigator, then through Navigator 2.0.起初我尝试通过常规 Navigator 来完成,然后通过 Navigator 2.0。 But it didn't work.但它没有用。 Then I found this go router and I liked it, but I can't solve the last problem然后找到了这个go路由器,很喜欢,就是解决不了最后一个问题

Use context.push('routeName') instead of context.go('routeName').使用 context.push('routeName') 而不是 context.go('routeName')。 That should resolve the issue.那应该可以解决问题。

Have you tried using the go_router pop extension instead of Navigator.pop(context)?您是否尝试过使用 go_router pop 扩展而不是 Navigator.pop(context)?

import 'package:go_router/go_router.dart';

context.pop();

Link to a page explaining the difference between push and go: Go vs Push链接到解释推送和 go 之间区别的页面: Go 与推送

You can use context.push('/router').您可以使用 context.push('/router')。 This is a link about the different between context.go('/...') and context.push('/...').这是关于 context.go('/...') 和 context.push('/...') 之间差异的链接。 https://codewithandrea.com/articles/flutter-navigation-gorouter-go-vs-push/ https://codewithandrea.com/articles/flutter-navigation-gorouter-go-vs-push/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM