Skip to content

Commit cd60efc

Browse files
authored
Fix default avatar icon theme size for Material 2 (#152307)
fixes [ActionChip avatar still does not respect padding](flutter/flutter#116508) ### Code sample <details> <summary>expand to view the code sample</summary> ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @OverRide Widget build(BuildContext context) { return MaterialApp( theme: ThemeData(brightness: Brightness.dark, useMaterial3: false), home: Scaffold( body: Center( child: Builder( builder: (BuildContext context) { return Chip( padding: const EdgeInsets.all(16.0), avatar: const Icon(Icons.favorite), label: const Text('Chip A'), onDeleted: () {}, ); }, ), ), ), ); } } ``` </details> ### Before <img width="490" alt="Screenshot 2024-07-25 at 16 24 08" src="https://github.com/user-attachments/assets/45408aa2-b3ab-4ff9-ae72-53a91c87c76a"> ### After <img width="490" alt="Screenshot 2024-07-25 at 16 23 56" src="https://github.com/user-attachments/assets/07ba367d-9ca3-46cc-8122-d1155dd2f32b">
1 parent 61848a4 commit cd60efc

File tree

4 files changed

+16
-89
lines changed

4 files changed

+16
-89
lines changed

packages/flutter/lib/src/material/chip.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
13691369
final TextStyle resolvedLabelStyle = effectiveLabelStyle.copyWith(color: resolvedLabelColor);
13701370
final Widget? avatar = iconTheme != null && hasAvatar
13711371
? IconTheme.merge(
1372-
data: theme.useMaterial3 ? chipDefaults.iconTheme!.merge(iconTheme) : iconTheme,
1372+
data: chipDefaults.iconTheme!.merge(iconTheme),
13731373
child: widget.avatar!,
13741374
)
13751375
: widget.avatar;

packages/flutter/lib/src/material/chip_theme.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class ChipThemeData with Diagnosticable {
276276
brightness: brightness,
277277
elevation: 0.0,
278278
pressElevation: 8.0,
279+
iconTheme: const IconThemeData(size: 18.0),
279280
);
280281
}
281282

packages/flutter/test/material/chip_test.dart

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -231,83 +231,6 @@ Finder findTooltipContainer(String tooltipText) {
231231
}
232232

233233
void main() {
234-
testWidgets('M2 Chip defaults', (WidgetTester tester) async {
235-
late TextTheme textTheme;
236-
237-
Widget buildFrame(Brightness brightness) {
238-
return MaterialApp(
239-
theme: ThemeData(brightness: brightness, useMaterial3: false),
240-
home: Scaffold(
241-
body: Center(
242-
child: Builder(
243-
builder: (BuildContext context) {
244-
textTheme = Theme.of(context).textTheme;
245-
return Chip(
246-
avatar: const CircleAvatar(child: Text('A')),
247-
label: const Text('Chip A'),
248-
onDeleted: () { },
249-
);
250-
},
251-
),
252-
),
253-
),
254-
);
255-
}
256-
257-
await tester.pumpWidget(buildFrame(Brightness.light));
258-
expect(getMaterialBox(tester), paints..rrect()..circle(color: const Color(0xff1976d2)));
259-
expect(tester.getSize(find.byType(Chip)), const Size(156.0, 48.0));
260-
expect(getMaterial(tester).color, null);
261-
expect(getMaterial(tester).elevation, 0);
262-
expect(getMaterial(tester).shape, const StadiumBorder());
263-
expect(getIconData(tester).color?.value, 0xffffffff);
264-
expect(getIconData(tester).opacity, null);
265-
expect(getIconData(tester).size, null);
266-
267-
TextStyle labelStyle = getLabelStyle(tester, 'Chip A').style;
268-
expect(labelStyle.color?.value, 0xde000000);
269-
expect(labelStyle.fontFamily, textTheme.bodyLarge?.fontFamily);
270-
expect(labelStyle.fontFamilyFallback, textTheme.bodyLarge?.fontFamilyFallback);
271-
expect(labelStyle.fontFeatures, textTheme.bodyLarge?.fontFeatures);
272-
expect(labelStyle.fontSize, textTheme.bodyLarge?.fontSize);
273-
expect(labelStyle.fontStyle, textTheme.bodyLarge?.fontStyle);
274-
expect(labelStyle.fontWeight, textTheme.bodyLarge?.fontWeight);
275-
expect(labelStyle.height, textTheme.bodyLarge?.height);
276-
expect(labelStyle.inherit, textTheme.bodyLarge?.inherit);
277-
expect(labelStyle.leadingDistribution, textTheme.bodyLarge?.leadingDistribution);
278-
expect(labelStyle.letterSpacing, textTheme.bodyLarge?.letterSpacing);
279-
expect(labelStyle.overflow, textTheme.bodyLarge?.overflow);
280-
expect(labelStyle.textBaseline, textTheme.bodyLarge?.textBaseline);
281-
expect(labelStyle.wordSpacing, textTheme.bodyLarge?.wordSpacing);
282-
283-
await tester.pumpWidget(buildFrame(Brightness.dark));
284-
await tester.pumpAndSettle(); // Theme transition animation
285-
expect(getMaterialBox(tester), paints..rrect(color: const Color(0x1fffffff)));
286-
expect(tester.getSize(find.byType(Chip)), const Size(156.0, 48.0));
287-
expect(getMaterial(tester).color, null);
288-
expect(getMaterial(tester).elevation, 0);
289-
expect(getMaterial(tester).shape, const StadiumBorder());
290-
expect(getIconData(tester).color?.value, 0xffffffff);
291-
expect(getIconData(tester).opacity, null);
292-
expect(getIconData(tester).size, null);
293-
294-
labelStyle = getLabelStyle(tester, 'Chip A').style;
295-
expect(labelStyle.color?.value, 0xdeffffff);
296-
expect(labelStyle.fontFamily, textTheme.bodyLarge?.fontFamily);
297-
expect(labelStyle.fontFamilyFallback, textTheme.bodyLarge?.fontFamilyFallback);
298-
expect(labelStyle.fontFeatures, textTheme.bodyLarge?.fontFeatures);
299-
expect(labelStyle.fontSize, textTheme.bodyLarge?.fontSize);
300-
expect(labelStyle.fontStyle, textTheme.bodyLarge?.fontStyle);
301-
expect(labelStyle.fontWeight, textTheme.bodyLarge?.fontWeight);
302-
expect(labelStyle.height, textTheme.bodyLarge?.height);
303-
expect(labelStyle.inherit, textTheme.bodyLarge?.inherit);
304-
expect(labelStyle.leadingDistribution, textTheme.bodyLarge?.leadingDistribution);
305-
expect(labelStyle.letterSpacing, textTheme.bodyLarge?.letterSpacing);
306-
expect(labelStyle.overflow, textTheme.bodyLarge?.overflow);
307-
expect(labelStyle.textBaseline, textTheme.bodyLarge?.textBaseline);
308-
expect(labelStyle.wordSpacing, textTheme.bodyLarge?.wordSpacing);
309-
});
310-
311234
testWidgets('M3 Chip defaults', (WidgetTester tester) async {
312235
late TextTheme textTheme;
313236
final ThemeData lightTheme = ThemeData.light();
@@ -4484,7 +4407,7 @@ void main() {
44844407
expect(box.size, equals(const Size(128, 32.0 + 16.0)));
44854408
expect(textBox.size, equals(const Size(56, 14)));
44864409
expect(iconBox.size, equals(const Size(18, 18)));
4487-
expect(avatarBox.size, equals(const Size(24, 24)));
4410+
expect(avatarBox.size, equals(const Size(18, 18)));
44884411
expect(textBox.top, equals(17));
44894412
expect(box.bottom - textBox.bottom, equals(17));
44904413
expect(textBox.left, equals(372));
@@ -4499,7 +4422,7 @@ void main() {
44994422
expect(box.size, equals(const Size(128, 60)));
45004423
expect(textBox.size, equals(const Size(56, 14)));
45014424
expect(iconBox.size, equals(const Size(18, 18)));
4502-
expect(avatarBox.size, equals(const Size(24, 24)));
4425+
expect(avatarBox.size, equals(const Size(18, 18)));
45034426
expect(textBox.top, equals(23));
45044427
expect(box.bottom - textBox.bottom, equals(23));
45054428
expect(textBox.left, equals(372));
@@ -4514,7 +4437,7 @@ void main() {
45144437
expect(box.size, equals(const Size(128, 36)));
45154438
expect(textBox.size, equals(const Size(56, 14)));
45164439
expect(iconBox.size, equals(const Size(18, 18)));
4517-
expect(avatarBox.size, equals(const Size(24, 24)));
4440+
expect(avatarBox.size, equals(const Size(18, 18)));
45184441
expect(textBox.top, equals(11));
45194442
expect(box.bottom - textBox.bottom, equals(11));
45204443
expect(textBox.left, equals(372));
@@ -4531,7 +4454,7 @@ void main() {
45314454
expect(box.size, equals(const Size(128, 36)));
45324455
expect(textBox.size, equals(const Size(56, 14)));
45334456
expect(iconBox.size, equals(const Size(18, 18)));
4534-
expect(avatarBox.size, equals(const Size(24, 24)));
4457+
expect(avatarBox.size, equals(const Size(18, 18)));
45354458
expect(textBox.top, equals(11));
45364459
expect(box.bottom - textBox.bottom, equals(11));
45374460
expect(textBox.left, equals(372));
@@ -5489,9 +5412,9 @@ void main() {
54895412
expect(getMaterial(tester).color, null);
54905413
expect(getMaterial(tester).elevation, 0);
54915414
expect(getMaterial(tester).shape, const StadiumBorder());
5492-
expect(getIconData(tester).color?.value, 0xffffffff);
5415+
expect(getIconData(tester).color, const Color(0xdd000000));
54935416
expect(getIconData(tester).opacity, null);
5494-
expect(getIconData(tester).size, null);
5417+
expect(getIconData(tester).size, 18.0);
54955418

54965419
TextStyle labelStyle = getLabelStyle(tester, 'Chip A').style;
54975420
expect(labelStyle.color?.value, 0xde000000);
@@ -5518,7 +5441,7 @@ void main() {
55185441
expect(getMaterial(tester).shape, const StadiumBorder());
55195442
expect(getIconData(tester).color?.value, 0xffffffff);
55205443
expect(getIconData(tester).opacity, null);
5521-
expect(getIconData(tester).size, null);
5444+
expect(getIconData(tester).size, 18.0);
55225445

55235446
labelStyle = getLabelStyle(tester, 'Chip A').style;
55245447
expect(labelStyle.color?.value, 0xdeffffff);

packages/flutter/test/material/chip_theme_test.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ void main() {
613613
selectedShadowColor: Colors.black,
614614
showCheckmark: false,
615615
checkmarkColor: Colors.black,
616+
iconTheme: const IconThemeData(size: 26.0),
616617
);
617618
final ChipThemeData chipThemeWhite = ChipThemeData.fromDefaults(
618619
secondaryColor: Colors.white,
@@ -630,6 +631,7 @@ void main() {
630631
selectedShadowColor: Colors.white,
631632
showCheckmark: true,
632633
checkmarkColor: Colors.white,
634+
iconTheme: const IconThemeData(size: 22.0),
633635
);
634636

635637
final ChipThemeData lerp = ChipThemeData.lerp(chipThemeBlack, chipThemeWhite, 0.5)!;
@@ -653,7 +655,7 @@ void main() {
653655
expect(lerp.elevation, 3.0);
654656
expect(lerp.pressElevation, 7.0);
655657
expect(lerp.checkmarkColor, equals(middleGrey));
656-
expect(lerp.iconTheme, isNull);
658+
expect(lerp.iconTheme, const IconThemeData(size: 24.0));
657659

658660
expect(ChipThemeData.lerp(null, null, 0.25), isNull);
659661

@@ -677,7 +679,7 @@ void main() {
677679
expect(lerpANull25.elevation, 1.25);
678680
expect(lerpANull25.pressElevation, 2.5);
679681
expect(lerpANull25.checkmarkColor, equals(Colors.white.withAlpha(0x40)));
680-
expect(lerp.iconTheme, isNull);
682+
expect(lerpANull25.iconTheme, const IconThemeData(size: 5.5));
681683

682684
final ChipThemeData lerpANull75 = ChipThemeData.lerp(null, chipThemeWhite, 0.75)!;
683685
expect(lerpANull75.backgroundColor, equals(Colors.black.withAlpha(0x17)));
@@ -699,6 +701,7 @@ void main() {
699701
expect(lerpANull75.elevation, 3.75);
700702
expect(lerpANull75.pressElevation, 7.5);
701703
expect(lerpANull75.checkmarkColor, equals(Colors.white.withAlpha(0xbf)));
704+
expect(lerpANull75.iconTheme, const IconThemeData(size: 16.5));
702705

703706
final ChipThemeData lerpBNull25 = ChipThemeData.lerp(chipThemeBlack, null, 0.25)!;
704707
expect(lerpBNull25.backgroundColor, equals(Colors.white.withAlpha(0x17)));
@@ -720,7 +723,7 @@ void main() {
720723
expect(lerpBNull25.elevation, 0.75);
721724
expect(lerpBNull25.pressElevation, 3.0);
722725
expect(lerpBNull25.checkmarkColor, equals(Colors.black.withAlpha(0xbf)));
723-
expect(lerp.iconTheme, isNull);
726+
expect(lerpBNull25.iconTheme, const IconThemeData(size: 19.5));
724727

725728
final ChipThemeData lerpBNull75 = ChipThemeData.lerp(chipThemeBlack, null, 0.75)!;
726729
expect(lerpBNull75.backgroundColor, equals(Colors.white.withAlpha(0x08)));
@@ -742,7 +745,7 @@ void main() {
742745
expect(lerpBNull75.elevation, 0.25);
743746
expect(lerpBNull75.pressElevation, 1.0);
744747
expect(lerpBNull75.checkmarkColor, equals(Colors.black.withAlpha(0x40)));
745-
expect(lerp.iconTheme, isNull);
748+
expect(lerpBNull75.iconTheme, const IconThemeData(size: 6.5));
746749
});
747750

748751
testWidgets('Chip uses stateful color from chip theme', (WidgetTester tester) async {

0 commit comments

Comments
 (0)