Unverified Commit c4c5e0d7 authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Fix doc samples in ridgets/routes.dart (#86453)

parent 72ae6e23
...@@ -29,6 +29,9 @@ import 'transitions.dart'; ...@@ -29,6 +29,9 @@ import 'transitions.dart';
// Examples can assume: // Examples can assume:
// dynamic routeObserver; // dynamic routeObserver;
// late NavigatorState navigator; // late NavigatorState navigator;
// late BuildContext context;
// Future<bool> 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]. /// A route that displays widgets in the [Navigator]'s [Overlay].
abstract class OverlayRoute<T> extends Route<T> { abstract class OverlayRoute<T> extends Route<T> {
...@@ -896,11 +899,14 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -896,11 +899,14 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// ///
/// Returns null if the given context is not associated with a modal route. /// Returns null if the given context is not associated with a modal route.
/// ///
/// {@tool snippet}
///
/// Typical usage is as follows: /// Typical usage is as follows:
/// ///
/// ```dart /// ```dart
/// ModalRoute route = ModalRoute.of(context); /// ModalRoute<int>? route = ModalRoute.of<int>(context);
/// ``` /// ```
/// {@end-tool}
/// ///
/// The given [BuildContext] will be rebuilt if the state of the route changes /// The given [BuildContext] will be rebuilt if the state of the route changes
/// while it is visible (specifically, if [isCurrent] or [canPop] change value). /// while it is visible (specifically, if [isCurrent] or [canPop] change value).
...@@ -993,22 +999,26 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -993,22 +999,26 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// topmost route, e.g. because the use pressed the back button, the /// topmost route, e.g. because the use pressed the back button, the
/// primary animation runs from 1.0 to 0.0. /// primary animation runs from 1.0 to 0.0.
/// ///
/// {@tool snippet}
/// The following example uses the primary animation to drive a /// The following example uses the primary animation to drive a
/// [SlideTransition] that translates the top of the new route vertically /// [SlideTransition] that translates the top of the new route vertically
/// from the bottom of the screen when it is pushed on the Navigator's /// from the bottom of the screen when it is pushed on the Navigator's
/// stack. When the route is popped the SlideTransition translates the /// stack. When the route is popped the SlideTransition translates the
/// route from the top of the screen back to the bottom. /// route from the top of the screen back to the bottom.
/// ///
/// We've used [PageRouteBuilder] to demonstrate the [buildTransitions] method
/// here. The body of an override of the [buildTransitions] method would be
/// defined in the same way.
///
/// ```dart /// ```dart
/// PageRouteBuilder( /// PageRouteBuilder(
/// pageBuilder: (BuildContext context, /// pageBuilder: (BuildContext context,
/// Animation<double> animation, /// Animation<double> animation,
/// Animation<double> secondaryAnimation, /// Animation<double> secondaryAnimation,
/// Widget child,
/// ) { /// ) {
/// return Scaffold( /// return Scaffold(
/// appBar: AppBar(title: Text('Hello')), /// appBar: AppBar(title: const Text('Hello')),
/// body: Center( /// body: const Center(
/// child: Text('Hello World'), /// child: Text('Hello World'),
/// ), /// ),
/// ); /// );
...@@ -1027,12 +1037,9 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1027,12 +1037,9 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// child: child, // child is the value returned by pageBuilder /// child: child, // child is the value returned by pageBuilder
/// ); /// );
/// }, /// },
/// ); /// )
/// ``` /// ```
/// /// {@end-tool}
/// We've used [PageRouteBuilder] to demonstrate the [buildTransitions] method
/// here. The body of an override of the [buildTransitions] method would be
/// defined in the same way.
/// ///
/// When the [Navigator] pushes a route on the top of its stack, the /// When the [Navigator] pushes a route on the top of its stack, the
/// [secondaryAnimation] can be used to define how the route that was on /// [secondaryAnimation] can be used to define how the route that was on
...@@ -1043,6 +1050,7 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1043,6 +1050,7 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// runs from 0.0 to 1.0. When the Navigator pops the topmost route, the /// runs from 0.0 to 1.0. When the Navigator pops the topmost route, the
/// secondaryAnimation for the route below it runs from 1.0 to 0.0. /// secondaryAnimation for the route below it runs from 1.0 to 0.0.
/// ///
/// {@tool snippet}
/// The example below adds a transition that's driven by the /// The example below adds a transition that's driven by the
/// [secondaryAnimation]. When this route disappears because a new route has /// [secondaryAnimation]. When this route disappears because a new route has
/// been pushed on top of it, it translates in the opposite direction of /// been pushed on top of it, it translates in the opposite direction of
...@@ -1050,6 +1058,18 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1050,6 +1058,18 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// route has been popped off. /// route has been popped off.
/// ///
/// ```dart /// ```dart
/// PageRouteBuilder(
/// pageBuilder: (BuildContext context,
/// Animation<double> animation,
/// Animation<double> secondaryAnimation,
/// ) {
/// return Scaffold(
/// appBar: AppBar(title: const Text('Hello')),
/// body: const Center(
/// child: Text('Hello World'),
/// ),
/// );
/// },
/// transitionsBuilder: ( /// transitionsBuilder: (
/// BuildContext context, /// BuildContext context,
/// Animation<double> animation, /// Animation<double> animation,
...@@ -1057,20 +1077,22 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1057,20 +1077,22 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// Widget child, /// Widget child,
/// ) { /// ) {
/// return SlideTransition( /// return SlideTransition(
/// position: AlignmentTween( /// position: Tween<Offset>(
/// begin: const Offset(0.0, 1.0), /// begin: const Offset(0.0, 1.0),
/// end: Offset.zero, /// end: Offset.zero,
/// ).animate(animation), /// ).animate(animation),
/// child: SlideTransition( /// child: SlideTransition(
/// position: TweenOffset( /// position: Tween<Offset>(
/// begin: Offset.zero, /// begin: Offset.zero,
/// end: const Offset(0.0, 1.0), /// end: const Offset(0.0, 1.0),
/// ).animate(secondaryAnimation), /// ).animate(secondaryAnimation),
/// child: child, /// child: child,
/// ), /// ),
/// ); /// );
/// } /// },
/// )
/// ``` /// ```
/// {@end-tool}
/// ///
/// In practice the `secondaryAnimation` is used pretty rarely. /// In practice the `secondaryAnimation` is used pretty rarely.
/// ///
...@@ -1372,6 +1394,7 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1372,6 +1394,7 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// Enables this route to veto attempts by the user to dismiss it. /// Enables this route to veto attempts by the user to dismiss it.
/// ///
/// {@tool snippet}
/// This callback is typically added using a [WillPopScope] widget. That /// This callback is typically added using a [WillPopScope] widget. That
/// widget finds the enclosing [ModalRoute] and uses this function to register /// widget finds the enclosing [ModalRoute] and uses this function to register
/// this callback: /// this callback:
...@@ -1379,11 +1402,15 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1379,11 +1402,15 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// ```dart /// ```dart
/// Widget build(BuildContext context) { /// Widget build(BuildContext context) {
/// return WillPopScope( /// return WillPopScope(
/// onWillPop: askTheUserIfTheyAreSure, /// onWillPop: () async {
/// child: ..., /// // ask the user if they are sure
/// return true;
/// },
/// child: Container(),
/// ); /// );
/// } /// }
/// ``` /// ```
/// {@end-tool}
/// ///
/// This callback runs asynchronously and it's possible that it will be called /// This callback runs asynchronously and it's possible that it will be called
/// after its route has been disposed. The callback should check [State.mounted] /// after its route has been disposed. The callback should check [State.mounted]
...@@ -1393,34 +1420,48 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T ...@@ -1393,34 +1420,48 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// unsaved [Form] data if the user attempts to back out of the form. In that /// unsaved [Form] data if the user attempts to back out of the form. In that
/// case, use the [Form.onWillPop] property to register the callback. /// case, use the [Form.onWillPop] property to register the callback.
/// ///
/// {@tool snippet}
/// To register a callback manually, look up the enclosing [ModalRoute] in a /// To register a callback manually, look up the enclosing [ModalRoute] in a
/// [State.didChangeDependencies] callback: /// [State.didChangeDependencies] callback:
/// ///
/// ```dart /// ```dart
/// ModalRoute<dynamic> _route; /// abstract class _MyWidgetState extends State<MyWidget> {
/// /// ModalRoute<dynamic>? _route;
/// @override ///
/// void didChangeDependencies() { /// // ...
/// super.didChangeDependencies(); ///
/// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); /// @override
/// _route = ModalRoute.of(context); /// void didChangeDependencies() {
/// _route?.addScopedWillPopCallback(askTheUserIfTheyAreSure); /// 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 /// If you register a callback manually, be sure to remove the callback with
/// [removeScopedWillPopCallback] by the time the widget has been disposed. A /// [removeScopedWillPopCallback] by the time the widget has been disposed. A
/// stateful widget can do this in its dispose method (continuing the previous /// stateful widget can do this in its dispose method (continuing the previous
/// example): /// example):
/// ///
/// ```dart /// ```dart
/// @override /// abstract class _MyWidgetState2 extends State<MyWidget> {
/// void dispose() { /// ModalRoute<dynamic>? _route;
/// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); ///
/// _route = null; /// // ...
/// super.dispose(); ///
/// @override
/// void dispose() {
/// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure);
/// _route = null;
/// super.dispose();
/// }
/// } /// }
/// ``` /// ```
/// {@end-tool}
/// ///
/// See also: /// See also:
/// ///
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment