pages.dart 4.17 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 48 49 50

/// Signature for the [PageRouteBuilder] function that builds the route's
/// primary contents.
///
/// See [ModalRoute.buildPage] for complete definition of the parameters.
51
typedef Widget RoutePageBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation);
52 53 54 55 56

/// Signature for the [PageRouteBuilder] function that builds the route's
/// transitions.
///
/// See [ModalRoute.buildTransitions] for complete definition of the parameters.
57
typedef Widget RouteTransitionsBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child);
58

59
Widget _defaultTransitionsBuilder(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
60 61 62 63 64 65 66 67
  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> {
68
  /// Creates a route that delegates to builder callbacks.
69
  ///
70
  /// The [pageBuilder], [transitionsBuilder], [opaque], [barrierDismissible],
71
  /// and [maintainState] arguments must not be null.
72
  PageRouteBuilder({
73
    RouteSettings settings,
74
    @required this.pageBuilder,
75 76 77
    this.transitionsBuilder: _defaultTransitionsBuilder,
    this.transitionDuration: const Duration(milliseconds: 300),
    this.opaque: true,
78
    this.barrierDismissible: false,
79
    this.barrierColor,
80
    this.barrierLabel,
81
    this.maintainState: true,
82 83 84 85
  }) : assert(pageBuilder != null),
       assert(transitionsBuilder != null),
       assert(barrierDismissible != null),
       assert(maintainState != null),
86 87
       assert(opaque != null),
       super(settings: settings);
88

89 90 91
  /// Used build the route's primary contents.
  ///
  /// See [ModalRoute.buildPage] for complete definition of the parameters.
92
  final RoutePageBuilder pageBuilder;
93 94 95 96

  /// Used to build the route's transitions.
  ///
  /// See [ModalRoute.buildTransitions] for complete definition of the parameters.
97 98 99 100 101 102 103 104 105
  final RouteTransitionsBuilder transitionsBuilder;

  @override
  final Duration transitionDuration;

  @override
  final bool opaque;

  @override
106
  final bool barrierDismissible;
107 108 109 110

  @override
  final Color barrierColor;

111 112 113
  @override
  final String barrierLabel;

114 115 116 117
  @override
  final bool maintainState;

  @override
118 119
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return pageBuilder(context, animation, secondaryAnimation);
120 121 122
  }

  @override
123 124
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return transitionsBuilder(context, animation, secondaryAnimation, child);
125 126 127
  }

}