[material_ui] Update gen_defaults color helper (#12846)
Work toward flutter/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
diff --git a/packages/material_ui/tool/gen_defaults/templates/template.dart b/packages/material_ui/tool/gen_defaults/templates/template.dart
index a2ddba2..91101fc 100644
--- a/packages/material_ui/tool/gen_defaults/templates/template.dart
+++ b/packages/material_ui/tool/gen_defaults/templates/template.dart
@@ -102,14 +102,27 @@
String number(num value) => value.toString();
/// Generates a [ColorScheme] color expression for the given token.
- String color(TokenColorRole role, String prefix) => '$prefix.${role.name}';
+ String color(TokenColorRole role, String prefix) {
+ final String colorName = switch (role) {
+ TokenColorRole.inverseOnSurface => 'onInverseSurface',
+ _ => role.name,
+ };
+ return '$prefix.$colorName';
+ }
/// Generates a color expression with opacity applied.
String colorWithOpacity(TokenColorRole role, double opacity, String prefix) {
+ final String colorExpression = color(role, prefix);
if (opacity == 1.0) {
- return color(role, prefix);
+ return colorExpression;
}
- return '${color(role, prefix)}.withOpacity(${number(opacity)})';
+ final String opacityValue = number(opacity);
+ return switch (_version) {
+ // TODO(QuncCccccc): Update M3 defaults to use withValues(alpha:) once
+ // all existing M3 templates have migrated to the new generator.
+ _MaterialVersion.material3 => '$colorExpression.withOpacity($opacityValue)',
+ _MaterialVersion.material3Expressive => '$colorExpression.withValues(alpha: $opacityValue)',
+ };
}
/// Generate a [BorderSide] for the given component.
diff --git a/packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart b/packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart
index 58c4b86..8ff0ad0 100644
--- a/packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart
+++ b/packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart
@@ -115,6 +115,10 @@
test('color generates color expression', () {
final template = IconButtonTemplateM3(testPath());
expect(template.color(TokenColorRole.onSurface, '_colors'), '_colors.onSurface');
+ expect(
+ template.color(TokenColorRole.inverseOnSurface, '_colors'),
+ '_colors.onInverseSurface',
+ );
});
test('textStyle generates text name', () {
@@ -129,18 +133,40 @@
);
});
- test('colorWithOpacity generates color expression with opacity', () {
+ // M3 templates intentionally keep the deprecated withOpacity output until
+ // all existing M3 templates have migrated to the new generator.
+ test('M3 colorWithOpacity generates color expression with opacity', () {
final template = IconButtonTemplateM3(testPath());
expect(
template.colorWithOpacity(TokenColorRole.onSurface, 0.12, '_colors'),
'_colors.onSurface.withOpacity(0.12)',
);
expect(
+ template.colorWithOpacity(TokenColorRole.inverseOnSurface, 0.12, '_colors'),
+ '_colors.onInverseSurface.withOpacity(0.12)',
+ );
+ expect(
template.colorWithOpacity(TokenColorRole.onSurface, 1.0, '_colors'),
'_colors.onSurface',
);
});
+ test('M3E colorWithOpacity uses withValues', () {
+ final template = IconButtonTemplateM3E(testPath());
+ expect(
+ template.colorWithOpacity(TokenColorRole.onSurface, 0.12, '_colors'),
+ '_colors.onSurface.withValues(alpha: 0.12)',
+ );
+ expect(
+ template.colorWithOpacity(TokenColorRole.inverseOnSurface, 0.12, '_colors'),
+ '_colors.onInverseSurface.withValues(alpha: 0.12)',
+ );
+ expect(
+ template.colorWithOpacity(TokenColorRole.inverseOnSurface, 1.0, '_colors'),
+ '_colors.onInverseSurface',
+ );
+ });
+
test('border generates border expression', () {
final template = IconButtonTemplateM3(testPath());
expect(template.border('_colors.outline'), 'BorderSide(color: _colors.outline)');