diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index 978fb4a2ce..09ce552cd8 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -508,6 +508,17 @@ class AnimationController extends Animation /// animation, when `target` is reached, [status] is reported as /// [AnimationStatus.completed]. TickerFuture animateTo(double target, { Duration? duration, Curve curve = Curves.linear }) { + assert(() { + if (this.duration == null && duration == null) { + throw FlutterError( + 'AnimationController.animateTo() called with no explicit duration and no default duration.\n' + 'Either the "duration" argument to the animateTo() method should be provided, or the ' + '"duration" property should be set, either in the constructor or later, before ' + 'calling the animateTo() function.' + ); + } + return true; + }()); assert( _ticker != null, 'AnimationController.animateTo() called after AnimationController.dispose()\n' @@ -530,6 +541,17 @@ class AnimationController extends Animation /// animation, when `target` is reached, [status] is reported as /// [AnimationStatus.dismissed]. TickerFuture animateBack(double target, { Duration? duration, Curve curve = Curves.linear }) { + assert(() { + if (this.duration == null && reverseDuration == null && duration == null) { + throw FlutterError( + 'AnimationController.animateBack() called with no explicit duration and no default duration or reverseDuration.\n' + 'Either the "duration" argument to the animateBack() method should be provided, or the ' + '"duration" or "reverseDuration" property should be set, either in the constructor or later, before ' + 'calling the animateBack() function.' + ); + } + return true; + }()); assert( _ticker != null, 'AnimationController.animateBack() called after AnimationController.dispose()\n' @@ -555,17 +577,8 @@ class AnimationController extends Animation } Duration? simulationDuration = duration; if (simulationDuration == null) { - assert(() { - if ((this.duration == null && _direction == _AnimationDirection.reverse && reverseDuration == null) || this.duration == null) { - throw FlutterError( - 'AnimationController.animateTo() called with no explicit duration and no default duration or reverseDuration.\n' - 'Either the "duration" argument to the animateTo() method should be provided, or the ' - '"duration" and/or "reverseDuration" property should be set, either in the constructor or later, before ' - 'calling the animateTo() function.' - ); - } - return true; - }()); + assert(!(this.duration == null && _direction == _AnimationDirection.forward)); + assert(!(this.duration == null && _direction == _AnimationDirection.reverse && reverseDuration == null)); final double range = upperBound - lowerBound; final double remainingFraction = range.isFinite ? (target - _value).abs() / range : 1.0; final Duration directionDuration = diff --git a/packages/flutter/test/animation/animation_controller_test.dart b/packages/flutter/test/animation/animation_controller_test.dart index 29bdbec3a8..ccff8027e0 100644 --- a/packages/flutter/test/animation/animation_controller_test.dart +++ b/packages/flutter/test/animation/animation_controller_test.dart @@ -929,6 +929,135 @@ void main() { tick(const Duration(seconds: 1)); expect(statuses, [AnimationStatus.reverse]); }); + + test('AnimateBack can runs successfully with just "reverseDuration" property set', () { + final List statuses = []; + final AnimationController controller = AnimationController( + reverseDuration: const Duration(seconds: 2), + vsync: const TestVSync(), + )..addStatusListener((AnimationStatus status) { + statuses.add(status); + }); + + controller.animateBack(0.8); + + expect(statuses, [AnimationStatus.reverse]); + statuses.clear(); + tick(const Duration(milliseconds: 0)); + tick(const Duration(seconds: 2)); + expect(statuses, [AnimationStatus.dismissed]); + + controller.dispose(); + }); + + group('AnimationController "duration" error test', () { + test('AnimationController forward() will throw an error if there is no default duration', () { + final AnimationController controller = AnimationController( + vsync: const TestVSync(), + ); + + late FlutterError error; + try { + controller.forward(); + } on FlutterError catch (e) { + error = e; + } + + expect(error, isNotNull); + expect( + error.toStringDeep(), + 'FlutterError\n' + ' AnimationController.forward() called with no default duration.\n' + ' The "duration" property should be set, either in the constructor\n' + ' or later, before calling the forward() function.\n' + ); + + controller.dispose(); + }); + + test('AnimationController animateTo() will throw an error if there is no explicit duration ' + 'and default duration', () { + final AnimationController controller = AnimationController( + vsync: const TestVSync(), + ); + + late FlutterError error; + try { + controller.animateTo(0.8); + } on FlutterError catch (e) { + error = e; + } + + expect(error, isNotNull); + expect( + error.toStringDeep(), + 'FlutterError\n' + ' AnimationController.animateTo() called with no explicit duration\n' + ' and no default duration.\n' + ' Either the "duration" argument to the animateTo() method should\n' + ' be provided, or the "duration" property should be set, either in\n' + ' the constructor or later, before calling the animateTo()\n' + ' function.\n' + ); + + controller.dispose(); + }); + + test('AnimationController reverse() will throw an error if there is no default duration or reverseDuration', () { + final AnimationController controller = AnimationController( + vsync: const TestVSync(), + ); + + late FlutterError error; + try { + controller.reverse(); + } on FlutterError catch (e) { + error = e; + } + + expect(error, isNotNull); + expect( + error.toStringDeep(), + 'FlutterError\n' + ' AnimationController.reverse() called with no default duration or\n' + ' reverseDuration.\n' + ' The "duration" or "reverseDuration" property should be set,\n' + ' either in the constructor or later, before calling the reverse()\n' + ' function.\n' + ); + + controller.dispose(); + }); + + test('AnimationController animateBack() will throw an error if there is no explicit duration and ' + 'no default duration or reverseDuration', () { + final AnimationController controller = AnimationController( + vsync: const TestVSync(), + ); + + late FlutterError error; + try { + controller.animateBack(0.8); + } on FlutterError catch (e) { + error = e; + } + + expect(error, isNotNull); + expect( + error.toStringDeep(), + 'FlutterError\n' + ' AnimationController.animateBack() called with no explicit\n' + ' duration and no default duration or reverseDuration.\n' + ' Either the "duration" argument to the animateBack() method should\n' + ' be provided, or the "duration" or "reverseDuration" property\n' + ' should be set, either in the constructor or later, before calling\n' + ' the animateBack() function.\n' + ); + + controller.dispose(); + }); + }); + } class TestSimulation extends Simulation {