Fix doc samples in ridgets/routes.dart (#86453)
This commit is contained in:
committed by
GitHub
parent
72ae6e236e
commit
c4c5e0d758
@@ -29,6 +29,9 @@ import 'transitions.dart';
|
||||
// Examples can assume:
|
||||
// dynamic routeObserver;
|
||||
// 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].
|
||||
abstract class OverlayRoute<T> extends Route<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.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
///
|
||||
/// Typical usage is as follows:
|
||||
///
|
||||
/// ```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
|
||||
/// 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
|
||||
/// topmost route, e.g. because the use pressed the back button, the
|
||||
/// primary animation runs from 1.0 to 0.0.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
/// The following example uses the primary animation to drive a
|
||||
/// [SlideTransition] that translates the top of the new route vertically
|
||||
/// from the bottom of the screen when it is pushed on the Navigator's
|
||||
/// stack. When the route is popped the SlideTransition translates the
|
||||
/// 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
|
||||
/// PageRouteBuilder(
|
||||
/// pageBuilder: (BuildContext context,
|
||||
/// Animation<double> animation,
|
||||
/// Animation<double> 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<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
/// child: child, // child is the value returned by pageBuilder
|
||||
/// );
|
||||
/// },
|
||||
/// );
|
||||
/// )
|
||||
/// ```
|
||||
///
|
||||
/// 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.
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// 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
|
||||
@@ -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
|
||||
/// 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
|
||||
/// [secondaryAnimation]. When this route disappears because a new route has
|
||||
/// 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
|
||||
/// route has been popped off.
|
||||
///
|
||||
/// ```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: (
|
||||
/// BuildContext context,
|
||||
/// Animation<double> animation,
|
||||
@@ -1057,20 +1077,22 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
/// Widget child,
|
||||
/// ) {
|
||||
/// return SlideTransition(
|
||||
/// position: AlignmentTween(
|
||||
/// position: Tween<Offset>(
|
||||
/// begin: const Offset(0.0, 1.0),
|
||||
/// end: Offset.zero,
|
||||
/// ).animate(animation),
|
||||
/// child: SlideTransition(
|
||||
/// position: TweenOffset(
|
||||
/// position: Tween<Offset>(
|
||||
/// 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<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
|
||||
/// Enables this route to veto attempts by the user to dismiss it.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
/// This callback is typically added using a [WillPopScope] widget. That
|
||||
/// widget finds the enclosing [ModalRoute] and uses this function to register
|
||||
/// this callback:
|
||||
@@ -1379,11 +1402,15 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
|
||||
/// ```dart
|
||||
/// Widget build(BuildContext context) {
|
||||
/// return WillPopScope(
|
||||
/// onWillPop: askTheUserIfTheyAreSure,
|
||||
/// child: ...,
|
||||
/// onWillPop: () async {
|
||||
/// // 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
|
||||
/// 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
|
||||
/// 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.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
/// To register a callback manually, look up the enclosing [ModalRoute] in a
|
||||
/// [State.didChangeDependencies] callback:
|
||||
///
|
||||
/// ```dart
|
||||
/// ModalRoute<dynamic> _route;
|
||||
/// abstract class _MyWidgetState extends State<MyWidget> {
|
||||
/// ModalRoute<dynamic>? _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<MyWidget> {
|
||||
/// ModalRoute<dynamic>? _route;
|
||||
///
|
||||
/// // ...
|
||||
///
|
||||
/// @override
|
||||
/// void dispose() {
|
||||
/// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure);
|
||||
/// _route = null;
|
||||
/// super.dispose();
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user