page.dart 2.42 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 6
import 'dart:async';

7 8
import 'package:flutter/widgets.dart';

9
class _MaterialPageTransition extends AnimatedWidget {
10 11
  _MaterialPageTransition({
    Key key,
12
    Animation<double> animation,
13 14 15 16 17
    this.child
  }) : super(
    key: key,
    animation: new CurvedAnimation(parent: animation, curve: Curves.easeOut)
  );
18

19
  final Widget child;
20

21 22 23 24
  final Tween<Point> _position = new Tween<Point>(
    begin: const Point(0.0, 75.0),
    end: Point.origin
  );
25

26
  @override
27 28
  Widget build(BuildContext context) {
    Point position = _position.evaluate(animation);
29
    Matrix4 transform = new Matrix4.identity()
30
      ..translate(position.x, position.y);
31 32 33 34
    return new Transform(
      transform: transform,
      // TODO(ianh): tell the transform to be un-transformed for hit testing
      child: new Opacity(
35
        opacity: animation.value,
36 37 38 39 40 41
        child: child
      )
    );
  }
}

Hixie's avatar
Hixie committed
42 43
const Duration kMaterialPageRouteTransitionDuration = const Duration(milliseconds: 150);

Hixie's avatar
Hixie committed
44
class MaterialPageRoute<T> extends PageRoute<T> {
45 46
  MaterialPageRoute({
    this.builder,
47
    Completer<T> completer,
48
    RouteSettings settings: const RouteSettings()
49
  }) : super(completer: completer, settings: settings) {
50 51 52 53 54 55
    assert(builder != null);
    assert(opaque);
  }

  final WidgetBuilder builder;

56
  @override
Hixie's avatar
Hixie committed
57
  Duration get transitionDuration => kMaterialPageRouteTransitionDuration;
58 59

  @override
60
  Color get barrierColor => null;
61 62

  @override
63
  bool canTransitionFrom(TransitionRoute<dynamic> nextRoute) => false;
64

65
  @override
66
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation) {
67 68 69 70 71 72 73 74 75 76
    Widget result = builder(context);
    assert(() {
      if (result == null)
        debugPrint('The builder for route \'${settings.name}\' returned null. Route builders must never return null.');
      assert(result != null && 'A route builder returned null. See the previous log message for details.' is String);
      return true;
    });
    return result;
  }

77
  @override
78
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation, Widget child) {
79
    return new _MaterialPageTransition(
80
      animation: animation,
81 82 83 84
      child: child
    );
  }

85
  @override
86 87
  String get debugLabel => '${super.debugLabel}(${settings.name})';
}