|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import '../data/assist_chip.dart'; |
| 6 | +import 'template.dart'; |
| 7 | + |
| 8 | +class ActionChipTemplateM3 extends TokenTemplateM3 { |
| 9 | + const ActionChipTemplateM3(); |
| 10 | + @override |
| 11 | + String get name => 'Action Chip'; |
| 12 | + |
| 13 | + @override |
| 14 | + String get parentFilePath => 'action_chip.dart'; |
| 15 | + |
| 16 | + String get _colorSchemePrefix => '_colors'; |
| 17 | + |
| 18 | + String get _textThemePrefix => '_textTheme'; |
| 19 | + |
| 20 | + @override |
| 21 | + String generateContents(String className) => |
| 22 | + ''' |
| 23 | +class $className extends ChipThemeData { |
| 24 | + $className(this.context, this.isEnabled, this._chipVariant) |
| 25 | + : super( |
| 26 | + shape: ${shape(TokenAssistChip.containerShape)}, |
| 27 | + showCheckmark: true, |
| 28 | + ); |
| 29 | +
|
| 30 | + final BuildContext context; |
| 31 | + final bool isEnabled; |
| 32 | + final _ChipVariant _chipVariant; |
| 33 | + late final ColorScheme $_colorSchemePrefix = Theme.of(context).colorScheme; |
| 34 | + late final TextTheme $_textThemePrefix = Theme.of(context).textTheme; |
| 35 | +
|
| 36 | + @override |
| 37 | + double? get elevation => _chipVariant == _ChipVariant.flat |
| 38 | + ? ${TokenAssistChip.flatContainerElevation} |
| 39 | + : isEnabled ? ${TokenAssistChip.elevatedContainerElevation} : ${TokenAssistChip.elevatedDisabledContainerElevation}; |
| 40 | +
|
| 41 | + @override |
| 42 | + double? get pressElevation => ${TokenAssistChip.elevatedPressedContainerElevation}; |
| 43 | +
|
| 44 | + @override |
| 45 | + TextStyle? get labelStyle => ${textStyle(TokenAssistChip.labelTextType, _textThemePrefix)}?.copyWith( |
| 46 | + color: isEnabled |
| 47 | + ? ${color(TokenAssistChip.labelTextColor, _colorSchemePrefix)} |
| 48 | + : ${color(TokenAssistChip.disabledLabelTextColor, _colorSchemePrefix)}, |
| 49 | + ); |
| 50 | +
|
| 51 | + @override |
| 52 | + WidgetStateProperty<Color?>? get color => |
| 53 | + WidgetStateProperty.resolveWith((Set<WidgetState> states) { |
| 54 | + if (states.contains(WidgetState.disabled)) { |
| 55 | + return _chipVariant == _ChipVariant.flat |
| 56 | + ? null |
| 57 | + : ${colorWithOpacity(TokenAssistChip.elevatedDisabledContainerColor, TokenAssistChip.elevatedDisabledContainerOpacity, _colorSchemePrefix)}; |
| 58 | + } |
| 59 | + return _chipVariant == _ChipVariant.flat |
| 60 | + ? null |
| 61 | + : ${color(TokenAssistChip.elevatedContainerColor, _colorSchemePrefix)}; |
| 62 | + }); |
| 63 | +
|
| 64 | + @override |
| 65 | + Color? get shadowColor => _chipVariant == _ChipVariant.flat |
| 66 | + ? Colors.transparent |
| 67 | + : ${color(TokenAssistChip.elevatedContainerShadowColor, _colorSchemePrefix)}; |
| 68 | +
|
| 69 | + @override |
| 70 | + Color? get surfaceTintColor => Colors.transparent; |
| 71 | +
|
| 72 | + @override |
| 73 | + Color? get checkmarkColor => null; |
| 74 | +
|
| 75 | + @override |
| 76 | + Color? get deleteIconColor => null; |
| 77 | +
|
| 78 | + @override |
| 79 | + BorderSide? get side => _chipVariant == _ChipVariant.flat |
| 80 | + ? isEnabled |
| 81 | + ? ${border(color(TokenAssistChip.flatOutlineColor, _colorSchemePrefix), width: TokenAssistChip.flatOutlineWidth)} |
| 82 | + : ${border(colorWithOpacity(TokenAssistChip.flatDisabledOutlineColor, TokenAssistChip.flatDisabledOutlineOpacity, _colorSchemePrefix))} |
| 83 | + : const BorderSide(color: Colors.transparent); |
| 84 | +
|
| 85 | + @override |
| 86 | + IconThemeData? get iconTheme => IconThemeData( |
| 87 | + color: isEnabled |
| 88 | + ? ${color(TokenAssistChip.withIconIconColor, _colorSchemePrefix)} |
| 89 | + : ${color(TokenAssistChip.withIconDisabledIconColor, _colorSchemePrefix)}, |
| 90 | + size: ${TokenAssistChip.withIconIconSize}, |
| 91 | + ); |
| 92 | +
|
| 93 | + @override |
| 94 | + EdgeInsetsGeometry? get padding => const EdgeInsets.all(8.0); |
| 95 | +
|
| 96 | + /// The label padding of the chip scales with the font size specified in the |
| 97 | + /// [labelStyle], and the system font size settings that scale font sizes |
| 98 | + /// globally. |
| 99 | + /// |
| 100 | + /// The chip at effective font size 14.0 starts with 8px on each side and as |
| 101 | + /// the font size scales up to closer to 28.0, the label padding is linearly |
| 102 | + /// interpolated from 8px to 4px. Once the label has a font size of 2 or |
| 103 | + /// higher, label padding remains 4px. |
| 104 | + @override |
| 105 | + EdgeInsetsGeometry? get labelPadding { |
| 106 | + final double fontSize = labelStyle?.fontSize ?? 14.0; |
| 107 | + final double fontSizeRatio = MediaQuery.textScalerOf(context).scale(fontSize) / 14.0; |
| 108 | + return EdgeInsets.lerp( |
| 109 | + const EdgeInsets.symmetric(horizontal: 8.0), |
| 110 | + const EdgeInsets.symmetric(horizontal: 4.0), |
| 111 | + clampDouble(fontSizeRatio - 1.0, 0.0, 1.0), |
| 112 | + )!; |
| 113 | + } |
| 114 | +} |
| 115 | +'''; |
| 116 | +} |
0 commit comments