Commit a54c2905 authored by Matt Perry's avatar Matt Perry

Simplify Navigator's Transition code a bit.

parent c9284dff
...@@ -7,6 +7,7 @@ import 'dart:async'; ...@@ -7,6 +7,7 @@ import 'dart:async';
import 'package:sky/animation/animated_value.dart'; import 'package:sky/animation/animated_value.dart';
import 'package:sky/animation/animation_performance.dart'; import 'package:sky/animation/animation_performance.dart';
import 'package:sky/animation/curves.dart'; import 'package:sky/animation/curves.dart';
import 'package:sky/animation/forces.dart';
import 'package:sky/widgets/animated_component.dart'; import 'package:sky/widgets/animated_component.dart';
import 'package:sky/widgets/basic.dart'; import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/focus.dart'; import 'package:sky/widgets/focus.dart';
...@@ -65,7 +66,6 @@ class RouteState extends RouteBase { ...@@ -65,7 +66,6 @@ class RouteState extends RouteBase {
// and support multiple transition types // and support multiple transition types
const Duration _kTransitionDuration = const Duration(milliseconds: 150); const Duration _kTransitionDuration = const Duration(milliseconds: 150);
const Point _kTransitionStartPoint = const Point(0.0, 75.0); const Point _kTransitionStartPoint = const Point(0.0, 75.0);
enum TransitionDirection { forward, reverse }
class Transition extends AnimatedComponent { class Transition extends AnimatedComponent {
Transition({ Transition({
Key key, Key key,
...@@ -76,7 +76,7 @@ class Transition extends AnimatedComponent { ...@@ -76,7 +76,7 @@ class Transition extends AnimatedComponent {
this.interactive this.interactive
}): super(key: key); }): super(key: key);
Widget content; Widget content;
TransitionDirection direction; Direction direction;
bool interactive; bool interactive;
Function onDismissed; Function onDismissed;
Function onCompleted; Function onCompleted;
...@@ -91,61 +91,38 @@ class Transition extends AnimatedComponent { ...@@ -91,61 +91,38 @@ class Transition extends AnimatedComponent {
end: Point.origin, end: Point.origin,
curve: easeOut curve: easeOut
); );
_opacity = new AnimatedValue<double>(0.0, end: 1.0) _opacity = new AnimatedValue<double>(0.0, end: 1.0, curve: easeOut);
..curve = easeOut; _performance = new AnimationPerformance(duration: _kTransitionDuration)
_performance = new AnimationPerformance() ..variable = new AnimatedList([_position, _opacity]);
..duration = _kTransitionDuration if (direction == Direction.reverse)
..variable = new AnimatedList([_position, _opacity])
..addListener(_checkDismissed)
..addListener(_checkCompleted);
if (direction == TransitionDirection.reverse)
_performance.progress = 1.0; _performance.progress = 1.0;
_performance.addStatusListener(_checkStatusChanged);
watch(_performance); watch(_performance);
_start(); _start();
} }
void _start() { void _start() {
_dismissed = false; _performance.play(direction);
switch (direction) {
case TransitionDirection.forward:
_performance.play();
break;
case TransitionDirection.reverse:
_performance.reverse();
break;
}
} }
void syncFields(Transition source) { void syncFields(Transition source) {
content = source.content; content = source.content;
interactive = source.interactive;
onDismissed = source.onDismissed;
if (direction != source.direction) { if (direction != source.direction) {
direction = source.direction; direction = source.direction;
_start(); _start();
} }
interactive = source.interactive;
onDismissed = source.onDismissed;
super.syncFields(source); super.syncFields(source);
} }
bool _dismissed = false; void _checkStatusChanged(AnimationStatus status) {
void _checkDismissed() { if (_performance.isDismissed) {
if (!_dismissed &&
direction == TransitionDirection.reverse &&
_performance.isDismissed) {
if (onDismissed != null) if (onDismissed != null)
onDismissed(); onDismissed();
_dismissed = true; } else if (_performance.isCompleted) {
}
}
bool _completed = false;
void _checkCompleted() {
if (!_completed &&
direction == TransitionDirection.forward &&
_performance.isCompleted) {
if (onCompleted != null) if (onCompleted != null)
onCompleted(); onCompleted();
_completed = true;
} }
} }
...@@ -266,7 +243,7 @@ class Navigator extends StatefulComponent { ...@@ -266,7 +243,7 @@ class Navigator extends StatefulComponent {
Transition transition = new Transition( Transition transition = new Transition(
key: new Key.fromObjectIdentity(historyEntry), key: new Key.fromObjectIdentity(historyEntry),
content: content, content: content,
direction: (i <= state.historyIndex) ? TransitionDirection.forward : TransitionDirection.reverse, direction: (i <= state.historyIndex) ? Direction.forward : Direction.reverse,
interactive: (i == state.historyIndex), interactive: (i == state.historyIndex),
onDismissed: () { onDismissed: () {
setState(() { setState(() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment