Added support for lerpDuration() (#64668)
This commit is contained in:
@@ -238,3 +238,10 @@ class Factory<T> {
|
||||
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(),
|
||||
);
|
||||
}
|
||||
57
packages/flutter/test/foundation/basic_types_test.dart
Normal file
57
packages/flutter/test/foundation/basic_types_test.dart
Normal file
@@ -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),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user