Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 2 additions & 104 deletions packages/material_ui/lib/src/action_chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import 'text_theme.dart';
import 'theme.dart';
import 'theme_data.dart';

part 'generated/action_chip_defaults_m3.g.dart';

enum _ChipVariant { flat, elevated }

/// A Material Design action chip.
Expand Down Expand Up @@ -255,107 +257,3 @@ class ActionChip extends StatelessWidget
);
}
}

// BEGIN GENERATED TOKEN PROPERTIES - ActionChip

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
// dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
class _ActionChipDefaultsM3 extends ChipThemeData {
_ActionChipDefaultsM3(this.context, this.isEnabled, this._chipVariant)
: super(
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
showCheckmark: true,
);

final BuildContext context;
final bool isEnabled;
final _ChipVariant _chipVariant;
late final ColorScheme _colors = Theme.of(context).colorScheme;
late final TextTheme _textTheme = Theme.of(context).textTheme;

@override
double? get elevation => _chipVariant == _ChipVariant.flat
? 0.0
: isEnabled ? 1.0 : 0.0;

@override
double? get pressElevation => 1.0;

@override
TextStyle? get labelStyle => _textTheme.labelLarge?.copyWith(
color: isEnabled
? _colors.onSurface
: _colors.onSurface,
);

@override
WidgetStateProperty<Color?>? get color =>
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return _chipVariant == _ChipVariant.flat
? null
: _colors.onSurface.withOpacity(0.12);
}
return _chipVariant == _ChipVariant.flat
? null
: _colors.surfaceContainerLow;
});

@override
Color? get shadowColor => _chipVariant == _ChipVariant.flat
? Colors.transparent
: _colors.shadow;

@override
Color? get surfaceTintColor => Colors.transparent;

@override
Color? get checkmarkColor => null;

@override
Color? get deleteIconColor => null;

@override
BorderSide? get side => _chipVariant == _ChipVariant.flat
? isEnabled
? BorderSide(color: _colors.outlineVariant)
: BorderSide(color: _colors.onSurface.withOpacity(0.12))
: const BorderSide(color: Colors.transparent);

@override
IconThemeData? get iconTheme => IconThemeData(
color: isEnabled
? _colors.primary
: _colors.onSurface,
size: 18.0,
);

@override
EdgeInsetsGeometry? get padding => const EdgeInsets.all(8.0);

/// The label padding of the chip scales with the font size specified in the
/// [labelStyle], and the system font size settings that scale font sizes
/// globally.
///
/// The chip at effective font size 14.0 starts with 8px on each side and as
/// the font size scales up to closer to 28.0, the label padding is linearly
/// interpolated from 8px to 4px. Once the label has a font size of 2 or
/// higher, label padding remains 4px.
@override
EdgeInsetsGeometry? get labelPadding {
final double fontSize = labelStyle?.fontSize ?? 14.0;
final double fontSizeRatio = MediaQuery.textScalerOf(context).scale(fontSize) / 14.0;
return EdgeInsets.lerp(
const EdgeInsets.symmetric(horizontal: 8.0),
const EdgeInsets.symmetric(horizontal: 4.0),
clampDouble(fontSizeRatio - 1.0, 0.0, 1.0),
)!;
}
}
// dart format on

// END GENERATED TOKEN PROPERTIES - ActionChip
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Do not edit by hand. The code is generated from data in the Material
// Design token database by the script:
// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart.
part of '../action_chip.dart';

