[ui] Add null check in FontWeight.lerp (flutter/engine#8274)

* Add null check in FontWeight.lerp

* Add Unit tests for FontWeight.lerp
This commit is contained in:
MH Johnson
2019-04-01 21:56:19 -04:00
committed by GitHub
parent 8bd246441e
commit dc2da3460f
2 changed files with 31 additions and 1 deletions

View File

@@ -63,7 +63,8 @@ class FontWeight {
/// Rather than using fractional weights, the interpolation rounds to the
/// nearest weight.
///
/// Any null values for `a` or `b` are interpreted as equivalent to [normal]
/// If both `a` and `b` are null, then this method will return null. Otherwise,
/// any null values for `a` or `b` are interpreted as equivalent to [normal]
/// (also known as [w400]).
///
/// The `t` argument represents position on the timeline, with 0.0 meaning
@@ -80,6 +81,8 @@ class FontWeight {
/// an [AnimationController].
static FontWeight lerp(FontWeight a, FontWeight b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
return values[lerpDouble(a?.index ?? normal.index, b?.index ?? normal.index, t).round().clamp(0, 8)];
}

View File

@@ -0,0 +1,27 @@
// Copyright 2019 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 'dart:ui';
import 'package:test/test.dart';
void main() {
group('FontWeight.lerp', () {
test('works with non-null values', () {
expect(FontWeight.lerp(FontWeight.w400, FontWeight.w600, .5), equals(FontWeight.w500));
});
test('returns null if a and b are null', () {
expect(FontWeight.lerp(null, null, 0), isNull);
});
test('returns FontWeight.w400 if a is null', () {
expect(FontWeight.lerp(null, FontWeight.w400, 0), equals(FontWeight.w400));
});
test('returns FontWeight.w400 if b is null', () {
expect(FontWeight.lerp(FontWeight.w400, null, 1), equals(FontWeight.w400));
});
});
}