Expose foreground property of TextStyle in Icon widget (#150315)

Expose foreground property to icon. See issues:

- #139411
This commit is contained in:
Lu Shueh Chou
2024-09-10 01:30:19 +08:00
committed by GitHub
parent d8ae99a790
commit 4aecec70a9
2 changed files with 41 additions and 6 deletions

View File

@@ -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: <FontVariation>[
@@ -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(

View File

@@ -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);