action_chip.dart 9.69 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter/foundation.dart' show clampDouble;
6 7 8
import 'package:flutter/widgets.dart';

import 'chip.dart';
9
import 'chip_theme.dart';
10
import 'color_scheme.dart';
11
import 'colors.dart';
12
import 'debug.dart';
13
import 'material_state.dart';
14
import 'text_theme.dart';
15
import 'theme.dart';
16 17
import 'theme_data.dart';

18 19
enum _ChipVariant { flat, elevated }

20
/// A Material Design action chip.
21 22 23 24 25
///
/// Action chips are a set of options which trigger an action related to primary
/// content. Action chips should appear dynamically and contextually in a UI.
///
/// Action chips can be tapped to trigger an action or show progress and
26 27
/// confirmation. For Material 3, a disabled state is supported for Action
/// chips and is specified with [onPressed] being null. For previous versions
28
/// of Material Design, it is recommended to remove the Action chip from
29
/// the interface entirely rather than display a disabled chip.
30 31 32 33 34 35 36 37 38 39
///
/// Action chips are displayed after primary content, such as below a card or
/// persistently at the bottom of a screen.
///
/// The material button widgets, [ElevatedButton], [TextButton], and
/// [OutlinedButton], are an alternative to action chips, which should appear
/// statically and consistently in a UI.
///
/// Requires one of its ancestors to be a [Material] widget.
///
40 41 42 43 44
/// {@tool dartpad}
/// This example shows how to create an [ActionChip] with a leading icon.
/// The icon is updated when the [ActionChip] is pressed.
///
/// ** See code in examples/api/lib/material/action_chip/action_chip.0.dart **
45 46
/// {@end-tool}
///
47 48 49 50 51 52
/// ## Material Design 3
///
/// [ActionChip] can be used for both the Assist and Suggestion chips from
/// Material Design 3. If [ThemeData.useMaterial3] is true, then [ActionChip]
/// will be styled to match the Material Design 3 Assist and Suggestion chips.
///
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/// ### Creating an Assist chip
///
/// Assist chips are used to provide a quick way to perform an action.
/// To create an Action chip, set the icon property to the icon
/// that represents the action and set the label to the name of the action.
///
///
/// ### Creating a Suggestion chip
///
/// Suggestion chips usually display generated suggestions for the user,
/// like a suggested response to a message.
///
/// To create a Suggestion chip, set the label to the suggestion
/// and don't set the icon property.
//
///
69 70 71 72 73 74 75 76 77 78 79 80
/// See also:
///
///  * [Chip], a chip that displays information and can be deleted.
///  * [InputChip], a chip that represents a complex piece of information, such
///    as an entity (person, place, or thing) or conversational text, in a
///    compact form.
///  * [ChoiceChip], allows a single selection from a set of options. Choice
///    chips contain related descriptive text or categories.
///  * [CircleAvatar], which shows images or initials of people.
///  * [Wrap], A widget that displays its children in multiple horizontal or
///    vertical runs.
///  * <https://material.io/design/components/chips.html>
81
class ActionChip extends StatelessWidget implements ChipAttributes, TappableChipAttributes, DisabledChipAttributes {
82 83 84 85 86 87
  /// Create a chip that acts like a button.
  ///
  /// The [label], [onPressed], [autofocus], and [clipBehavior] arguments must
  /// not be null. The [pressElevation] and [elevation] must be null or
  /// non-negative. Typically, [pressElevation] is greater than [elevation].
  const ActionChip({
88
    super.key,
89 90 91 92
    this.avatar,
    required this.label,
    this.labelStyle,
    this.labelPadding,
93
    this.onPressed,
94 95 96 97 98 99 100
    this.pressElevation,
    this.tooltip,
    this.side,
    this.shape,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus = false,
101
    this.color,
102
    this.backgroundColor,
103
    this.disabledColor,
104 105 106 107 108
    this.padding,
    this.visualDensity,
    this.materialTapTargetSize,
    this.elevation,
    this.shadowColor,
109 110
    this.surfaceTintColor,
    this.iconTheme,
111
  }) : assert(pressElevation == null || pressElevation >= 0.0),
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
       assert(elevation == null || elevation >= 0.0),
       _chipVariant = _ChipVariant.flat;

  /// Create an elevated chip that acts like a button.
  ///
  /// The [label], [onPressed], [autofocus], and [clipBehavior] arguments must
  /// not be null. The [pressElevation] and [elevation] must be null or
  /// non-negative. Typically, [pressElevation] is greater than [elevation].
  const ActionChip.elevated({
    super.key,
    this.avatar,
    required this.label,
    this.labelStyle,
    this.labelPadding,
    this.onPressed,
    this.pressElevation,
    this.tooltip,
    this.side,
    this.shape,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus = false,
134
    this.color,
135 136 137 138 139 140 141 142 143 144 145 146
    this.backgroundColor,
    this.disabledColor,
    this.padding,
    this.visualDensity,
    this.materialTapTargetSize,
    this.elevation,
    this.shadowColor,
    this.surfaceTintColor,
    this.iconTheme,
  }) : assert(pressElevation == null || pressElevation >= 0.0),
       assert(elevation == null || elevation >= 0.0),
       _chipVariant = _ChipVariant.elevated;
147 148 149 150 151 152 153 154 155 156

