Unverified Commit 57b69524 authored by Matt Carroll's avatar Matt Carroll Committed by GitHub

Added support for lerpDuration() (#64668)

parent fbc15384
......@@ -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(),
);
}
\ No newline at end of file
// 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment