A Navigator observer that notifies RouteAwares of changes to the state of their Route.

RouteObserver informs subscribers whenever a route of type T is pushed on top of their own route of type T or popped from it. This is for example useful to keep track of page transitions, e.i. a RouteObserver<PageRoute> will inform subscribed RouteAwares whenever the user navigates away from the current page route to another page route.

If you want to be informed about route changes of any type, you should instantiate a RouteObserver<Route>.

Sample code

To make a StatefulWidget aware of its current Route state, implement RouteAware in its State and subscribe it to a RouteObserver:

// Register the RouteObserver as a navigation observer.
final RouteObserver<PageRoute> routeObserver = new RouteObserver<PageRoute>();
void main() {
  runApp(new MaterialApp(
    home: new Container(),
    navigatorObservers: [routeObserver],
  ));
}

class RouteAwareWidget extends StatefulWidget {
  State<RouteAwareWidget> createState() => new RouteAwareWidgetState();
}

// Implement RouteAware in a widget's state and subscribe it to the RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {

  @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.
  }

  @override
  Widget build(BuildContext context) => new Container();

}

Inheritance

Constructors

RouteObserver()

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
The navigator that the observer is observing, if any.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

didPop(Route route, Route previousRoute) → void
The Navigator popped route.
didPush(Route route, Route previousRoute) → void
The Navigator pushed route.
subscribe(RouteAware routeAware, T route) → void
Subscribe routeAware to be informed about changes to route. [...]
unsubscribe(RouteAware routeAware) → void
Unsubscribe routeAware. [...]
didRemove(Route route, Route previousRoute) → void
The Navigator removed route.
inherited
didStartUserGesture() → void
The Navigator's routes are being moved by a user gesture. [...]
inherited
didStopUserGesture() → void
User gesture is no longer controlling the Navigator. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(other) bool
The equality operator. [...]
inherited