diff --git a/packages/flutter/lib/src/animation/curves.dart b/packages/flutter/lib/src/animation/curves.dart index 3613736e01eca..1b8dc5dcf3128 100644 --- a/packages/flutter/lib/src/animation/curves.dart +++ b/packages/flutter/lib/src/animation/curves.dart @@ -401,6 +401,15 @@ class Cubic extends Curve { @override double transformInternal(double t) { + if (t.isNaN) { + throw ArgumentError.value(t, 't', 'must not be NaN'); + } + if (t <= 0.0) { + return 0.0; + } + if (t >= 1.0) { + return 1.0; + } var start = 0.0; var end = 1.0; while (true) { diff --git a/packages/flutter/test/animation/curves_test.dart b/packages/flutter/test/animation/curves_test.dart index 92a7011c4a6af..e83e1d43235d0 100644 --- a/packages/flutter/test/animation/curves_test.dart +++ b/packages/flutter/test/animation/curves_test.dart @@ -180,6 +180,16 @@ void main() { expect(test.transform(0.166666), equals(0.4)); }); + // Regression test for https://github.com/flutter/flutter/issues/171364. + test('Cubic transformInternal clamps values outside the unit interval', () { + expect(Curves.easeOut.transformInternal(2.0), 1.0); + expect(Curves.easeOut.transformInternal(-1.0), 0.0); + }); + + test('Cubic transformInternal throws for NaN', () { + expect(() => Curves.easeOut.transformInternal(double.nan), throwsArgumentError); + }); + test('Invalid transform parameter should assert', () { expect(() => const SawTooth(2).transform(-0.0001), throwsAssertionError); expect(() => const SawTooth(2).transform(1.0001), throwsAssertionError);