How to check previous navigate pages?
Example: HomeScreen > ProductListScreen >ViewCart > Payment (Now I am in the Payment Page)
like this: print(priousPages);
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,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With