class _ActionChipDefaultsM3 extends ChipThemeData {
_ActionChipDefaultsM3(this.context, this.isEnabled, this._chipVariant)
: super(
Expand All @@ -16,12 +21,11 @@ class _ActionChipDefaultsM3 extends ChipThemeData {
late final TextTheme _textTheme = Theme.of(context).textTheme;

@override
double? get elevation =>
_chipVariant == _ChipVariant.flat
? 0.0
: isEnabled
? 1.0
: 0.0;
double? get elevation => _chipVariant == _ChipVariant.flat
? 0.0
: isEnabled
? 1.0
: 0.0;

@override
double? get pressElevation => 1.0;
Expand Down Expand Up @@ -52,12 +56,11 @@ class _ActionChipDefaultsM3 extends ChipThemeData {
Color? get deleteIconColor => null;

@override
BorderSide? get side =>
_chipVariant == _ChipVariant.flat
? isEnabled
? BorderSide(color: _colors.outlineVariant)
: BorderSide(color: _colors.onSurface.withOpacity(0.12))
: const BorderSide(color: Colors.transparent);
BorderSide? get side => _chipVariant == _ChipVariant.flat
? isEnabled
? BorderSide(color: _colors.outlineVariant)
: BorderSide(color: _colors.onSurface.withOpacity(0.12))
: const BorderSide(color: Colors.transparent);

@override
IconThemeData? get iconTheme =>
Expand Down
2 changes: 2 additions & 0 deletions packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import 'package:args/args.dart';

import '../templates/action_chip_template.dart';
import '../templates/app_bar_template.dart';

Future<void> main(List<String> args) async {
Expand All @@ -21,5 +22,6 @@ Future<void> main(List<String> args) async {
final ArgResults argResults = parser.parse(args);
// TODO(elliette): Add token logger when verbose flag is used.
final verbose = argResults['verbose'] as bool;
const ActionChipTemplateM3().generateFile(verbose: verbose);
const AppBarTemplateM3().generateFile(verbose: verbose);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../data/assist_chip.dart';
import 'template.dart';

class ActionChipTemplate extends TokenTemplate {
const ActionChipTemplate(
super.blockName,
super.fileName,
super.tokens, {
super.colorSchemePrefix = '_colors.',
super.textThemePrefix = '_textTheme.',
class ActionChipTemplateM3 extends TokenTemplateM3 {
const ActionChipTemplateM3({
this.colorSchemePrefix = '_colors',
});

static const String tokenGroup = 'md.comp.assist-chip';
static const String flatVariant = '.flat';
static const String elevatedVariant = '.elevated';
final String colorSchemePrefix;

@override
String generate() => '''
class _${blockName}DefaultsM3 extends ChipThemeData {
_${blockName}DefaultsM3(this.context, this.isEnabled, this._chipVariant)
String get name => 'Action Chip';

@override
String get parentFilePath => 'action_chip.dart';

@override
String generateContents(String className) =>
'''
class $className extends ChipThemeData {
$className(this.context, this.isEnabled, this._chipVariant)
: super(
shape: ${shape("$tokenGroup.container")},
shape: ${shape(TokenAssistChip.containerShape)},
showCheckmark: true,
);

Expand All @@ -34,39 +36,39 @@ class _${blockName}DefaultsM3 extends ChipThemeData {

@override
double? get elevation => _chipVariant == _ChipVariant.flat
? ${elevation("$tokenGroup$flatVariant.container")}
: isEnabled ? ${elevation("$tokenGroup$elevatedVariant.container")} : ${elevation("$tokenGroup$elevatedVariant.disabled.container")};
? ${TokenAssistChip.flatContainerElevation}
: isEnabled ? ${TokenAssistChip.elevatedContainerElevation} : ${TokenAssistChip.elevatedDisabledContainerElevation};

@override
double? get pressElevation => ${elevation("$tokenGroup$elevatedVariant.pressed.container")};
double? get pressElevation => ${TokenAssistChip.elevatedPressedContainerElevation};

@override
TextStyle? get labelStyle => ${textStyle("$tokenGroup.label-text")}?.copyWith(
TextStyle? get labelStyle => _textTheme.labelLarge?.copyWith(
color: isEnabled
? ${color("$tokenGroup.label-text.color")}
: ${color("$tokenGroup.disabled.label-text.color")},
? ${color(TokenAssistChip.labelTextColor, colorSchemePrefix)}
: ${color(TokenAssistChip.disabledLabelTextColor, colorSchemePrefix)},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The disabled label color is missing its opacity. According to the token data in assist_chip.dart, disabledLabelTextOpacity should be applied to the color for the disabled state.

Suggested change
: ${color(TokenAssistChip.disabledLabelTextColor, colorSchemePrefix)},
: ${colorWithOpacity(TokenAssistChip.disabledLabelTextColor, TokenAssistChip.disabledLabelTextOpacity, colorSchemePrefix)},

);

@override
WidgetStateProperty<Color?>? get color =>
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.disabled)) {
return _chipVariant == _ChipVariant.flat
? ${componentColor("$tokenGroup$flatVariant.disabled.container")}
: ${componentColor("$tokenGroup$elevatedVariant.disabled.container")};
? null
: ${colorWithOpacity(TokenAssistChip.elevatedDisabledContainerColor, TokenAssistChip.elevatedDisabledContainerOpacity, colorSchemePrefix)};
}
return _chipVariant == _ChipVariant.flat
? ${componentColor("$tokenGroup$flatVariant.container")}
: ${componentColor("$tokenGroup$elevatedVariant.container")};
? null
: ${color(TokenAssistChip.elevatedContainerColor, colorSchemePrefix)};
});

@override
Color? get shadowColor => _chipVariant == _ChipVariant.flat
? ${colorOrTransparent("$tokenGroup$flatVariant.container.shadow-color")}
: ${colorOrTransparent("$tokenGroup$elevatedVariant.container.shadow-color")};
? Colors.transparent
: ${color(TokenAssistChip.elevatedContainerShadowColor, colorSchemePrefix)};

@override
Color? get surfaceTintColor => ${colorOrTransparent("$tokenGroup.container.surface-tint-layer.color")};
Color? get surfaceTintColor => Colors.transparent;

@override
Color? get checkmarkColor => null;
Expand All @@ -77,16 +79,16 @@ class _${blockName}DefaultsM3 extends ChipThemeData {
@override
BorderSide? get side => _chipVariant == _ChipVariant.flat
? isEnabled
? ${border('$tokenGroup$flatVariant.outline')}
: ${border('$tokenGroup$flatVariant.disabled.outline')}
? ${border(color(TokenAssistChip.flatOutlineColor, colorSchemePrefix), width: TokenAssistChip.flatOutlineWidth)}
: ${border(colorWithOpacity(TokenAssistChip.flatDisabledOutlineColor, TokenAssistChip.flatDisabledOutlineOpacity, colorSchemePrefix))}
: const BorderSide(color: Colors.transparent);

@override
IconThemeData? get iconTheme => IconThemeData(
color: isEnabled
? ${color("$tokenGroup.with-icon.icon.color")}
: ${color("$tokenGroup.with-icon.disabled.icon.color")},
size: ${getToken("$tokenGroup.with-icon.icon.size")},
? ${color(TokenAssistChip.withIconIconColor, colorSchemePrefix)}
: ${color(TokenAssistChip.withIconDisabledIconColor, colorSchemePrefix)},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The disabled icon color is missing its opacity. According to the token data in assist_chip.dart, withIconDisabledIconOpacity should be applied to the color for the disabled state.

Suggested change
: ${color(TokenAssistChip.withIconDisabledIconColor, colorSchemePrefix)},
: ${colorWithOpacity(TokenAssistChip.withIconDisabledIconColor, TokenAssistChip.withIconDisabledIconOpacity, colorSchemePrefix)},

size: ${TokenAssistChip.withIconIconSize},
);

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ abstract class TokenTemplate {
}
return '${color(role, prefix)}.withOpacity(${number(opacity)})';
}

/// Generate a [BorderSide] for the given component.
String border(String color, {double? width}) {
final widthString = (width != null && width != 1.0) ? ', width: $width' : '';
return 'BorderSide(color: $color$widthString)';
}

/// Generates an [OutlinedBorder] expression for a shape token.
///
Expand Down
22 changes: 22 additions & 0 deletions packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:io';
import 'package:test/test.dart';
import '../data/color_role.dart';
import '../data/shape_struct.dart';
import '../templates/action_chip_template.dart';
import '../templates/app_bar_template.dart';
import '../templates/template.dart';
import 'test_fixtures/test_templates.dart';
Expand Down Expand Up @@ -159,6 +160,27 @@ void main() {
expect(contents, contains('static const double expandedHeight = 112.0'));
expect(contents, contains('static const double expandedHeight = 152.0'));
});

test('ActionChipTemplateM3 emits M3 ActionChip defaults from assist chip tokens', () {
final String contents = const ActionChipTemplateM3().generateContents(
'_ActionChipDefaultsM3',
);
expect(contents, contains('class _ActionChipDefaultsM3 extends ChipThemeData'));
expect(
contents,
contains(
'shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0)))',
),
);
expect(contents, contains('showCheckmark: true'));
expect(contents, contains('double? get elevation => _chipVariant == _ChipVariant.flat'));
expect(contents, contains('? 0.0'));
expect(contents, contains(': isEnabled ? 1.0 : 0.0;'));
expect(contents, contains('double? get pressElevation => 1.0;'));
expect(contents, contains('_colors.onSurface.withOpacity(0.12)'));
expect(contents, contains('size: 18.0'));
});

test('will run dart format over the generated file', () {
final template = UnformattedTemplate(testPath());
template.generateFile();
Expand Down