Skip to content

Commit e7c867c

Browse files
authored
Add support for font variation based theming to Icon (#109140)
1 parent f3d182f commit e7c867c

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

packages/flutter/lib/src/widgets/icon.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ class Icon extends StatelessWidget {
240240

241241
final double? iconSize = size ?? iconTheme.size;
242242

243+
final double? iconFill = fill ?? iconTheme.fill;
244+
245+
final double? iconWeight = weight ?? iconTheme.weight;
246+
247+
final double? iconGrade = grade ?? iconTheme.grade;
248+
249+
final double? iconOpticalSize = opticalSize ?? iconTheme.opticalSize;
250+
243251
final List<Shadow>? iconShadows = shadows ?? iconTheme.shadows;
244252

245253
if (icon == null) {
@@ -262,10 +270,10 @@ class Icon extends StatelessWidget {
262270
text: String.fromCharCode(icon!.codePoint),
263271
style: TextStyle(
264272
fontVariations: <FontVariation>[
265-
if (fill != null) FontVariation('FILL', fill!),
266-
if (weight != null) FontVariation('wght', weight!),
267-
if (grade != null) FontVariation('GRAD', grade!),
268-
if (opticalSize != null) FontVariation('opsz', opticalSize!),
273+
if (iconFill != null) FontVariation('FILL', iconFill),
274+
if (iconWeight != null) FontVariation('wght', iconWeight),
275+
if (iconGrade != null) FontVariation('GRAD', iconGrade),
276+
if (iconOpticalSize != null) FontVariation('opsz', iconOpticalSize),
269277
],
270278
inherit: false,
271279
color: iconColor,

packages/flutter/test/widgets/icon_test.dart

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ void main() {
220220
);
221221

222222
RichText text = tester.widget(find.byType(RichText));
223-
expect(text.text.style!.fontVariations, <FontVariation>[]);
223+
expect(text.text.style!.fontVariations, <FontVariation>[
224+
const FontVariation('FILL', 0.0),
225+
const FontVariation('wght', 400.0),
226+
const FontVariation('GRAD', 0.0),
227+
const FontVariation('opsz', 48.0)
228+
]);
224229

225230
await tester.pumpWidget(
226231
const Directionality(
@@ -239,6 +244,57 @@ void main() {
239244
]);
240245
});
241246

247+
testWidgets('Fill, weight, grade, and optical size can be set at the theme-level', (WidgetTester tester) async {
248+
await tester.pumpWidget(
249+
const Directionality(
250+
textDirection: TextDirection.ltr,
251+
child: IconTheme(
252+
data: IconThemeData(
253+
fill: 0.2,
254+
weight: 3.0,
255+
grade: 4.0,
256+
opticalSize: 5.0,
257+
),
258+
child: Icon(Icons.abc),
259+
),
260+
),
261+
);
262+
263+
final RichText text = tester.widget(find.byType(RichText));
264+
expect(text.text.style!.fontVariations, <FontVariation>[
265+
const FontVariation('FILL', 0.2),
266+
const FontVariation('wght', 3.0),
267+
const FontVariation('GRAD', 4.0),
268+
const FontVariation('opsz', 5.0)
269+
]);
270+
});
271+
272+
testWidgets('Theme-level fill, weight, grade, and optical size can be overriden', (WidgetTester tester) async {
273+
await tester.pumpWidget(
274+
const Directionality(
275+
textDirection: TextDirection.ltr,
276+
child: IconTheme(
277+
data: IconThemeData(
278+
fill: 0.2,
279+
weight: 3.0,
280+
grade: 4.0,
281+
opticalSize: 5.0,
282+
),
283+
child: Icon(Icons.abc, fill: 0.6, weight: 7.0, grade: 8.0, opticalSize: 9.0),
284+
),
285+
),
286+
);
287+
288+
final RichText text = tester.widget(find.byType(RichText));
289+
expect(text.text.style!.fontVariations, isNotNull);
290+
expect(text.text.style!.fontVariations, <FontVariation>[
291+
const FontVariation('FILL', 0.6),
292+
const FontVariation('wght', 7.0),
293+
const FontVariation('GRAD', 8.0),
294+
const FontVariation('opsz', 9.0)
295+
]);
296+
});
297+
242298
test('Throws if given invalid values', () {
243299
expect(() => Icon(Icons.abc, fill: -0.1), throwsAssertionError);
244300
expect(() => Icon(Icons.abc, fill: 1.1), throwsAssertionError);

0 commit comments

Comments
 (0)