Skip to content

Commit c8f867b

Browse files
authored
Chip theme cleanup (#106384)
- Added complete support for showCheckmark to ChipThemeData. - Removed build method checks for ThemeData.chipTheme, as that is already handled by ChipTheme.of().
1 parent ac8329a commit c8f867b

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

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

+8-18
Original file line numberDiff line numberDiff line change
@@ -1094,10 +1094,6 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
10941094
);
10951095
}
10961096

1097-
static const double _defaultElevation = 0.0;
1098-
static const double _defaultPressElevation = 8.0;
1099-
static const Color _defaultShadowColor = Colors.black;
1100-
11011097
@override
11021098
Widget build(BuildContext context) {
11031099
assert(debugCheckHasMaterial(context));
@@ -1128,37 +1124,31 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
11281124

11291125
final double elevation = widget.elevation
11301126
?? chipTheme.elevation
1131-
?? theme.chipTheme.elevation
1132-
?? _defaultElevation;
1127+
?? chipDefaults.elevation!;
11331128
final double pressElevation = widget.pressElevation
11341129
?? chipTheme.pressElevation
1135-
?? theme.chipTheme.pressElevation
1136-
?? _defaultPressElevation;
1130+
?? chipDefaults.pressElevation!;
11371131
final Color shadowColor = widget.shadowColor
11381132
?? chipTheme.shadowColor
1139-
?? theme.chipTheme.shadowColor
1140-
?? _defaultShadowColor;
1133+
?? chipDefaults.shadowColor!;
11411134
final Color selectedShadowColor = widget.selectedShadowColor
11421135
?? chipTheme.selectedShadowColor
1143-
?? theme.chipTheme.selectedShadowColor
1144-
?? _defaultShadowColor;
1136+
?? chipDefaults.selectedShadowColor!;
11451137
final Color? checkmarkColor = widget.checkmarkColor
11461138
?? chipTheme.checkmarkColor
1147-
?? theme.chipTheme.checkmarkColor;
1139+
?? chipDefaults.checkmarkColor;
11481140
final bool showCheckmark = widget.showCheckmark
11491141
?? chipTheme.showCheckmark
1150-
?? theme.chipTheme.showCheckmark
1151-
?? true;
1142+
?? chipDefaults.showCheckmark!;
11521143
final EdgeInsetsGeometry padding = widget.padding
11531144
?? chipTheme.padding
1154-
?? theme.chipTheme.padding
11551145
?? chipDefaults.padding!;
1146+
// Widget's label style is merged with this below.
11561147
final TextStyle labelStyle = chipTheme.labelStyle
1157-
?? theme.chipTheme.labelStyle
11581148
?? chipDefaults.labelStyle!;
11591149
final EdgeInsetsGeometry labelPadding = widget.labelPadding
11601150
?? chipTheme.labelPadding
1161-
?? theme.chipTheme.labelPadding
1151+
?? chipDefaults.labelPadding
11621152
?? defaultLabelPadding;
11631153

11641154
final TextStyle effectiveLabelStyle = labelStyle.merge(widget.labelStyle);

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

+11
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,15 @@ class ChipThemeData with Diagnosticable {
256256
disabledColor: disabledColor,
257257
selectedColor: selectedColor,
258258
secondarySelectedColor: secondarySelectedColor,
259+
shadowColor: Colors.black,
260+
selectedShadowColor: Colors.black,
261+
showCheckmark: true,
259262
padding: padding,
260263
labelStyle: labelStyle,
261264
secondaryLabelStyle: secondaryLabelStyle,
262265
brightness: brightness,
266+
elevation: 0.0,
267+
pressElevation: 8.0,
263268
);
264269
}
265270

