diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index d198d2b19c..87bb557163 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -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)]; } diff --git a/engine/src/flutter/testing/dart/text_test.dart b/engine/src/flutter/testing/dart/text_test.dart new file mode 100644 index 0000000000..fb8b13bbbe --- /dev/null +++ b/engine/src/flutter/testing/dart/text_test.dart @@ -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)); + }); + }); +}