pages.dart 4.34 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'basic.dart';
6
import 'framework.dart';
7 8 9
import 'routes.dart';

/// A modal route that replaces the entire screen.
10 11 12 13 14 15 16
///
/// The [PageRouteBuilder] subclass provides a way to create a [PageRoute] using
/// callbacks rather than by defining a new class via subclassing.
///
/// See also:
///
///  * [Route], which documents the meaning of the `T` generic type argument.
17
abstract class PageRoute<T> extends ModalRoute<T> {
18
  /// Creates a modal route that replaces the entire screen.
19
  PageRoute({
20
    super.settings,
21
    this.fullscreenDialog = false,
22
    this.allowSnapshotting = true,
23
  });
24

25
  /// {@template flutter.widgets.PageRoute.fullscreenDialog}
26 27 28 29 30 31
  /// Whether this page route is a full-screen dialog.
  ///
  /// In Material and Cupertino, being fullscreen has the effects of making
  /// the app bars have a close button instead of a back button. On
  /// iOS, dialogs transitions animate differently and are also not closeable
  /// with the back swipe gesture.
32
  /// {@endtemplate}
33 34
  final bool fullscreenDialog;

35
  @override
36
  final bool allowSnapshotting;
37

38
  @override
39
  bool get opaque => true;
40 41

  @override
42
  bool get barrierDismissible => false;
43 44

  @override
45
  bool canTransitionTo(TransitionRoute<dynamic> nextRoute) => nextRoute is PageRoute;
46 47

  @override
48
  bool canTransitionFrom(TransitionRoute<dynamic> previousRoute) => previousRoute is PageRoute;
49
}
50

51
Widget _defaultTransitionsBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
52 53 54 55 56 57 58
  return child;
}

/// A utility class for defining one-off page routes in terms of callbacks.
///
/// Callers must define the [pageBuilder] function which creates the route's
/// primary contents. To add transitions define the [transitionsBuilder] function.
59 60 61 62 63 64 65
///
/// The `T` generic type argument corresponds to the type argument of the
/// created [Route] objects.
///
/// See also:
///
///  * [Route], which documents the meaning of the `T` generic type argument.
66
class PageRouteBuilder<T> extends PageRoute<T> {
67
  /// Creates a route that delegates to builder callbacks.
68
  ///
69
  /// The [pageBuilder], [transitionsBuilder], [opaque], [barrierDismissible],
70
  /// [maintainState], and [fullscreenDialog] arguments must not be null.
71
  PageRouteBuilder({
72
    super.settings,
73
    required this.pageBuilder,
74 75
    this.transitionsBuilder = _defaultTransitionsBuilder,
    this.transitionDuration = const Duration(milliseconds: 300),
76
    this.reverseTransitionDuration = const Duration(milliseconds: 300),
77 78
    this.opaque = true,
    this.barrierDismissible = false,
79
    this.barrierColor,
80
    this.barrierLabel,
81
    this.maintainState = true,
82
    super.fullscreenDialog,
83
    super.allowSnapshotting = true,
84 85
  }) : assert(pageBuilder != null),
       assert(transitionsBuilder != null),
86
       assert(opaque != null),
87 88
       assert(barrierDismissible != null),
       assert(maintainState != null),
89
       assert(fullscreenDialog != null);
90

91
  /// {@template flutter.widgets.pageRouteBuilder.pageBuilder}
92 93 94
  /// Used build the route's primary contents.
  ///
  /// See [ModalRoute.buildPage] for complete definition of the parameters.
95
  /// {@endtemplate}
96
  final RoutePageBuilder pageBuilder;
97

98
  /// {@template flutter.widgets.pageRouteBuilder.transitionsBuilder}
99 100 101
  /// Used to build the route's transitions.
  ///
  /// See [ModalRoute.buildTransitions] for complete definition of the parameters.
102
  /// {@endtemplate}
103 104
  ///
  /// The default transition is a jump cut (i.e. no animation).
105 106 107 108 109
  final RouteTransitionsBuilder transitionsBuilder;

  @override
  final Duration transitionDuration;

110 111 112
  @override
  final Duration reverseTransitionDuration;

113 114 115 116
  @override
  final bool opaque;

  @override
117
  final bool barrierDismissible;
118 119

  @override
120
  final Color? barrierColor;
121

122
  @override
123
  final String? barrierLabel;
124

125 126 127 128
  @override
  final bool maintainState;

  @override
129 130
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return pageBuilder(context, animation, secondaryAnimation);
131 132 133
  }

  @override
134 135
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return transitionsBuilder(context, animation, secondaryAnimation, child);
136
  }
137
}