Skip to content

Instantly share code, notes, and snippets.

@vishna
Created February 4, 2020 13:31
Show Gist options
  • Save vishna/82cf8b56323f93d0aeaa644492e667ed to your computer and use it in GitHub Desktop.
Save vishna/82cf8b56323f93d0aeaa644492e667ed to your computer and use it in GitHub Desktop.
navigator_observer_delegate.dart
// utility class so that you don't have to extend from [NavigatorObserver] but rather
/// pass the function you want
class NavigatorObserverDelegate extends NavigatorObserver {
NavigatorObserverDelegate(
{this.onDidPush,
this.onDidPop,
this.onDidRemove,
this.onDidReplace,
this.onDidStartUserGesture,
this.onDidStopUserGesture});
final Function(Route<dynamic> route, Route<dynamic> previousRoute) onDidPush;
final Function(Route<dynamic> route, Route<dynamic> previousRoute) onDidPop;
final Function(Route<dynamic> route, Route<dynamic> previousRoute)
onDidRemove;
final Function({Route<dynamic> newRoute, Route<dynamic> oldRoute})
onDidReplace;
final Function(Route<dynamic> route, Route<dynamic> previousRoute)
onDidStartUserGesture;
final Function() onDidStopUserGesture;
@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
onDidPush?.call(route, previousRoute);
}
@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
onDidPop?.call(route, previousRoute);
}
@override
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
onDidRemove?.call(route, previousRoute);
}
@override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
onDidReplace?.call(newRoute: newRoute, oldRoute: oldRoute);
}
@override
void didStartUserGesture(Route<dynamic> route, Route<dynamic> previousRoute) {
onDidStartUserGesture?.call(route, previousRoute);
}
@override
void didStopUserGesture() {
onDidStopUserGesture?.call();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment