Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check our previous navigate pages on Flutter?

Tags:

flutter

dart

How to check previous navigate pages?

Example: HomeScreen > ProductListScreen >ViewCart > Payment (Now I am in the Payment Page)

like this: print(priousPages);

like image 856
BIS Tech Avatar asked Nov 01 '25 14:11

BIS Tech


1 Answers

Pass previous page widget as parameter to the Payment widget constructor.

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

Or, you can use NavigatorObserver to receive events of navigator, and you can record changes of route in observer.

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");
  }
}

You can use route.settings.name to identify the route. So when you create route, you should give a route name.

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

Lastly, don't forget to register this observer to App.

MaterialApp(
  navigatorObservers:[ _NavigatorHistory()],
  title: "App title",
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: homePage,
)
like image 130
JunYao Yuan Avatar answered Nov 04 '25 05:11

JunYao Yuan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!