Skip to content

Commit

Permalink
AnimationController reset() method (flutter#13044)
Browse files Browse the repository at this point in the history
* AnimationController reset() method

Just a simple convenience method to fix flutter#13039

* Added `reset()` test

* More test expectations

Per feedback.

* Removed test print

* Improved documentation of reset()

* Add controller.reverse to test
  • Loading branch information
Kyle Bradshaw authored and Hixie committed Nov 22, 2017
1 parent c7c3b60 commit 3dc3287
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/flutter/lib/src/animation/animation_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ class AnimationController extends Animation<double>
notifyListeners();
_checkStatusChanged();
}

/// Sets the controller's value to [lowerBound], stopping the animation (if
/// in progress), and resetting to its beginning point, or dismissed state.
void reset() {
value = lowerBound;
}

/// The rate of change of [value] per second.
///
Expand Down
53 changes: 53 additions & 0 deletions packages/flutter/test/animation/animation_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,59 @@ void main() {
expect(controller.value, 1.0);
});

test('resetting animation works at all phases', (){
final List<AnimationStatus> statusLog = <AnimationStatus>[];
final AnimationController controller = new AnimationController(
duration: const Duration(milliseconds: 100),
value: 0.0,
lowerBound: 0.0,
upperBound: 1.0,
vsync: const TestVSync(),
)..addStatusListener(statusLog.add);

expect(controller.value, 0.0);
expect(controller.status, AnimationStatus.dismissed);

controller.reset();

expect(controller.value, 0.0);
expect(controller.status, AnimationStatus.dismissed);

statusLog.clear();
controller.forward();
tick(const Duration(milliseconds: 0));
tick(const Duration(milliseconds: 50));
expect(controller.status, AnimationStatus.forward);
controller.reset();

expect(controller.value, 0.0);
expect(controller.status, AnimationStatus.dismissed);
expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.forward, AnimationStatus.dismissed ]));

controller.value = 1.0;
statusLog.clear();
controller.reverse();
tick(const Duration(milliseconds: 0));
tick(const Duration(milliseconds: 50));
expect(controller.status, AnimationStatus.reverse);
controller.reset();

expect(controller.value, 0.0);
expect(controller.status, AnimationStatus.dismissed);
expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.reverse, AnimationStatus.dismissed ]));

statusLog.clear();
controller.forward();
tick(const Duration(milliseconds: 0));
tick(const Duration(milliseconds: 150));
expect(controller.status, AnimationStatus.completed);
controller.reset();

expect(controller.value, 0.0);
expect(controller.status, AnimationStatus.dismissed);
expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.forward, AnimationStatus.completed, AnimationStatus.dismissed ]));
});

test('setting value directly sets correct status', () {
final AnimationController controller = new AnimationController(
value: 0.0,
Expand Down

0 comments on commit 3dc3287

Please sign in to comment.