| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 import 'curves.dart'; | |
| 6 import 'dart:async'; | |
| 7 import 'generators.dart'; | |
| 8 | |
| 9 typedef void Callback (); | |
| 10 | |
| 11 class AnimatedValue { | |
| 12 StreamController _controller = new StreamController.broadcast(sync: true); | |
| 13 AnimationGenerator _animation; | |
| 14 Completer _completer; | |
| 15 double _value; | |
| 16 | |
| 17 AnimatedValue(double initial, { Callback onChange }) { | |
| 18 _value = initial; | |
| 19 _onChange = onChange; | |
| 20 } | |
| 21 Callback _onChange; | |
| 22 | |
| 23 // A stream of change in value from |initial|. The stream does not | |
| 24 // contain the initial value. Consumers should check the initial value via | |
| 25 // the |value| accessor. | |
| 26 Stream<double> get onValueChanged => _controller.stream; | |
| 27 | |
| 28 double get value => _value; | |
| 29 | |
| 30 void set value(double value) { | |
| 31 stop(); | |
| 32 _setValue(value); | |
| 33 } | |
| 34 | |
| 35 bool get isAnimating => _animation != null; | |
| 36 | |
| 37 void _setValue(double value) { | |
| 38 _value = value; | |
| 39 _controller.add(_value); | |
| 40 if (_onChange != null) | |
| 41 _onChange(); | |
| 42 } | |
| 43 | |
| 44 void _done() { | |
| 45 _animation = null; | |
| 46 if (_completer == null) | |
| 47 return; | |
| 48 Completer completer = _completer; | |
| 49 _completer = null; | |
| 50 completer.complete(_value); | |
| 51 } | |
| 52 | |
| 53 void stop() { | |
| 54 if (_animation != null) { | |
| 55 _animation.cancel(); // will call _done() if it isn't already finished | |
| 56 _done(); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 Future<double> animateTo(double newValue, double duration, | |
| 61 { Curve curve: linear, double initialDelay: 0.0 }) { | |
| 62 stop(); | |
| 63 | |
| 64 _animation = new AnimationGenerator( | |
| 65 duration: duration, | |
| 66 begin: _value, | |
| 67 end: newValue, | |
| 68 curve: curve, | |
| 69 initialDelay: initialDelay) | |
| 70 ..onTick.listen(_setValue, onDone: _done); | |
| 71 | |
| 72 _completer = new Completer(); | |
| 73 return _completer.future; | |
| 74 } | |
| 75 | |
| 76 double get remainingTime { | |
| 77 if (_animation == null) | |
| 78 return 0.0; | |
| 79 return _animation.remainingTime; | |
| 80 } | |
| 81 | |
| 82 } | |
| OLD | NEW |