Fix the duration assertion error for the _animateToInternal method and refine the error description (#71137)

This commit is contained in:
houyushan 2020-12-09 03:53:03 +08:00 committed by GitHub
parent ee66a50162
commit 4217ce443b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 153 additions and 11 deletions

View file

@ -508,6 +508,17 @@ class AnimationController extends Animation<double>
/// 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<double>
/// 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<double>
}
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 =

View file

@ -929,6 +929,135 @@ void main() {
tick(const Duration(seconds: 1));
expect(statuses, <AnimationStatus>[AnimationStatus.reverse]);
});
test('AnimateBack can runs successfully with just "reverseDuration" property set', () {
final List<AnimationStatus> statuses = <AnimationStatus>[];
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>[AnimationStatus.reverse]);
statuses.clear();
tick(const Duration(milliseconds: 0));
tick(const Duration(seconds: 2));
expect(statuses, <AnimationStatus>[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 {