From 2759ec256380a0f1d2500948c81c28f806f3cb81 Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Wed, 3 Apr 2024 10:49:58 -0700 Subject: [PATCH] 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 --- .../flutter/lib/src/painting/text_style.dart | 3 ++- .../test/painting/text_style_test.dart | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_style.dart b/packages/flutter/lib/src/painting/text_style.dart index ab41a25b8e..24de4a5bb4 100644 --- a/packages/flutter/lib/src/painting/text_style.dart +++ b/packages/flutter/lib/src/painting/text_style.dart @@ -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, diff --git a/packages/flutter/test/painting/text_style_test.dart b/packages/flutter/test/painting/text_style_test.dart index 0af6f3b3d0..7ab2241088 100644 --- a/packages/flutter/test/painting/text_style_test.dart +++ b/packages/flutter/test/painting/text_style_test.dart @@ -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: [shadow1, shadow2]); + const TextStyle s2 = TextStyle(shadows: [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);