pages.dart 3.86 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 10 11
import 'navigator.dart';
import 'routes.dart';

/// A modal route that replaces the entire screen.
abstract class PageRoute<T> extends ModalRoute<T> {
12
  /// Creates a modal route that replaces the entire screen.
13
  PageRoute({
14
    RouteSettings? settings,
15
    this.fullscreenDialog = false,
16
  }) : super(settings: settings);
17

18
  /// {@template flutter.widgets.pageRoute.fullscreenDialog}
19 20 21 22 23 24
  /// 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.
25
  /// {@endtemplate}
26 27
  final bool fullscreenDialog;

28
  @override
29
  bool get opaque => true;
30 31

  @override
32
  bool get barrierDismissible => false;
33 34

  @override
35
  bool canTransitionTo(TransitionRoute<dynamic> nextRoute) => nextRoute is PageRoute;
36 37

  @override
38
  bool canTransitionFrom(TransitionRoute<dynamic> previousRoute) => previousRoute is PageRoute;
39
}
40

41
Widget _defaultTransitionsBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
42 43 44 45 46 47 48 49
  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.
class PageRouteBuilder<T> extends PageRoute<T> {
50
  /// Creates a route that delegates to builder callbacks.
51
  ///
52
  /// The [pageBuilder], [transitionsBuilder], [opaque], [barrierDismissible],
53
  /// [maintainState], and [fullscreenDialog] arguments must not be null.
54
  PageRouteBuilder({
55 56
    RouteSettings? settings,
    required this.pageBuilder,
57 58
    this.transitionsBuilder = _defaultTransitionsBuilder,
    this.transitionDuration = const Duration(milliseconds: 300),
59
    this.reverseTransitionDuration = const Duration(milliseconds: 300),
60 61
    this.opaque = true,
    this.barrierDismissible = false,
62
    this.barrierColor,
63
    this.barrierLabel,
64
    this.maintainState = true,
65
    bool fullscreenDialog = false,
66 67
  }) : assert(pageBuilder != null),
       assert(transitionsBuilder != null),
68
       assert(opaque != null),
69 70
       assert(barrierDismissible != null),
       assert(maintainState != null),
71 72
       assert(fullscreenDialog != null),
       super(settings: settings, fullscreenDialog: fullscreenDialog);
73

74
  /// {@template flutter.widgets.pageRouteBuilder.pageBuilder}
75 76 77
  /// Used build the route's primary contents.
  ///
  /// See [ModalRoute.buildPage] for complete definition of the parameters.
78
  /// {@endtemplate}
79
  final RoutePageBuilder pageBuilder;
80

81
  /// {@template flutter.widgets.pageRouteBuilder.transitionsBuilder}
82 83 84
  /// Used to build the route's transitions.
  ///
  /// See [ModalRoute.buildTransitions] for complete definition of the parameters.
85
  /// {@endtemplate}
86 87 88 89 90
  final RouteTransitionsBuilder transitionsBuilder;

  @override
  final Duration transitionDuration;

91 92 93
  @override
  final Duration reverseTransitionDuration;

94 95 96 97
  @override
  final bool opaque;

  @override
98
  final bool barrierDismissible;
99 100

  @override
101
  final Color? barrierColor;
102

103
  @override
104
  final String? barrierLabel;
105

106 107 108 109
  @override
  final bool maintainState;

  @override
110 111
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return pageBuilder(context, animation, secondaryAnimation);
112 113 114
  }

  @override
115 116
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return transitionsBuilder(context, animation, secondaryAnimation, child);
117
  }
118
}