diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index e27090c926..3094f0faca 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -29,6 +29,9 @@ import 'transitions.dart'; // Examples can assume: // dynamic routeObserver; // late NavigatorState navigator; +// late BuildContext context; +// Future askTheUserIfTheyAreSure() async { return true; } +// abstract class MyWidget extends StatefulWidget { const MyWidget({Key? key}) : super(key: key); } /// A route that displays widgets in the [Navigator]'s [Overlay]. abstract class OverlayRoute extends Route { @@ -896,11 +899,14 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute? route = ModalRoute.of(context); /// ``` + /// {@end-tool} /// /// The given [BuildContext] will be rebuilt if the state of the route changes /// while it is visible (specifically, if [isCurrent] or [canPop] change value). @@ -993,22 +999,26 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute animation, /// Animation secondaryAnimation, - /// Widget child, /// ) { /// return Scaffold( - /// appBar: AppBar(title: Text('Hello')), - /// body: Center( + /// appBar: AppBar(title: const Text('Hello')), + /// body: const Center( /// child: Text('Hello World'), /// ), /// ); @@ -1027,12 +1037,9 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute animation, + /// Animation secondaryAnimation, + /// ) { + /// return Scaffold( + /// appBar: AppBar(title: const Text('Hello')), + /// body: const Center( + /// child: Text('Hello World'), + /// ), + /// ); + /// }, /// transitionsBuilder: ( /// BuildContext context, /// Animation animation, @@ -1057,20 +1077,22 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute( /// begin: const Offset(0.0, 1.0), /// end: Offset.zero, /// ).animate(animation), /// child: SlideTransition( - /// position: TweenOffset( + /// position: Tween( /// begin: Offset.zero, /// end: const Offset(0.0, 1.0), /// ).animate(secondaryAnimation), /// child: child, /// ), - /// ); - /// } + /// ); + /// }, + /// ) /// ``` + /// {@end-tool} /// /// In practice the `secondaryAnimation` is used pretty rarely. /// @@ -1372,6 +1394,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute _route; + /// abstract class _MyWidgetState extends State { + /// ModalRoute? _route; /// - /// @override - /// void didChangeDependencies() { - /// super.didChangeDependencies(); - /// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); - /// _route = ModalRoute.of(context); - /// _route?.addScopedWillPopCallback(askTheUserIfTheyAreSure); + /// // ... + /// + /// @override + /// void didChangeDependencies() { + /// super.didChangeDependencies(); + /// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); + /// _route = ModalRoute.of(context); + /// _route?.addScopedWillPopCallback(askTheUserIfTheyAreSure); + /// } /// } /// ``` + /// {@end-tool} /// + /// {@tool snippet} /// If you register a callback manually, be sure to remove the callback with /// [removeScopedWillPopCallback] by the time the widget has been disposed. A /// stateful widget can do this in its dispose method (continuing the previous /// example): /// /// ```dart - /// @override - /// void dispose() { - /// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); - /// _route = null; - /// super.dispose(); + /// abstract class _MyWidgetState2 extends State { + /// ModalRoute? _route; + /// + /// // ... + /// + /// @override + /// void dispose() { + /// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); + /// _route = null; + /// super.dispose(); + /// } /// } /// ``` + /// {@end-tool} /// /// See also: ///