flutter GoRouter:页面上的刷新松开“后退”按钮

jobtbby3  于 2023-03-24  发布在  Flutter
关注(0)|答案(2)|浏览(121)

我有一个主屏幕和一个细节页面。当我导航到细节页面时,Flutter会自动在AppBar中添加一个后退按钮,当点击该按钮时,会返回到上一页。
现在使用GoRouter,UrlPathStrategy.path允许我的详细页面在/detail,当刷新页面时,它直接打开详细页面。这一切都很好,然而,问题是,在/detail刷新后没有返回按钮。
是否有这样一个概念,即GoRouter可以根据路由推断导航堆栈,从而在打开/detail页面时,显示一个返回按钮,导致/(主页)?
我当前的路由看起来像这样:

routes: [
  GoRoute(
    name: "detail",
    path: "/detail",
    builder: (context, state) => DetailPage(),
  ),
  GoRoute(
    name: "home",
    path: "/",
    builder: (context, state) => HomePage(),
  ),
],
sf6xfgos

sf6xfgos1#

可能只是把详细路线放在回家路线里?

routes: [
  GoRoute(
    name: "home",
    path: "/",
    builder: (context, state) => HomePage(),
    routes: [
      GoRoute(
        name: "detail",
        path: "/detail",
        builder: (context, state) => DetailPage(),
      ),
    ],
  ),
],
2izufjch

2izufjch2#

由于您位于您知道的页面上,因此可以使用go_router pushReplacement代替refresh
以下是来自router.dart

/// Replaces the top-most page of the page stack with the given URL location
  /// w/ optional query parameters, e.g. `/family/f2/person/p1?color=blue`.
  ///
  /// See also:
  /// * [go] which navigates to the location.
  /// * [push] which pushes the location onto the page stack.
  void pushReplacement(String location, {Object? extra}) {
    routeInformationParser
        .parseRouteInformationWithDependencies(
      RouteInformation(location: location, state: extra),
      // TODO(chunhtai): avoid accessing the context directly through global key.
      // https://github.com/flutter/flutter/issues/99112
      _routerDelegate.navigatorKey.currentContext!,
    )
        .then<void>((RouteMatchList matchList) {
      routerDelegate.pushReplacement(matchList);
    });
  }

它工作得很好:)

相关问题