You can use approach with extending Navigator Observer.
Observer will have method for check if page in route.
In my example I use Get lib:
- Extend navigator observer:
class YourRouteObserver extends RouteObserver<PageRoute<dynamic>> {
static final List<Route> routeStack = [];
@override
void didPush(Route route, Route? previousRoute) {
routeStack.add(route);
super.didPush(route, previousRoute);
}
@override
void didPop(Route route, Route? previousRoute) {
routeStack.remove(route);
super.didPop(route, previousRoute);
}
@override
void didRemove(Route route, Route? previousRoute) {
routeStack.remove(route);
super.didRemove(route, previousRoute);
}
@override
void didReplace({Route? newRoute, Route? oldRoute}) {
final index = routeStack.indexOf(oldRoute!);
if (index != -1) {
routeStack[index] = newRoute!;
} else {
routeStack.add(newRoute!);
}
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
}
static bool containsRoute(String routeName) {
return routeStack.any((route) => route.settings.name == routeName);
}
}
- Add observer to your app:
void main() {
final routeObserver = YourRouteObserver();
runApp(
GetMaterialApp(
getPages: [
GetPage(name: RouteName.yourPage, page: () => const YourPage()),
],
navigatorObservers: [routeObserver],
),
);
}
2.1. RouteName just abstract class with Strings, but remember to use / in beginning of name:
abstract class RouteName {
static const yourPage = '/your-page';
}
Open page by name: onTap: () => Get.toNamed(RouteName.yourPage),
Check if page in stack: AppRouteObserver.containsRoute(RouteName.yourPage)
NavigationState
? Thanks! – Flagelliform