Skip to content

Commit 2abaedd

Browse files
authored
[material_ui] Migrate M3 Chip template to use new gen_defaults (flutter#12847)
Work towards flutter/flutter#187899 Fixes flutter/flutter#188408 Migrates the M3 Chip defaults template to the new `packages/material_ui/tool/gen_defaults` generator. ## Pre-Review Checklist
1 parent de1f758 commit 2abaedd

5 files changed

Lines changed: 48 additions & 117 deletions

File tree

‎packages/material_ui/lib/src/chip.dart‎

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import 'theme.dart';
3232
import 'theme_data.dart';
3333
import 'tooltip.dart';
3434

35+
part 'generated/chip_defaults_m3.g.dart';
36+
3537
// Some design constants
3638
const double _kChipHeight = 32.0;
3739

@@ -2516,87 +2518,3 @@ class _RenderEnsureMinSemanticsSize extends RenderProxyBox {
25162518
);
25172519
}
25182520
}
2519-
2520-
// BEGIN GENERATED TOKEN PROPERTIES - Chip
2521-
2522-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
2523-
// "END GENERATED" comments are generated from data in the Material
2524-
// Design token database by the script:
2525-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
2526-
2527-
// dart format off
2528-
class _ChipDefaultsM3 extends ChipThemeData {
2529-
_ChipDefaultsM3(this.context, this.isEnabled)
2530-
: super(
2531-
elevation: 0.0,
2532-
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
2533-
showCheckmark: true,
2534-
);
2535-
2536-
final BuildContext context;
2537-
final bool isEnabled;
2538-
late final ColorScheme _colors = Theme.of(context).colorScheme;
2539-
late final TextTheme _textTheme = Theme.of(context).textTheme;
2540-
2541-
@override
2542-
TextStyle? get labelStyle => _textTheme.labelLarge?.copyWith(
2543-
color: isEnabled
2544-
? _colors.onSurfaceVariant
2545-
: _colors.onSurface,
2546-
);
2547-
2548-
@override
2549-
WidgetStateProperty<Color?>? get color => null; // Subclasses override this getter
2550-
2551-
@override
2552-
Color? get shadowColor => Colors.transparent;
2553-
2554-
@override
2555-
Color? get surfaceTintColor => Colors.transparent;
2556-
2557-
@override
2558-
Color? get checkmarkColor => null;
2559-
2560-
@override
2561-
Color? get deleteIconColor => isEnabled
2562-
? _colors.onSurfaceVariant
2563-
: _colors.onSurface;
2564-
2565-
@override
2566-
BorderSide? get side => isEnabled
2567-
? BorderSide(color: _colors.outlineVariant)
2568-
: BorderSide(color: _colors.onSurface.withOpacity(0.12));
2569-
2570-
@override
2571-
IconThemeData? get iconTheme => IconThemeData(
2572-
color: isEnabled
2573-
? _colors.primary
2574-
: _colors.onSurface,
2575-
size: 18.0,
2576-
);
2577-
2578-
@override
2579-
EdgeInsetsGeometry? get padding => const EdgeInsets.all(8.0);
2580-
2581-
/// The label padding of the chip scales with the font size specified in the
2582-
/// [labelStyle], and the system font size settings that scale font sizes
2583-
/// globally.
2584-
///
2585-
/// The chip at effective font size 14.0 starts with 8px on each side and as
2586-
/// the font size scales up to closer to 28.0, the label padding is linearly
2587-
/// interpolated from 8px to 4px. Once the label has a font size of 2 or
2588-
/// higher, label padding remains 4px.
2589-
@override
2590-
EdgeInsetsGeometry? get labelPadding {
2591-
final double fontSize = labelStyle?.fontSize ?? 14.0;
2592-
final double fontSizeRatio = MediaQuery.textScalerOf(context).scale(fontSize) / 14.0;
2593-
return EdgeInsets.lerp(
2594-
const EdgeInsets.symmetric(horizontal: 8.0),
2595-
const EdgeInsets.symmetric(horizontal: 4.0),
2596-
clampDouble(fontSizeRatio - 1.0, 0.0, 1.0),
2597-
)!;
2598-
}
2599-
}
2600-
// dart format on
2601-
2602-
// END GENERATED TOKEN PROPERTIES - Chip

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/chip_defaults.g.dart renamed to packages/material_ui/lib/src/generated/chip_defaults_m3.g.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
// Do not edit by hand. The code is generated from data in the Material
6+
// Design token database by the script:
7+
// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart.
8+
part of '../chip.dart';
9+
510
class _ChipDefaultsM3 extends ChipThemeData {
611
_ChipDefaultsM3(this.context, this.isEnabled)
712
: super(

‎packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import '../templates/bottom_sheet_template.dart';
2121
import '../templates/button_template.dart';
2222
import '../templates/card_template.dart';
2323
import '../templates/checkbox_template.dart';
24-
25-
// import '../templates/chip_template.dart';
24+
import '../templates/chip_template.dart';
2625
// import '../templates/color_scheme_template.dart';
2726
// import '../templates/date_picker_template.dart';
2827
// import '../templates/dialog_template.dart';
@@ -81,7 +80,7 @@ Future<void> main(List<String> args) async {
8180
const CardTemplateM3('Filled Card').generateFile(verbose: verbose);
8281
const CardTemplateM3('Outlined Card').generateFile(verbose: verbose);
8382
const CheckboxTemplateM3().generateFile(verbose: verbose);
84-
// const ChipTemplateM3().generateFile(verbose: verbose);
83+
const ChipTemplateM3().generateFile(verbose: verbose);
8584
// const ColorSchemeTemplateM3().generateFile(verbose: verbose);
8685
// const DatePickerTemplateM3().generateFile(verbose: verbose);
8786
// const DialogTemplateM3().generateFile(verbose: verbose);

packages/material_ui/tool/gen_defaults/temporarily_excluded/templates/chip_template.dart renamed to packages/material_ui/tool/gen_defaults/templates/chip_template.dart

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import '../data/filter_chip.dart';
56
import 'template.dart';
67

7-
class ChipTemplate extends TokenTemplate {
8-
const ChipTemplate(
9-
super.blockName,
10-
super.fileName,
11-
super.tokens, {
12-
super.colorSchemePrefix = '_colors.',
13-
super.textThemePrefix = '_textTheme.',
14-
});
8+
class ChipTemplateM3 extends TokenTemplateM3 {
9+
const ChipTemplateM3();
1510

16-
static const String tokenGroup = 'md.comp.filter-chip';
17-
static const String variant = '.flat';
11+
@override
12+
String get name => 'Chip';
13+
14+
@override
15+
String get parentFilePath => 'chip.dart';
1816

1917
@override
20-
String generate() =>
18+
String generateContents(String className) =>
2119
'''
22-
class _${blockName}DefaultsM3 extends ChipThemeData {
23-
_${blockName}DefaultsM3(this.context, this.isEnabled)
20+
class $className extends ChipThemeData {
21+
$className(this.context, this.isEnabled)
2422
: super(
25-
elevation: ${elevation("$tokenGroup$variant.container")},
26-
shape: ${shape("$tokenGroup.container")},
23+
elevation: ${number(TokenFilterChip.flatContainerElevation)},
24+
shape: ${shape(TokenFilterChip.containerShape)},
2725
showCheckmark: true,
2826
);
2927
@@ -33,40 +31,40 @@ class _${blockName}DefaultsM3 extends ChipThemeData {
3331
late final TextTheme _textTheme = Theme.of(context).textTheme;
3432
3533
@override
36-
TextStyle? get labelStyle => ${textStyle("$tokenGroup.label-text")}?.copyWith(
34+
TextStyle? get labelStyle => ${textStyle(TokenFilterChip.labelTextType, '_textTheme')}?.copyWith(
3735
color: isEnabled
38-
? ${color("$tokenGroup.unselected.label-text.color")}
39-
: ${color("$tokenGroup.disabled.label-text.color")},
36+
? ${color(TokenFilterChip.unselectedLabelTextColor)}
37+
: ${color(TokenFilterChip.disabledLabelTextColor)},
4038
);
4139
4240
@override
4341
WidgetStateProperty<Color?>? get color => null; // Subclasses override this getter
4442
4543
@override
46-
Color? get shadowColor => ${colorOrTransparent("$tokenGroup.container.shadow-color")};
44+
Color? get shadowColor => Colors.transparent;
4745
4846
@override
49-
Color? get surfaceTintColor => ${colorOrTransparent("$tokenGroup.container.surface-tint-layer.color")};
47+
Color? get surfaceTintColor => Colors.transparent;
5048
5149
@override
5250
Color? get checkmarkColor => null;
5351
5452
@override
5553
Color? get deleteIconColor => isEnabled
56-
? ${color("$tokenGroup.with-trailing-icon.unselected.trailing-icon.color")}
57-
: ${color("$tokenGroup.with-trailing-icon.disabled.trailing-icon.color")};
54+
? ${color(TokenFilterChip.withTrailingIconUnselectedTrailingIconColor)}
55+
: ${color(TokenFilterChip.withTrailingIconDisabledTrailingIconColor)};
5856
5957
@override
6058
BorderSide? get side => isEnabled
61-
? ${border('$tokenGroup$variant.unselected.outline')}
62-
: ${border('$tokenGroup$variant.disabled.unselected.outline')};
59+
? ${border(color(TokenFilterChip.flatUnselectedOutlineColor), width: TokenFilterChip.flatUnselectedOutlineWidth)}
60+
: ${border(colorWithOpacity(TokenFilterChip.flatDisabledUnselectedOutlineColor, TokenFilterChip.flatDisabledUnselectedOutlineOpacity))};
6361
6462
@override
6563
IconThemeData? get iconTheme => IconThemeData(
6664
color: isEnabled
67-
? ${color("$tokenGroup.with-leading-icon.unselected.leading-icon.color")}
68-
: ${color("$tokenGroup.with-leading-icon.disabled.leading-icon.color")},
69-
size: ${getToken("$tokenGroup.with-icon.icon.size")},
65+
? ${color(TokenFilterChip.withLeadingIconUnselectedLeadingIconColor)}
66+
: ${color(TokenFilterChip.withLeadingIconDisabledLeadingIconColor)},
67+
size: ${number(TokenFilterChip.withIconIconSize)},
7068
);
7169
7270
@override

‎packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import '../templates/bottom_sheet_template.dart';
1818
import '../templates/button_template.dart';
1919
import '../templates/card_template.dart';
2020
import '../templates/checkbox_template.dart';
21-
// import '../templates/chip_template.dart';
21+
import '../templates/chip_template.dart';
2222
// import '../templates/color_scheme_template.dart';
2323
// import '../templates/date_picker_template.dart';
2424
// import '../templates/dialog_template.dart';
@@ -505,8 +505,19 @@ void main() {
505505
});
506506

507507
test('ChipTemplateM3 emits M3 Chip defaults from tokens', () {
508-
// Intentionally empty, will be implemented during migration. See:
509-
// https://github.com/flutter/flutter/issues/187899
508+
final String contents = const ChipTemplateM3().generateContents('_ChipDefaultsM3');
509+
expect(contents, contains('class _ChipDefaultsM3 extends ChipThemeData'));
510+
expect(contents, contains('elevation: 0.0'));
511+
expect(
512+
contents,
513+
contains(
514+
'shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0)))',
515+
),
516+
);
517+
expect(contents, contains('Color? get shadowColor => Colors.transparent'));
518+
expect(contents, contains('Color? get surfaceTintColor => Colors.transparent'));
519+
expect(contents, contains('BorderSide(color: _colors.onSurface.withOpacity(0.12))'));
520+
expect(contents, contains('size: 18.0'));
510521
});
511522

512523
test('ColorSchemeTemplateM3 emits M3 ColorScheme defaults from tokens', () {

0 commit comments

Comments
 (0)