Skip to content

Instantly share code, notes, and snippets.

@marcinOz
Created February 12, 2019 18:26
Show Gist options
  • Save marcinOz/71432c248322184ea0ed7f72932d4720 to your computer and use it in GitHub Desktop.
Save marcinOz/71432c248322184ea0ed7f72932d4720 to your computer and use it in GitHub Desktop.
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
class RouteAwareWidget extends StatefulWidget {
final Widget child;
RouteAwareWidget({this.child});
State<RouteAwareWidget> createState() => RouteAwareWidgetState(child: child);
}
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {
final Widget child;
RouteAwareWidgetState({this.child});
@override
void didChangeDependencies() {
super.didChangeDependencies();
routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void dispose() {
routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPush() {
// Route was pushed onto navigator and is now topmost route.
}
@override
void didPopNext() {
// Covering route was popped off the navigator.
StoreProvider.of<AppState>(context).dispatch(NavigatePopAction());
}
@override
Widget build(BuildContext context) => Container(child: child);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment