Added support for lerpDuration() (#64668)
diff --git a/packages/flutter/lib/src/foundation/basic_types.dart b/packages/flutter/lib/src/foundation/basic_types.dart
index fc57ec8..8358b7b 100644
--- a/packages/flutter/lib/src/foundation/basic_types.dart
+++ b/packages/flutter/lib/src/foundation/basic_types.dart
@@ -238,3 +238,10 @@
return 'Factory(type: $type)';
}
}
+
+/// Linearly interpolate between two `Duration`s.
+Duration lerpDuration(Duration a, Duration b, double t) {
+ return Duration(
+ microseconds: (a.inMicroseconds + (b.inMicroseconds - a.inMicroseconds) * t).round(),
+ );
+}
\ No newline at end of file
diff --git a/packages/flutter/test/foundation/basic_types_test.dart b/packages/flutter/test/foundation/basic_types_test.dart
new file mode 100644
index 0000000..79efc73
--- /dev/null
+++ b/packages/flutter/test/foundation/basic_types_test.dart
@@ -0,0 +1,57 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/src/foundation/basic_types.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ group('lerp Duration', () {
+ test('linearly interpolates between positive Durations', () {
+ expect(
+ lerpDuration(const Duration(seconds: 1), const Duration(seconds: 2), 0.5),
+ const Duration(milliseconds: 1500),
+ );
+ });
+
+ test('linearly interpolates between negative Durations', () {
+ expect(
+ lerpDuration(const Duration(seconds: -1), const Duration(seconds: -2), 0.5),
+ const Duration(milliseconds: -1500),
+ );
+ });
+
+ test('linearly interpolates between positive and negative Durations', () {
+ expect(
+ lerpDuration(const Duration(seconds: -1), const Duration(seconds:2), 0.5),
+ const Duration(milliseconds: 500),
+ );
+ });
+
+ test('starts at first Duration', () {
+ expect(
+ lerpDuration(const Duration(seconds: 1), const Duration(seconds: 2), 0),
+ const Duration(seconds: 1),
+ );
+ });
+
+ test('ends at second Duration', () {
+ expect(
+ lerpDuration(const Duration(seconds: 1), const Duration(seconds: 2), 1),
+ const Duration(seconds: 2),
+ );
+ });
+
+ test('time values beyond 1.0 have a multiplier effect', () {
+ expect(
+ lerpDuration(const Duration(seconds: 1), const Duration(seconds: 2), 5),
+ const Duration(seconds: 6),
+ );
+
+ expect(
+ lerpDuration(const Duration(seconds: -1), const Duration(seconds: -2), 5),
+ const Duration(seconds: -6),
+ );
+ });
+ });
+}
\ No newline at end of file