1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'basic.dart';
6import 'framework.dart';
7import 'routes.dart';
8
9/// A modal route that replaces the entire screen.
10///
11/// The [PageRouteBuilder] subclass provides a way to create a [PageRoute] using
12/// callbacks rather than by defining a new class via subclassing.
13///
14/// If `barrierDismissible` is true, then pressing the escape key on the keyboard
15/// will cause the current route to be popped with null as the value.
16///
17/// See also:
18///
19/// * [Route], which documents the meaning of the `T` generic type argument.
20abstract class PageRoute<T> extends ModalRoute<T> {
21 /// Creates a modal route that replaces the entire screen.
22 PageRoute({
23 super.settings,
24 this.fullscreenDialog = false,
25 this.allowSnapshotting = true,
26 bool barrierDismissible = false,
27 }) : _barrierDismissible = barrierDismissible;
28
29 /// {@template flutter.widgets.PageRoute.fullscreenDialog}
30 /// Whether this page route is a full-screen dialog.
31 ///
32 /// In Material and Cupertino, being fullscreen has the effects of making
33 /// the app bars have a close button instead of a back button. On
34 /// iOS, dialogs transitions animate differently and are also not closeable
35 /// with the back swipe gesture.
36 /// {@endtemplate}
37 final bool fullscreenDialog;
38
39 @override
40 final bool allowSnapshotting;
41
42 @override
43 bool get opaque => true;
44
45 @override
46 bool get barrierDismissible => _barrierDismissible;
47 final bool _barrierDismissible;
48
49 @override
50 bool canTransitionTo(TransitionRoute<dynamic> nextRoute) => nextRoute is PageRoute;
51
52 @override
53 bool canTransitionFrom(TransitionRoute<dynamic> previousRoute) => previousRoute is PageRoute;
54}
55
56Widget _defaultTransitionsBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
57 return child;
58}
59
60/// A utility class for defining one-off page routes in terms of callbacks.
61///
62/// Callers must define the [pageBuilder] function which creates the route's
63/// primary contents. To add transitions define the [transitionsBuilder] function.
64///
65/// The `T` generic type argument corresponds to the type argument of the
66/// created [Route] objects.
67///
68/// See also:
69///
70/// * [Route], which documents the meaning of the `T` generic type argument.
71class PageRouteBuilder<T> extends PageRoute<T> {
72 /// Creates a route that delegates to builder callbacks.
73 PageRouteBuilder({
74 super.settings,
75 required this.pageBuilder,
76 this.transitionsBuilder = _defaultTransitionsBuilder,
77 this.transitionDuration = const Duration(milliseconds: 300),
78 this.reverseTransitionDuration = const Duration(milliseconds: 300),
79 this.opaque = true,
80 this.barrierDismissible = false,
81 this.barrierColor,
82 this.barrierLabel,
83 this.maintainState = true,
84 super.fullscreenDialog,
85 super.allowSnapshotting = true,
86 });
87
88 /// {@template flutter.widgets.pageRouteBuilder.pageBuilder}
89 /// Used build the route's primary contents.
90 ///
91 /// See [ModalRoute.buildPage] for complete definition of the parameters.
92 /// {@endtemplate}
93 final RoutePageBuilder pageBuilder;
94
95 /// {@template flutter.widgets.pageRouteBuilder.transitionsBuilder}
96 /// Used to build the route's transitions.
97 ///
98 /// See [ModalRoute.buildTransitions] for complete definition of the parameters.
99 /// {@endtemplate}
100 ///
101 /// The default transition is a jump cut (i.e. no animation).
102 final RouteTransitionsBuilder transitionsBuilder;
103
104 @override
105 final Duration transitionDuration;
106
107 @override
108 final Duration reverseTransitionDuration;
109
110 @override
111 final bool opaque;
112
113 @override
114 final bool barrierDismissible;
115
116 @override
117 final Color? barrierColor;
118
119 @override
120 final String? barrierLabel;
121
122 @override
123 final bool maintainState;
124
125 @override
126 Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
127 return pageBuilder(context, animation, secondaryAnimation);
128 }
129
130 @override
131 Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
132 return transitionsBuilder(context, animation, secondaryAnimation, child);
133 }
134}
135