@@ -420,6 +425,7 @@ class ChipThemeData with Diagnosticable {
420425
Color? secondarySelectedColor,
421426
Color? shadowColor,
422427
Color? selectedShadowColor,
428+
bool? showCheckmark,
423429
Color? checkmarkColor,
424430
EdgeInsetsGeometry? labelPadding,
425431
EdgeInsetsGeometry? padding,
@@ -439,6 +445,7 @@ class ChipThemeData with Diagnosticable {
439445
secondarySelectedColor: secondarySelectedColor ?? this.secondarySelectedColor,
440446
shadowColor: shadowColor ?? this.shadowColor,
441447
selectedShadowColor: selectedShadowColor ?? this.selectedShadowColor,
448+
showCheckmark: showCheckmark ?? this.showCheckmark,
442449
checkmarkColor: checkmarkColor ?? this.checkmarkColor,
443450
labelPadding: labelPadding ?? this.labelPadding,
444451
padding: padding ?? this.padding,
@@ -470,6 +477,7 @@ class ChipThemeData with Diagnosticable {
470477
secondarySelectedColor: Color.lerp(a?.secondarySelectedColor, b?.secondarySelectedColor, t),
471478
shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
472479
selectedShadowColor: Color.lerp(a?.selectedShadowColor, b?.selectedShadowColor, t),
480+
showCheckmark: t < 0.5 ? a?.showCheckmark ?? true : b?.showCheckmark ?? true,
473481
checkmarkColor: Color.lerp(a?.checkmarkColor, b?.checkmarkColor, t),
474482
labelPadding: EdgeInsetsGeometry.lerp(a?.labelPadding, b?.labelPadding, t),
475483
padding: EdgeInsetsGeometry.lerp(a?.padding, b?.padding, t),
@@ -514,6 +522,7 @@ class ChipThemeData with Diagnosticable {
514522
secondarySelectedColor,
515523
shadowColor,
516524
selectedShadowColor,
525+
showCheckmark,
517526
checkmarkColor,
518527
labelPadding,
519528
padding,
@@ -542,6 +551,7 @@ class ChipThemeData with Diagnosticable {
542551
&& other.secondarySelectedColor == secondarySelectedColor
543552
&& other.shadowColor == shadowColor
544553
&& other.selectedShadowColor == selectedShadowColor
554+
&& other.showCheckmark == showCheckmark
545555
&& other.checkmarkColor == checkmarkColor
546556
&& other.labelPadding == labelPadding
547557
&& other.padding == padding
@@ -564,6 +574,7 @@ class ChipThemeData with Diagnosticable {
564574
properties.add(ColorProperty('secondarySelectedColor', secondarySelectedColor, defaultValue: null));
565575
properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
566576
properties.add(ColorProperty('selectedShadowColor', selectedShadowColor, defaultValue: null));
577+
properties.add(DiagnosticsProperty<bool>('showCheckmark', showCheckmark, defaultValue: null));
567578
properties.add(ColorProperty('checkMarkColor', checkmarkColor, defaultValue: null));
568579
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('labelPadding', labelPadding, defaultValue: null));
569580
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));

packages/flutter/test/material/chip_theme_test.dart

+46-9
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ void main() {
112112
'secondarySelectedColor: Color(0xfffffff4)',
113113
'shadowColor: Color(0xfffffff5)',
114114
'selectedShadowColor: Color(0xfffffff6)',
115+
'showCheckmark: true',
115116
'checkMarkColor: Color(0xfffffff7)',
116117
'labelPadding: EdgeInsets.all(1.0)',
117118
'padding: EdgeInsets.all(2.0)',
@@ -268,25 +269,54 @@ void main() {
268269
});
269270

270271
testWidgets('ChipTheme.fromDefaults', (WidgetTester tester) async {
272+
const TextStyle labelStyle = TextStyle();
271273
ChipThemeData chipTheme = ChipThemeData.fromDefaults(
272274
brightness: Brightness.light,
273275
secondaryColor: Colors.red,
274-
labelStyle: const TextStyle(),
276+
labelStyle: labelStyle,
275277
);
276-
expect(chipTheme.backgroundColor, equals(Colors.black.withAlpha(0x1f)));
277-
expect(chipTheme.selectedColor, equals(Colors.black.withAlpha(0x3d)));
278-
expect(chipTheme.secondarySelectedColor, equals(Colors.red.withAlpha(0x3d)));
279-
expect(chipTheme.deleteIconColor, equals(Colors.black.withAlpha(0xde)));
278+
expect(chipTheme.backgroundColor, Colors.black.withAlpha(0x1f));
279+
expect(chipTheme.deleteIconColor, Colors.black.withAlpha(0xde));
280+
expect(chipTheme.disabledColor, Colors.black.withAlpha(0x0c));
281+
expect(chipTheme.selectedColor, Colors.black.withAlpha(0x3d));
282+
expect(chipTheme.secondarySelectedColor, Colors.red.withAlpha(0x3d));
283+
expect(chipTheme.shadowColor, Colors.black);
284+
expect(chipTheme.selectedShadowColor, Colors.black);
285+
expect(chipTheme.showCheckmark, true);
286+
expect(chipTheme.checkmarkColor, null);
287+
expect(chipTheme.labelPadding, null);
288+
expect(chipTheme.padding, const EdgeInsets.all(4.0));
289+
expect(chipTheme.side, null);
290+
expect(chipTheme.shape, null);
291+
expect(chipTheme.labelStyle, labelStyle.copyWith(color: Colors.black.withAlpha(0xde)));
292+
expect(chipTheme.secondaryLabelStyle, labelStyle.copyWith(color: Colors.red.withAlpha(0xde)));
293+
expect(chipTheme.brightness, Brightness.light);
294+
expect(chipTheme.elevation, 0.0);
295+
expect(chipTheme.pressElevation, 8.0);
280296

281297
chipTheme = ChipThemeData.fromDefaults(
282298
brightness: Brightness.dark,
283299
secondaryColor: Colors.tealAccent[200]!,
284300
labelStyle: const TextStyle(),
285301
);
286-
expect(chipTheme.backgroundColor, equals(Colors.white.withAlpha(0x1f)));
287-
expect(chipTheme.selectedColor, equals(Colors.white.withAlpha(0x3d)));
288-
expect(chipTheme.secondarySelectedColor, equals(Colors.tealAccent[200]!.withAlpha(0x3d)));
289-
expect(chipTheme.deleteIconColor, equals(Colors.white.withAlpha(0xde)));
302+
expect(chipTheme.backgroundColor, Colors.white.withAlpha(0x1f));
303+
expect(chipTheme.deleteIconColor, Colors.white.withAlpha(0xde));
304+
expect(chipTheme.disabledColor, Colors.white.withAlpha(0x0c));
305+
expect(chipTheme.selectedColor, Colors.white.withAlpha(0x3d));
306+
expect(chipTheme.secondarySelectedColor, Colors.tealAccent[200]!.withAlpha(0x3d));
307+
expect(chipTheme.shadowColor, Colors.black);
308+
expect(chipTheme.selectedShadowColor, Colors.black);
309+
expect(chipTheme.showCheckmark, true);
310+
expect(chipTheme.checkmarkColor, null);
311+
expect(chipTheme.labelPadding, null);
312+
expect(chipTheme.padding, const EdgeInsets.all(4.0));
313+
expect(chipTheme.side, null);
314+
expect(chipTheme.shape, null);
315+
expect(chipTheme.labelStyle, labelStyle.copyWith(color: Colors.white.withAlpha(0xde)));
316+
expect(chipTheme.secondaryLabelStyle, labelStyle.copyWith(color: Colors.tealAccent[200]!.withAlpha(0xde)));
317+
expect(chipTheme.brightness, Brightness.dark);
318+
expect(chipTheme.elevation, 0.0);
319+
expect(chipTheme.pressElevation, 8.0);
290320
});
291321

292322

@@ -366,6 +396,7 @@ void main() {
366396
pressElevation: 4.0,
367397
shadowColor: Colors.black,
368398
selectedShadowColor: Colors.black,
399+
showCheckmark: false,
369400
checkmarkColor: Colors.black,
370401
);
371402
final ChipThemeData chipThemeWhite = ChipThemeData.fromDefaults(
@@ -381,6 +412,7 @@ void main() {
381412
pressElevation: 10.0,
382413
shadowColor: Colors.white,
383414
selectedShadowColor: Colors.white,
415+
showCheckmark: true,
384416
checkmarkColor: Colors.white,
385417
);
386418

@@ -393,6 +425,7 @@ void main() {
393425
expect(lerp.secondarySelectedColor, equals(middleGrey.withAlpha(0x3d)));
394426
expect(lerp.shadowColor, equals(middleGrey));
395427
expect(lerp.selectedShadowColor, equals(middleGrey));
428+
expect(lerp.showCheckmark, equals(true));
396429
expect(lerp.labelPadding, equals(const EdgeInsets.all(4.0)));
397430
expect(lerp.padding, equals(const EdgeInsets.all(3.0)));
398431
expect(lerp.side!.color, equals(middleGrey));
@@ -414,6 +447,7 @@ void main() {
414447
expect(lerpANull25.secondarySelectedColor, equals(Colors.white.withAlpha(0x0f)));
415448
expect(lerpANull25.shadowColor, equals(Colors.white.withAlpha(0x40)));
416449
expect(lerpANull25.selectedShadowColor, equals(Colors.white.withAlpha(0x40)));
450+
expect(lerpANull25.showCheckmark, equals(true));
417451
expect(lerpANull25.labelPadding, equals(const EdgeInsets.only(top: 2.0, bottom: 2.0)));
418452
expect(lerpANull25.padding, equals(const EdgeInsets.all(0.5)));
419453
expect(lerpANull25.side!.color, equals(Colors.white.withAlpha(0x3f)));
@@ -433,6 +467,7 @@ void main() {
433467
expect(lerpANull75.secondarySelectedColor, equals(Colors.white.withAlpha(0x2e)));
434468
expect(lerpANull75.shadowColor, equals(Colors.white.withAlpha(0xbf)));
435469
expect(lerpANull75.selectedShadowColor, equals(Colors.white.withAlpha(0xbf)));
470+
expect(lerpANull75.showCheckmark, equals(true));
436471
expect(lerpANull75.labelPadding, equals(const EdgeInsets.only(top: 6.0, bottom: 6.0)));
437472
expect(lerpANull75.padding, equals(const EdgeInsets.all(1.5)));
438473
expect(lerpANull75.side!.color, equals(Colors.white.withAlpha(0xbf)));
@@ -452,6 +487,7 @@ void main() {
452487
expect(lerpBNull25.secondarySelectedColor, equals(Colors.black.withAlpha(0x2e)));
453488
expect(lerpBNull25.shadowColor, equals(Colors.black.withAlpha(0xbf)));
454489
expect(lerpBNull25.selectedShadowColor, equals(Colors.black.withAlpha(0xbf)));
490+
expect(lerpBNull25.showCheckmark, equals(false));
455491
expect(lerpBNull25.labelPadding, equals(const EdgeInsets.only(left: 6.0, right: 6.0)));
456492
expect(lerpBNull25.padding, equals(const EdgeInsets.all(3.0)));
457493
expect(lerpBNull25.side!.color, equals(Colors.black.withAlpha(0x3f)));
@@ -471,6 +507,7 @@ void main() {
471507
expect(lerpBNull75.secondarySelectedColor, equals(Colors.black.withAlpha(0x0f)));
472508
expect(lerpBNull75.shadowColor, equals(Colors.black.withAlpha(0x40)));
473509
expect(lerpBNull75.selectedShadowColor, equals(Colors.black.withAlpha(0x40)));
510+
expect(lerpBNull75.showCheckmark, equals(true));
474511
expect(lerpBNull75.labelPadding, equals(const EdgeInsets.only(left: 2.0, right: 2.0)));
475512
expect(lerpBNull75.padding, equals(const EdgeInsets.all(1.0)));
476513
expect(lerpBNull75.side!.color, equals(Colors.black.withAlpha(0xbf)));

0 commit comments

Comments
 (0)