  @override
  final Widget? avatar;
  @override
  final Widget label;
  @override
  final TextStyle? labelStyle;
  @override
  final EdgeInsetsGeometry? labelPadding;
  @override
157
  final VoidCallback? onPressed;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  @override
  final double? pressElevation;
  @override
  final String? tooltip;
  @override
  final BorderSide? side;
  @override
  final OutlinedBorder? shape;
  @override
  final Clip clipBehavior;
  @override
  final FocusNode? focusNode;
  @override
  final bool autofocus;
  @override
173 174
  final MaterialStateProperty<Color?>? color;
  @override
175 176
  final Color? backgroundColor;
  @override
177 178
  final Color? disabledColor;
  @override
179 180 181 182 183 184 185 186 187
  final EdgeInsetsGeometry? padding;
  @override
  final VisualDensity? visualDensity;
  @override
  final MaterialTapTargetSize? materialTapTargetSize;
  @override
  final double? elevation;
  @override
  final Color? shadowColor;
188 189 190 191 192 193 194
  @override
  final Color? surfaceTintColor;
  @override
  final IconThemeData? iconTheme;

  @override
  bool get isEnabled => onPressed != null;
195

196 197
  final _ChipVariant _chipVariant;

198 199 200
  @override
  Widget build(BuildContext context) {
    assert(debugCheckHasMaterial(context));
201
    final ChipThemeData? defaults = Theme.of(context).useMaterial3
202
      ? _ActionChipDefaultsM3(context, isEnabled, _chipVariant)
203
      : null;
204
    return RawChip(
205
      defaultProperties: defaults,
206 207 208 209 210 211
      avatar: avatar,
      label: label,
      onPressed: onPressed,
      pressElevation: pressElevation,
      tooltip: tooltip,
      labelStyle: labelStyle,
212
      color: color,
213 214 215 216 217 218
      backgroundColor: backgroundColor,
      side: side,
      shape: shape,
      clipBehavior: clipBehavior,
      focusNode: focusNode,
      autofocus: autofocus,
219
      disabledColor: disabledColor,
220 221
      padding: padding,
      visualDensity: visualDensity,
222
      isEnabled: isEnabled,
223 224 225 226
      labelPadding: labelPadding,
      materialTapTargetSize: materialTapTargetSize,
      elevation: elevation,
      shadowColor: shadowColor,
227
      surfaceTintColor: surfaceTintColor,
228 229 230
    );
  }
}
231

232
// BEGIN GENERATED TOKEN PROPERTIES - ActionChip
233

234 235 236 237
// 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.
238

239
class _ActionChipDefaultsM3 extends ChipThemeData {
240
  _ActionChipDefaultsM3(this.context, this.isEnabled, this._chipVariant)
241
    : super(
242
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
243 244 245 246 247
        showCheckmark: true,
      );

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

252 253 254 255 256 257 258 259
  @override
  double? get elevation => _chipVariant == _ChipVariant.flat
    ? 0.0
    : isEnabled ? 1.0 : 0.0;

  @override
  double? get pressElevation => 1.0;

260
  @override
261
  TextStyle? get labelStyle => _textTheme.labelLarge;
262 263

  @override
264 265 266 267 268 269 270 271 272
  MaterialStateProperty<Color?>? get color =>
    MaterialStateProperty.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        return _chipVariant == _ChipVariant.flat
          ? null
          : _colors.onSurface.withOpacity(0.12);
      }
      return null;
    });
273 274

  @override
275 276 277
  Color? get shadowColor => _chipVariant == _ChipVariant.flat
    ? Colors.transparent
    : _colors.shadow;
278 279

  @override
280
  Color? get surfaceTintColor => _colors.surfaceTint;
281 282 283 284 285 286 287 288

  @override
  Color? get checkmarkColor => null;

  @override
  Color? get deleteIconColor => null;

  @override
289 290 291 292 293
  BorderSide? get side => _chipVariant == _ChipVariant.flat
    ? isEnabled
        ? BorderSide(color: _colors.outline)
        : BorderSide(color: _colors.onSurface.withOpacity(0.12))
    : const BorderSide(color: Colors.transparent);
294 295 296 297

  @override
  IconThemeData? get iconTheme => IconThemeData(
    color: isEnabled
298 299
      ? _colors.primary
      : _colors.onSurface,
300 301 302 303 304 305 306 307 308 309 310 311 312 313
    size: 18.0,
  );

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

  /// The chip at text scale 1 starts with 8px on each side and as text scaling
  /// gets closer to 2 the label padding is linearly interpolated from 8px to 4px.
  /// Once the widget has a text scaling of 2 or higher than the label padding
  /// remains 4px.
  @override
  EdgeInsetsGeometry? get labelPadding => EdgeInsets.lerp(
    const EdgeInsets.symmetric(horizontal: 8.0),
    const EdgeInsets.symmetric(horizontal: 4.0),
314
    clampDouble(MediaQuery.textScalerOf(context).textScaleFactor - 1.0, 0.0, 1.0),
315 316 317
  )!;
}

318
// END GENERATED TOKEN PROPERTIES - ActionChip