diff --git a/packages/flutter/lib/src/widgets/icon.dart b/packages/flutter/lib/src/widgets/icon.dart index 36c409d947..3868c6e18b 100644 --- a/packages/flutter/lib/src/widgets/icon.dart +++ b/packages/flutter/lib/src/widgets/icon.dart @@ -240,6 +240,14 @@ class Icon extends StatelessWidget { final double? iconSize = size ?? iconTheme.size; + final double? iconFill = fill ?? iconTheme.fill; + + final double? iconWeight = weight ?? iconTheme.weight; + + final double? iconGrade = grade ?? iconTheme.grade; + + final double? iconOpticalSize = opticalSize ?? iconTheme.opticalSize; + final List? iconShadows = shadows ?? iconTheme.shadows; if (icon == null) { @@ -262,10 +270,10 @@ class Icon extends StatelessWidget { text: String.fromCharCode(icon!.codePoint), style: TextStyle( fontVariations: [ - if (fill != null) FontVariation('FILL', fill!), - if (weight != null) FontVariation('wght', weight!), - if (grade != null) FontVariation('GRAD', grade!), - if (opticalSize != null) FontVariation('opsz', opticalSize!), + if (iconFill != null) FontVariation('FILL', iconFill), + if (iconWeight != null) FontVariation('wght', iconWeight), + if (iconGrade != null) FontVariation('GRAD', iconGrade), + if (iconOpticalSize != null) FontVariation('opsz', iconOpticalSize), ], inherit: false, color: iconColor, diff --git a/packages/flutter/test/widgets/icon_test.dart b/packages/flutter/test/widgets/icon_test.dart index 347409be9a..1e59741044 100644 --- a/packages/flutter/test/widgets/icon_test.dart +++ b/packages/flutter/test/widgets/icon_test.dart @@ -220,7 +220,12 @@ void main() { ); RichText text = tester.widget(find.byType(RichText)); - expect(text.text.style!.fontVariations, []); + expect(text.text.style!.fontVariations, [ + const FontVariation('FILL', 0.0), + const FontVariation('wght', 400.0), + const FontVariation('GRAD', 0.0), + const FontVariation('opsz', 48.0) + ]); await tester.pumpWidget( const Directionality( @@ -239,6 +244,57 @@ void main() { ]); }); + testWidgets('Fill, weight, grade, and optical size can be set at the theme-level', (WidgetTester tester) async { + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: IconTheme( + data: IconThemeData( + fill: 0.2, + weight: 3.0, + grade: 4.0, + opticalSize: 5.0, + ), + child: Icon(Icons.abc), + ), + ), + ); + + final RichText text = tester.widget(find.byType(RichText)); + expect(text.text.style!.fontVariations, [ + const FontVariation('FILL', 0.2), + const FontVariation('wght', 3.0), + const FontVariation('GRAD', 4.0), + const FontVariation('opsz', 5.0) + ]); + }); + + testWidgets('Theme-level fill, weight, grade, and optical size can be overriden', (WidgetTester tester) async { + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: IconTheme( + data: IconThemeData( + fill: 0.2, + weight: 3.0, + grade: 4.0, + opticalSize: 5.0, + ), + child: Icon(Icons.abc, fill: 0.6, weight: 7.0, grade: 8.0, opticalSize: 9.0), + ), + ), + ); + + final RichText text = tester.widget(find.byType(RichText)); + expect(text.text.style!.fontVariations, isNotNull); + expect(text.text.style!.fontVariations, [ + const FontVariation('FILL', 0.6), + const FontVariation('wght', 7.0), + const FontVariation('GRAD', 8.0), + const FontVariation('opsz', 9.0) + ]); + }); + test('Throws if given invalid values', () { expect(() => Icon(Icons.abc, fill: -0.1), throwsAssertionError); expect(() => Icon(Icons.abc, fill: 1.1), throwsAssertionError);