mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
This CL also refactors how animations work, particularly for the Drawer. I've
renamed DrawerAnimation to DrawerController and switched it from being an
Animation to having an Animation. I've also renamed Animation to AnimatedValue
to capture the idea that the class actually presents the value being animated.
Finally, I've factored AnimatedValueListener out of Drawer so that it can be
used by PopupMenuItem as well.
Finally, I've added a scheduleBuild convienence function to Component instead
of having to call setState(() {}), which has come up a couple times.
R=eseidel@chromium.org
Review URL: https://codereview.chromium.org/1016093002
83 lines
1.8 KiB
Dart
83 lines
1.8 KiB
Dart
// 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.
|
|
|
|
import '../fn.dart';
|
|
import 'curves.dart';
|
|
import 'dart:async';
|
|
import 'generators.dart';
|
|
|
|
class AnimatedValue {
|
|
StreamController _controller = new StreamController(sync: true);
|
|
AnimationGenerator _animation;
|
|
double _value;
|
|
|
|
AnimatedValue(double initial) {
|
|
value = initial;
|
|
}
|
|
|
|
Stream<double> get onValueChanged => _controller.stream;
|
|
|
|
double get value => _value;
|
|
|
|
void set value(double value) {
|
|
stop();
|
|
_setValue(value);
|
|
}
|
|
|
|
bool get isAnimating => _animation != null;
|
|
|
|
void _setValue(double value) {
|
|
_value = value;
|
|
_controller.add(_value);
|
|
}
|
|
|
|
void stop() {
|
|
if (_animation != null) {
|
|
_animation.cancel();
|
|
_animation = null;
|
|
}
|
|
}
|
|
|
|
void animateTo(double newValue, double duration,
|
|
{ Curve curve: linear, double initialDelay: 0.0 }) {
|
|
stop();
|
|
|
|
_animation = new AnimationGenerator(
|
|
duration: duration,
|
|
begin: _value,
|
|
end: newValue,
|
|
curve: curve,
|
|
initialDelay: initialDelay);
|
|
|
|
_animation.onTick.listen(_setValue, onDone: () {
|
|
_animation = null;
|
|
});
|
|
}
|
|
}
|
|
|
|
class AnimatedValueListener {
|
|
final Component _component;
|
|
final AnimatedValue _value;
|
|
StreamSubscription<double> _subscription;
|
|
|
|
AnimatedValueListener(this._component, this._value);
|
|
|
|
double get value => _value == null ? null : _value.value;
|
|
|
|
void ensureListening() {
|
|
if (_subscription != null || _value == null)
|
|
return;
|
|
_subscription = _value.onValueChanged.listen((_) {
|
|
_component.scheduleBuild();
|
|
});
|
|
}
|
|
|
|
void stopListening() {
|
|
if (_subscription == null)
|
|
return;
|
|
_subscription.cancel();
|
|
_subscription = null;
|
|
}
|
|
}
|