action_chip_template.dart 3.94 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'template.dart';

7
class ActionChipTemplate extends TokenTemplate {
8 9 10 11
  const ActionChipTemplate(super.blockName, super.fileName, super.tokens, {
    super.colorSchemePrefix = '_colors.',
    super.textThemePrefix = '_textTheme.'
  });
12 13

  static const String tokenGroup = 'md.comp.assist-chip';
14 15
  static const String flatVariant = '.flat';
  static const String elevatedVariant = '.elevated';
16 17 18

  @override
  String generate() => '''
19
class _${blockName}DefaultsM3 extends ChipThemeData {
20
  _${blockName}DefaultsM3(this.context, this.isEnabled, this._chipVariant)
21 22 23 24 25 26 27
    : super(
        shape: ${shape("$tokenGroup.container")},
        showCheckmark: true,
      );

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

32 33 34 35 36 37 38 39
  @override
  double? get elevation => _chipVariant == _ChipVariant.flat
    ? ${elevation("$tokenGroup$flatVariant.container")}
    : isEnabled ? ${elevation("$tokenGroup$elevatedVariant.container")} : ${elevation("$tokenGroup$elevatedVariant.disabled.container")};

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

40
  @override
41 42 43 44 45
  TextStyle? get labelStyle => ${textStyle("$tokenGroup.label-text")}?.copyWith(
    color: isEnabled
      ? ${color("$tokenGroup.label-text.color")}
      : ${color("$tokenGroup.disabled.label-text.color")},
  );
46 47

  @override
48 49 50 51 52 53 54
  MaterialStateProperty<Color?>? get color =>
    MaterialStateProperty.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        return _chipVariant == _ChipVariant.flat
          ? ${componentColor("$tokenGroup$flatVariant.disabled.container")}
          : ${componentColor("$tokenGroup$elevatedVariant.disabled.container")};
      }
55
      return ${componentColor("$tokenGroup$flatVariant.container")};
56
    });
57 58

  @override
59 60 61
  Color? get shadowColor => _chipVariant == _ChipVariant.flat
    ? ${colorOrTransparent("$tokenGroup$flatVariant.container.shadow-color")}
    : ${colorOrTransparent("$tokenGroup$elevatedVariant.container.shadow-color")};
62 63

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

  @override
67
  Color? get checkmarkColor => null;
68 69

  @override
70
  Color? get deleteIconColor => null;
71 72

  @override
73 74 75 76 77
  BorderSide? get side => _chipVariant == _ChipVariant.flat
    ? isEnabled
        ? ${border('$tokenGroup$flatVariant.outline')}
        : ${border('$tokenGroup$flatVariant.disabled.outline')}
    : const BorderSide(color: Colors.transparent);
78 79 80 81 82 83

  @override
  IconThemeData? get iconTheme => IconThemeData(
    color: isEnabled
      ? ${color("$tokenGroup.with-icon.icon.color")}
      : ${color("$tokenGroup.with-icon.disabled.icon.color")},
84
    size: ${getToken("$tokenGroup.with-icon.icon.size")},
85 86 87 88 89
  );

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

90 91 92 93 94 95 96 97
  /// 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.
98
  @override
99 100 101 102 103 104 105 106 107
  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),
    )!;
  }
108 109 110
}
''';
}