pages.dart 3.48 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 19 20 21 22 23 24 25
  /// 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.
  final bool fullscreenDialog;

26
  @override
27
  bool get opaque => true;
28 29

  @override
30
  bool get barrierDismissible => false;
31 32

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

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

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

71 72 73
  /// Used build the route's primary contents.
  ///
  /// See [ModalRoute.buildPage] for complete definition of the parameters.
74
  final RoutePageBuilder pageBuilder;
75 76 77 78

  /// Used to build the route's transitions.
  ///
  /// See [ModalRoute.buildTransitions] for complete definition of the parameters.
79 80 81 82 83 84 85 86 87
  final RouteTransitionsBuilder transitionsBuilder;

  @override
  final Duration transitionDuration;

  @override
  final bool opaque;

  @override
88
  final bool barrierDismissible;
89 90 91 92

  @override
  final Color barrierColor;

93 94 95
  @override
  final String barrierLabel;

96 97 98 99
  @override
  final bool maintainState;

  @override
100 101
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return pageBuilder(context, animation, secondaryAnimation);
102 103 104
  }

  @override
105 106
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return transitionsBuilder(context, animation, secondaryAnimation, child);
107 108 109
  }

}