是否可以在Flutter上查看我们之前的导航页面?

时间:2019-10-28 02:49:16

标签: flutter dart

如何查看以前的导航页面?

示例:主屏幕>产品列表屏幕> ViewCart>付款(现在我在“付款”页面中)

像这样:print(priousPages);

1 个答案:

答案 0 :(得分:0)

将上一页小部件作为参数传递给“付款”小部件构造函数。

// In page you want to navigate to Payment widget.
Navigator.of(context).push(MaterialPageRoute(
  builder: (context) => Payment(priousPages: this.runtimeType);
));

或者,您可以使用NavigatorObserver来接收导航器的事件,并且可以在观察器中记录路线的变化。

class _NavigatorHistory extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
    print("${route.settings.name} pushed");
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
    print("${route.settings.name} popped");
  }

  @override
  void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
    print("${oldRoute.settings.name} is replaced by ${newRoute.settings.name}");
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
    print("${route.settings.name} removed");
  }
}

您可以使用route.settings.name来确定路线。因此,在创建路线时,应提供路线名称。

MaterialPageRoute(
  builder: (context) => YourWidget(),
  settings: RouteSettings(name: "/payment")
)

最后,请不要忘记将此观察者注册到App。

MaterialApp(
  navigatorObservers:[ _NavigatorHistory()],
  title: "App title",
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: homePage,
)