Fix TextStyle.lerp() to properly interpolate text shadows (#145666)

Currently, `TextStyle.lerp()` does not properly interpolate the shadows property between two TextStyle instances. This PR addresses the issue by using ui.Shadow.lerpList() to interpolate the shadow lists, ensuring that the individual shadow properties (blur radius, color, and offset) are correctly lerped.

The changes include:
- Updating the `TextStyle.lerp()` method to use `ui.Shadow.lerpList()` for the shadows property.
- Adding a new test case in `text_style_test.dart` to verify the correct interpolation of text shadows.

Here is a snippet showing the comparison, when using `ui.Shadow.lerpList()`
https://dartpad.dev/?id=799c4bec8b791b74580193174759b93d

Closes #145665
This commit is contained in:
Leo Farias
2024-04-03 10:49:58 -07:00
committed by GitHub
parent a81729c68e
commit 2759ec2563
2 changed files with 24 additions and 2 deletions

View File

@@ -5,6 +5,7 @@
import 'dart:collection';
import 'dart:ui' as ui show
ParagraphStyle,
Shadow,
StrutStyle,
TextStyle,
lerpDouble;
@@ -1277,7 +1278,7 @@ class TextStyle with Diagnosticable {
? a.background ?? (Paint()..color = a.backgroundColor!)
: b.background ?? (Paint()..color = b.backgroundColor!)
: null,
shadows: t < 0.5 ? a.shadows : b.shadows,
shadows: ui.Shadow.lerpList(a.shadows, b.shadows, t),
fontFeatures: t < 0.5 ? a.fontFeatures : b.fontFeatures,
fontVariations: lerpFontVariations(a.fontVariations, b.fontVariations, t),
decoration: t < 0.5 ? a.decoration : b.decoration,

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui show FontFeature, FontVariation, ParagraphStyle, Shadow, TextStyle;
import 'dart:ui' as ui show FontFeature, FontVariation, ParagraphStyle, Shadow, TextStyle, lerpDouble;
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -409,6 +409,27 @@ void main() {
expect(c.hashCode, isNot(d.hashCode));
});
test('TextStyle shadows', () {
const ui.Shadow shadow1 = ui.Shadow(blurRadius: 1.0, offset: Offset(1.0, 1.0));
const ui.Shadow shadow2 = ui.Shadow(blurRadius: 2.0, color: Color(0xFF111111), offset: Offset(2.0, 2.0));
const ui.Shadow shadow3 = ui.Shadow(blurRadius: 3.0, color: Color(0xFF222222), offset: Offset(3.0, 3.0));
const ui.Shadow shadow4 = ui.Shadow(blurRadius: 4.0, color: Color(0xFF333333), offset: Offset(4.0, 4.0));
const TextStyle s1 = TextStyle(shadows: <ui.Shadow>[shadow1, shadow2]);
const TextStyle s2 = TextStyle(shadows: <ui.Shadow>[shadow3, shadow4]);
final TextStyle lerp12 = TextStyle.lerp(s1, s2, 0.5)!;
expect(lerp12.shadows, hasLength(2));
expect(lerp12.shadows?[0].blurRadius, ui.lerpDouble(shadow1.blurRadius, shadow3.blurRadius, 0.5));
expect(lerp12.shadows?[0].color, Color.lerp(shadow1.color, shadow3.color, 0.5));
expect(lerp12.shadows?[0].offset, Offset.lerp(shadow1.offset, shadow3.offset, 0.5));
expect(lerp12.shadows?[1].blurRadius, ui.lerpDouble(shadow2.blurRadius, shadow4.blurRadius, 0.5));
expect(lerp12.shadows?[1].color, Color.lerp(shadow2.color, shadow4.color, 0.5));
expect(lerp12.shadows?[1].offset, Offset.lerp(shadow2.offset, shadow4.offset, 0.5));
});
test('TextStyle foreground and color combos', () {
const Color red = Color.fromARGB(255, 255, 0, 0);
const Color blue = Color.fromARGB(255, 0, 0, 255);