diff --git a/packages/flutter/lib/src/widgets/icon.dart b/packages/flutter/lib/src/widgets/icon.dart index c895ff2dc1..9d47b714f7 100644 --- a/packages/flutter/lib/src/widgets/icon.dart +++ b/packages/flutter/lib/src/widgets/icon.dart @@ -86,9 +86,10 @@ class Icon extends StatelessWidget { this.semanticLabel, this.textDirection, this.applyTextScaling, - }) : assert(fill == null || (0.0 <= fill && fill <= 1.0)), - assert(weight == null || (0.0 < weight)), - assert(opticalSize == null || (0.0 < opticalSize)); + this.blendMode, + }) : assert(fill == null || (0.0 <= fill && fill <= 1.0)), + assert(weight == null || (0.0 < weight)), + assert(opticalSize == null || (0.0 < opticalSize)); /// The icon to display. The available icons are described in [Icons]. /// @@ -247,6 +248,11 @@ class Icon extends StatelessWidget { /// [IconThemeData.applyTextScaling]. final bool? applyTextScaling; + /// The [BlendMode] to apply to the foreground of the icon. + /// + /// Defaults to [BlendMode.srcOver] + final BlendMode? blendMode; + @override Widget build(BuildContext context) { assert(this.textDirection != null || debugCheckHasDirectionality(context)); @@ -279,10 +285,18 @@ class Icon extends StatelessWidget { } final double iconOpacity = iconTheme.opacity ?? 1.0; - Color iconColor = color ?? iconTheme.color!; + Color? iconColor = color ?? iconTheme.color!; + Paint? foreground; if (iconOpacity != 1.0) { iconColor = iconColor.withOpacity(iconColor.opacity * iconOpacity); } + if (blendMode != null) { + foreground = Paint() + ..blendMode = blendMode! + ..color = iconColor; + // Cannot provide both a color and a foreground. + iconColor = null; + } final TextStyle fontStyle = TextStyle( fontVariations: [ @@ -298,8 +312,9 @@ class Icon extends StatelessWidget { package: icon.fontPackage, fontFamilyFallback: icon.fontFamilyFallback, shadows: iconShadows, - height: 1.0, // Makes sure the font's body is vertically centered within the iconSize x iconSize square. + height: 1.0, // Makes sure the font's body is vertically centered within the iconSize x iconSize square. leadingDistribution: TextLeadingDistribution.even, + foreground: foreground, ); Widget iconWidget = RichText( diff --git a/packages/flutter/test/widgets/icon_test.dart b/packages/flutter/test/widgets/icon_test.dart index 79f77e3365..7e00dcb786 100644 --- a/packages/flutter/test/widgets/icon_test.dart +++ b/packages/flutter/test/widgets/icon_test.dart @@ -358,7 +358,6 @@ void main() { expect(const IconData(123).toString(), 'IconData(U+0007B)'); }); - testWidgets('Fill, weight, grade, and optical size variations are passed', (WidgetTester tester) async { await tester.pumpWidget( const Directionality( @@ -443,6 +442,27 @@ void main() { ]); }); + testWidgets("TextStyle's blendMode should bind in foreground property", (WidgetTester tester) async { + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: IconTheme( + data: IconThemeData(opacity: 0.5), + child: Icon( + IconData(0x41), + blendMode: BlendMode.clear, + ), + ), + ), + ), + ); + + final RichText richText = tester.firstWidget(find.byType(RichText)); + expect(richText.text.style?.color, isNull); + expect(richText.text.style?.foreground?.blendMode, BlendMode.clear); + }); + test('Throws if given invalid values', () { expect(() => Icon(Icons.abc, fill: -0.1), throwsAssertionError); expect(() => Icon(Icons.abc, fill: 1.1), throwsAssertionError);