pages.dart 4.26 KB
Newer Older
1 2 3
// 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.
4
import 'package:flutter/foundation.dart';
5

6
import 'basic.dart';
7
import 'framework.dart';
8 9 10 11 12
import 'navigator.dart';
import 'routes.dart';

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

19 20 21 22 23 24 25 26
  /// 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;

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

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

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

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

39
  @override
40
  AnimationController createAnimationController() {
41
    final AnimationController controller = super.createAnimationController();
42
    if (settings.isInitialRoute)
43 44
      controller.value = 1.0;
    return controller;
45
  }
46
}
47 48 49 50 51

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

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

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

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

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

  @override
  final Duration transitionDuration;

  @override
  final bool opaque;

  @override
107
  final bool barrierDismissible;
108 109 110 111

  @override
  final Color barrierColor;

112 113 114
  @override
  final String barrierLabel;

115 116 117 118
  @override
  final bool maintainState;

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

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

}