pages.dart 3.58 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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
  @override
39
  AnimationController createAnimationController() {
40
    final AnimationController controller = super.createAnimationController();
41
    if (settings.isInitialRoute)
42 43
      controller.value = 1.0;
    return controller;
44
  }
45
}
46

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

77 78 79
  /// Used build the route's primary contents.
  ///
  /// See [ModalRoute.buildPage] for complete definition of the parameters.
80
  final RoutePageBuilder pageBuilder;
81 82 83 84

  /// Used to build the route's transitions.
  ///
  /// See [ModalRoute.buildTransitions] for complete definition of the parameters.
85 86 87 88 89 90 91 92 93
  final RouteTransitionsBuilder transitionsBuilder;

  @override
  final Duration transitionDuration;

  @override
  final bool opaque;

  @override
94
  final bool barrierDismissible;
95 96 97 98

  @override
  final Color barrierColor;

99 100 101
  @override
  final String barrierLabel;

102 103 104 105
  @override
  final bool maintainState;

  @override
106 107
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return pageBuilder(context, animation, secondaryAnimation);
108 109 110
  }

  @override
111 112
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return transitionsBuilder(context, animation, secondaryAnimation, child);
113 114 115
  }

}