Skip to content

Commit f0b2c78

Browse files
authored
[material_ui] Update gen_defaults color helper (flutter#12846)
Work toward flutter#187899. This PR is to update the `colorWithOpacity` helper to preserve existing Material 3 generated defaults by continuing to emit `withOpacity(...)`, while Material 3 Expressive generated defaults emit `withValues(alpha: ...)` to avoid introducing new deprecated `Color.withOpacity` usages. ## Pre-Review Checklist
1 parent 87910ce commit f0b2c78

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎packages/material_ui/tool/gen_defaults/templates/template.dart‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,27 @@ abstract class TokenTemplate {
102102
String number(num value) => value.toString();
103103

104104
/// Generates a [ColorScheme] color expression for the given token.
105-
String color(TokenColorRole role, String prefix) => '$prefix.${role.name}';
105+
String color(TokenColorRole role, String prefix) {
106+
final String colorName = switch (role) {
107+
TokenColorRole.inverseOnSurface => 'onInverseSurface',
108+
_ => role.name,
109+
};
110+
return '$prefix.$colorName';
111+
}
106112

107113
/// Generates a color expression with opacity applied.
108114
String colorWithOpacity(TokenColorRole role, double opacity, String prefix) {
115+
final String colorExpression = color(role, prefix);
109116
if (opacity == 1.0) {
110-
return color(role, prefix);
117+
return colorExpression;
111118
}
112-
return '${color(role, prefix)}.withOpacity(${number(opacity)})';
119+
final String opacityValue = number(opacity);
120+
return switch (_version) {
121+
// TODO(QuncCccccc): Update M3 defaults to use withValues(alpha:) once
122+
// all existing M3 templates have migrated to the new generator.
123+
_MaterialVersion.material3 => '$colorExpression.withOpacity($opacityValue)',
124+
_MaterialVersion.material3Expressive => '$colorExpression.withValues(alpha: $opacityValue)',
125+
};
113126
}
114127

115128
/// Generate a [BorderSide] for the given component.

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ void main() {
115115
test('color generates color expression', () {
116116
final template = IconButtonTemplateM3(testPath());
117117
expect(template.color(TokenColorRole.onSurface, '_colors'), '_colors.onSurface');
118+
expect(
119+
template.color(TokenColorRole.inverseOnSurface, '_colors'),
120+
'_colors.onInverseSurface',
121+
);
118122
});
119123

120124
test('textStyle generates text name', () {
@@ -129,18 +133,40 @@ void main() {
129133
);
130134
});
131135

132-
test('colorWithOpacity generates color expression with opacity', () {
136+
// M3 templates intentionally keep the deprecated withOpacity output until
137+
// all existing M3 templates have migrated to the new generator.
138+
test('M3 colorWithOpacity generates color expression with opacity', () {
133139
final template = IconButtonTemplateM3(testPath());
134140
expect(
135141
template.colorWithOpacity(TokenColorRole.onSurface, 0.12, '_colors'),
136142
'_colors.onSurface.withOpacity(0.12)',
137143
);
144+
expect(
145+
template.colorWithOpacity(TokenColorRole.inverseOnSurface, 0.12, '_colors'),
146+
'_colors.onInverseSurface.withOpacity(0.12)',
147+
);
138148
expect(
139149
template.colorWithOpacity(TokenColorRole.onSurface, 1.0, '_colors'),
140150
'_colors.onSurface',
141151
);
142152
});
143153

154+
test('M3E colorWithOpacity uses withValues', () {
155+
final template = IconButtonTemplateM3E(testPath());
156+
expect(
157+
template.colorWithOpacity(TokenColorRole.onSurface, 0.12, '_colors'),
158+
'_colors.onSurface.withValues(alpha: 0.12)',
159+
);
160+
expect(
161+
template.colorWithOpacity(TokenColorRole.inverseOnSurface, 0.12, '_colors'),
162+
'_colors.onInverseSurface.withValues(alpha: 0.12)',
163+
);
164+
expect(
165+
template.colorWithOpacity(TokenColorRole.inverseOnSurface, 1.0, '_colors'),
166+
'_colors.onInverseSurface',
167+
);
168+
});
169+
144170
test('border generates border expression', () {
145171
final template = IconButtonTemplateM3(testPath());
146172
expect(template.border('_colors.outline'), 'BorderSide(color: _colors.outline)');

0 commit comments

Comments
 (0)