From 8e3884820386246553b902b16494f41f9310adf4 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Thu, 20 Jul 2017 14:26:52 -0700 Subject: [PATCH] Fix sample code and update docs (#11257) * fix sample code * review comments * review comments * document other members for extra bonus points --- packages/flutter/lib/src/widgets/routes.dart | 46 ++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index fd75f000e0..80f7cb1ae4 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -971,11 +971,37 @@ abstract class PopupRoute extends ModalRoute { /// A [Navigator] observer that notifies [RouteAware]s 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` +/// will inform subscribed [RouteAware]s 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`. +/// +/// ## Sample code +/// /// To make a [StatefulWidget] aware of its current [Route] state, implement -/// [RouteAware] in its [State] and subscribe it to the [RouteObserver]: +/// [RouteAware] in its [State] and subscribe it to a [RouteObserver]: /// /// ```dart +/// // Register the RouteObserver as a navigation observer. +/// final RouteObserver routeObserver = new RouteObserver(); +/// void main() { +/// runApp(new MaterialApp( +/// home: new Container(), +/// navigatorObservers: [routeObserver], +/// )); +/// } +/// +/// class RouteAwareWidget extends StatefulWidget { +/// State createState() => new RouteAwareWidgetState(); +/// } +/// +/// // Implement RouteAware in a widget's state and subscribe it to the RouteObserver. /// class RouteAwareWidgetState extends State with RouteAware { +/// /// @override /// void didChangeDependencies() { /// super.didChangeDependencies(); @@ -990,19 +1016,28 @@ abstract class PopupRoute extends ModalRoute { /// /// @override /// void didPush() { -/// // Do something +/// // Route was pushed onto navigator and is now topmost route. /// } /// /// @override /// void didPopNext() { -/// // Do something +/// // Covering route was popped off the navigator. /// } /// +/// @override +/// Widget build(BuildContext context) => new Container(); +/// /// } +/// /// ``` class RouteObserver> extends NavigatorObserver { final Map _listeners = {}; + /// Subscribe [routeAware] to be informed about changes to [route]. + /// + /// Going forward, [routeAware] will be informed about qualifying changes + /// to [route], e.g. when [route] is covered by another route or when [route] + /// is popped off the [Navigator] stack. void subscribe(RouteAware routeAware, T route) { assert(routeAware != null); assert(route != null); @@ -1012,6 +1047,9 @@ class RouteObserver> extends NavigatorObserver { } } + /// Unsubscribe [routeAware]. + /// + /// [routeAware] is no longer informed about changes to its route. void unsubscribe(RouteAware routeAware) { assert(routeAware != null); _listeners.remove(routeAware); @@ -1048,4 +1086,4 @@ abstract class RouteAware { /// Called when a new route has been pushed, and the current route is no /// longer visible. void didPushNext() { } -} \ No newline at end of file +}