Repository navigation
Expand file tree
/
Copy pathchip.dart
More file actions
2520 lines (2345 loc) · 87.5 KB
/
Copy pathchip.dart
File metadata and controls
2520 lines (2345 loc) · 87.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'action_chip.dart';
/// @docImport 'app.dart';
/// @docImport 'choice_chip.dart';
/// @docImport 'circle_avatar.dart';
/// @docImport 'filter_chip.dart';
/// @docImport 'input_chip.dart';
/// @docImport 'scaffold.dart';
library;
import 'dart:math' as math;
import 'package:flutter/foundation.dart' show clampDouble, kIsWeb;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'chip_theme.dart';
import 'color_scheme.dart';
import 'colors.dart';
import 'constants.dart';
import 'debug.dart';
import 'icons.dart';
import 'ink_decoration.dart';
import 'ink_well.dart';
import 'material.dart';
import 'material_localizations.dart';
import 'text_theme.dart';
import 'theme.dart';
import 'theme_data.dart';
import 'tooltip.dart';
part 'generated/chip_defaults_m3.g.dart';
// Some design constants
const double _kChipHeight = 32.0;
const int _kCheckmarkAlpha = 0xde; // 87%
const int _kDisabledAlpha = 0x61; // 38%
const double _kCheckmarkStrokeWidth = 2.0;
const Duration _kSelectDuration = Duration(milliseconds: 195);
const Duration _kCheckmarkDuration = Duration(milliseconds: 150);
const Duration _kCheckmarkReverseDuration = Duration(milliseconds: 50);
const Duration _kDrawerDuration = Duration(milliseconds: 150);
const Duration _kReverseDrawerDuration = Duration(milliseconds: 100);
const Duration _kDisableDuration = Duration(milliseconds: 75);
const Color _kSelectScrimColor = Color(0x60191919);
const Icon _kDefaultDeleteIcon = Icon(Icons.cancel);
/// An interface defining the base attributes for a Material Design chip.
///
/// Chips are compact elements that represent an attribute, text, entity, or
/// action.
///
/// The defaults mentioned in the documentation for each attribute are what
/// the implementing classes typically use for defaults (but this class doesn't
/// provide or enforce them).
///
/// 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.
/// * [FilterChip], uses tags or descriptive words as a way to filter content.
/// * [ActionChip], represents an action related to primary content.
/// * <https://material.io/design/components/chips.html>
abstract interface class ChipAttributes {
/// The primary content of the chip.
///
/// Typically a [Text] widget.
Widget get label;
/// A widget to display prior to the chip's label.
///
/// Typically a [CircleAvatar] widget.
Widget? get avatar;
/// The style to be applied to the chip's label.
///
/// If this is null and [ThemeData.useMaterial3] is true, then
/// [TextTheme.labelLarge] is used. Otherwise, [TextTheme.bodyLarge]
/// is used.
//
/// This only has an effect on widgets that respect the [DefaultTextStyle],
/// such as [Text].
///
/// If [TextStyle.color] is a [WidgetStateProperty<Color>], [WidgetStateProperty.resolve]
/// is used for the following [WidgetState]s:
///
/// * [WidgetState.disabled].
/// * [WidgetState.selected].
/// * [WidgetState.hovered].
/// * [WidgetState.focused].
/// * [WidgetState.pressed].
TextStyle? get labelStyle;
/// The color and weight of the chip's outline.
///
/// Defaults to the border side in the ambient [ChipThemeData]. If the theme
/// border side resolves to null and [ThemeData.useMaterial3] is true, then
/// [BorderSide] with a [ColorScheme.outline] color is used when the chip is
/// enabled, and [BorderSide] with a [ColorScheme.onSurface] color with an
/// opacity of 0.12 is used when the chip is disabled. Otherwise, it defaults
/// to null.
///
/// This value is combined with [shape] to create a shape decorated with an
/// outline. To omit the outline entirely, pass [BorderSide.none] to [side].
///
/// If it is a [WidgetStateBorderSide], [WidgetStateProperty.resolve] is
/// used for the following [WidgetState]s:
///
/// * [WidgetState.disabled].
/// * [WidgetState.selected].
/// * [WidgetState.hovered].
/// * [WidgetState.focused].
/// * [WidgetState.pressed].
BorderSide? get side;
/// The [OutlinedBorder] to draw around the chip.
///
/// Defaults to the shape in the ambient [ChipThemeData]. If the theme
/// shape resolves to null and [ThemeData.useMaterial3] is true, then
/// [RoundedRectangleBorder] with a circular border radius of 8.0 is used.
/// Otherwise, [StadiumBorder] is used.
///
/// This shape is combined with [side] to create a shape decorated with an
/// outline. If [side] is not null or side of [shape] is [BorderSide.none],
/// side of [shape] is ignored. To omit the outline entirely,
/// pass [BorderSide.none] to [side].
///
/// If it is a [WidgetStateOutlinedBorder], [WidgetStateProperty.resolve]
/// is used for the following [WidgetState]s:
///
/// * [WidgetState.disabled].
/// * [WidgetState.selected].
/// * [WidgetState.hovered].
/// * [WidgetState.focused].
/// * [WidgetState.pressed].
OutlinedBorder? get shape;
/// {@macro cupertino_ui.Material.clipBehavior}
///
/// Defaults to [Clip.none].
Clip get clipBehavior;
/// {@macro flutter.widgets.Focus.focusNode}
FocusNode? get focusNode;
/// {@macro flutter.widgets.Focus.autofocus}
bool get autofocus;
/// The color that fills the chip, in all [WidgetState]s.
///
/// Defaults to null.
///
/// Resolves in the following states:
/// * [WidgetState.selected].
/// * [WidgetState.disabled].
WidgetStateProperty<Color?>? get color;
/// Color to be used for the unselected, enabled chip's background.
///
/// The default is light grey.
Color? get backgroundColor;
/// The padding between the contents of the chip and the outside [shape].
///
/// If this is null and [ThemeData.useMaterial3] is true, then
/// a padding of 8.0 logical pixels on all sides is used. Otherwise,
/// it defaults to a padding of 4.0 logical pixels on all sides.
EdgeInsetsGeometry? get padding;
/// Defines how compact the chip's layout will be.
///
/// Chips are unaffected by horizontal density changes.
///
/// {@macro material_ui.themedata.visualDensity}
///
/// See also:
///
/// * [ThemeData.visualDensity], which specifies the [visualDensity] for all
/// widgets within a [Theme].
VisualDensity? get visualDensity;
/// The padding around the [label] widget.
///
/// By default, this is 4 logical pixels at the beginning and the end of the
/// label, and zero on top and bottom.
EdgeInsetsGeometry? get labelPadding;
/// Configures the minimum size of the tap target.
///
/// Defaults to [ThemeData.materialTapTargetSize].
///
/// See also:
///
/// * [MaterialTapTargetSize], for a description of how this affects tap targets.
MaterialTapTargetSize? get materialTapTargetSize;
/// Elevation to be applied on the chip relative to its parent.
///
/// This controls the size of the shadow below the chip.
///
/// Defaults to 0. The value is always non-negative.
double? get elevation;
/// Color of the chip's shadow when the elevation is greater than 0.
///
/// If this is null and [ThemeData.useMaterial3] is true, then
/// [Colors.transparent] color is used. Otherwise, it defaults to null.
Color? get shadowColor;
/// Color of the chip's surface tint overlay when its elevation is
/// greater than 0.
///
/// This is not recommended for use. [Material 3 spec](https://m3.material.io/styles/color/the-color-system/color-roles)
/// introduced a set of tone-based surfaces and surface containers in its [ColorScheme],
/// which provide more flexibility. The intention is to eventually remove surface tint color from
/// the framework.
///
/// If this is null, defaults to [Colors.transparent].
Color? get surfaceTintColor;
/// Theme used for all icons in the chip.
///
/// If this is null and [ThemeData.useMaterial3] is true, then [IconThemeData]
/// with a [ColorScheme.primary] color and a size of 18.0 is used when
/// the chip is enabled, and [IconThemeData] with a [ColorScheme.onSurface]
/// color and a size of 18.0 is used when the chip is disabled. Otherwise,
/// it defaults to null.
IconThemeData? get iconTheme;
/// Optional size constraints for the avatar.
///
/// When unspecified, defaults to a minimum size of chip height or label height
/// (whichever is greater) and a padding of 8.0 pixels on all sides.
///
/// The default constraints ensure that the avatar is accessible.
/// Specifying this parameter enables creation of avatar smaller than
/// the minimum size, but it is not recommended.
///
/// <callout-box>
///
/// This sample shows how to use [avatarBoxConstraints] to adjust avatar size constraints
///
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/chip/chip_attributes.avatar_box_constraints.0.dart#body}
///
/// </callout-box>
BoxConstraints? get avatarBoxConstraints;
/// Used to override the default chip animations durations.
///
/// If [ChipAnimationStyle.enableAnimation] with duration or reverse duration is
/// provided, it will be used to override the chip enable and disable animation durations.
/// If it is null, then default duration will be 75ms.
///
/// If [ChipAnimationStyle.selectAnimation] with duration or reverse duration is provided,
/// it will be used to override the chip select and unselect animation durations.
/// If it is null, then default duration will be 195ms.
///
/// If [ChipAnimationStyle.avatarDrawerAnimation] with duration or reverse duration
/// is provided, it will be used to override the chip checkmark animation duration.
/// If it is null, then default duration will be 150ms.
///
/// If [ChipAnimationStyle.deleteDrawerAnimation] with duration or reverse duration
/// is provided, it will be used to override the chip delete icon animation duration.
/// If it is null, then default duration will be 150ms.
///
/// <callout-box>
///
/// This sample showcases how to override the chip animations durations using
/// [ChipAnimationStyle].
///
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/chip/chip_attributes.chip_animation_style.0.dart#body}
///
/// </callout-box>
ChipAnimationStyle? get chipAnimationStyle;
/// The cursor for a mouse pointer when it enters or is hovering over the
/// widget.
///
/// If [mouseCursor] is a [WidgetStateMouseCursor],
/// [WidgetStateProperty.resolve] is used for the following [WidgetState]s:
///
/// * [WidgetState.hovered].
/// * [WidgetState.focused].
/// * [WidgetState.disabled].
///
/// If this property is null, [WidgetStateMouseCursor.adaptiveClickable] will be used.
MouseCursor? get mouseCursor;
}
/// An interface for Material Design chips that can be deleted.
///
/// The defaults mentioned in the documentation for each attribute are what
/// the implementing classes typically use for defaults (but this class doesn't
/// provide or enforce them).
///
/// 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.
/// * <https://material.io/design/components/chips.html>
abstract interface class DeletableChipAttributes {
/// The icon displayed when [onDeleted] is set.
///
/// If [deleteIconColor] is provided, it will be used as the color of the
/// delete icon. If [deleteIconColor] is null, then the icon will use the
/// color specified in the chip [IconTheme]. If the [IconTheme] is null, then
/// the icon will use the color specified in the [ThemeData.iconTheme].
///
/// If a size is specified in the chip [IconTheme], then the delete icon will
/// use that size. Otherwise, defaults to 18 pixels.
///
/// Defaults to an [Icon] widget set to use [Icons.clear].
/// If [ThemeData.useMaterial3] is false, then defaults to an [Icon] widget
/// set to use [Icons.cancel].
Widget? get deleteIcon;
/// Called when the user taps the [deleteIcon] to delete the chip.
///
/// If null, the delete button will not appear on the chip.
///
/// The chip will not automatically remove itself: this just tells the app
/// that the user tapped the delete button. In order to delete the chip, you
/// have to do something similar to the following sample:
///
/// <callout-box>
///
/// This sample shows how to use [onDeleted] to remove an entry when the
/// delete button is tapped.
///
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/chip/deletable_chip_attributes.on_deleted.0.dart#body}
///
/// </callout-box>
VoidCallback? get onDeleted;
/// Used to define the delete icon's color with an [IconTheme] that
/// contains the icon.
///
/// The default is `Color(0xde000000)`
/// (slightly transparent black) for light themes, and `Color(0xdeffffff)`
/// (slightly transparent white) for dark themes.
///
/// The delete icon appears if [DeletableChipAttributes.onDeleted] is
/// non-null.
Color? get deleteIconColor;
/// The message to be used for the chip's delete button tooltip.
///
/// If provided with an empty string, the tooltip of the delete button will be
/// disabled.
///
/// If null, the default [MaterialLocalizations.deleteButtonTooltip] will be
/// used.
///
/// If the chip is disabled, the delete button tooltip will not be shown.
String? get deleteButtonTooltipMessage;
/// Optional size constraints for the delete icon.
///
/// When unspecified, defaults to a minimum size of chip height or label height
/// (whichever is greater) and a padding of 8.0 pixels on all sides.
///
/// The default constraints ensure that the delete icon is accessible.
/// Specifying this parameter enables creation of delete icon smaller than
/// the minimum size, but it is not recommended.
///
/// <callout-box>
///
/// This sample shows how to use [deleteIconBoxConstraints] to adjust delete icon
/// size constraints.
///
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/chip/deletable_chip_attributes.delete_icon_box_constraints.0.dart#body}
///
/// </callout-box>
BoxConstraints? get deleteIconBoxConstraints;
}
/// An interface for Material Design chips that can have check marks.
///
/// The defaults mentioned in the documentation for each attribute are what
/// the implementing classes typically use for defaults (but this class doesn't
/// provide or enforce them).
///
/// See also:
///
/// * [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.
/// * [FilterChip], uses tags or descriptive words as a way to filter content.
/// * <https://material.io/design/components/chips.html>
abstract interface class CheckmarkableChipAttributes {
/// Whether or not to show a check mark when
/// [SelectableChipAttributes.selected] is true.
///
/// Defaults to true.
bool? get showCheckmark;
/// [Color] of the chip's check mark when a check mark is visible.
///
/// This will override the color set by the platform's brightness setting.
///
/// If null, it will defer to a color selected by the platform's brightness
/// setting.
Color? get checkmarkColor;
}
/// An interface for Material Design chips that can be selected.
///
/// The defaults mentioned in the documentation for each attribute are what
/// the implementing classes typically use for defaults (but this class doesn't
/// provide or enforce them).
///
/// See also:
///
/// * [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.
/// * [FilterChip], uses tags or descriptive words as a way to filter content.
/// * <https://material.io/design/components/chips.html>
abstract interface class SelectableChipAttributes {
/// Whether or not this chip is selected.
///
/// If [onSelected] is not null, this value will be used to determine if the
/// select check mark will be shown or not.
///
/// Defaults to false.
bool get selected;
/// Called when the chip should change between selected and de-selected
/// states.
///
/// When the chip is tapped, then the [onSelected] callback, if set, will be
/// applied to `!selected` (see [selected]).
///
/// The chip passes the new value to the callback but does not actually
/// change state until the parent widget rebuilds the chip with the new
/// value.
///
/// The callback provided to [onSelected] should update the state of the
/// parent [StatefulWidget] using the [State.setState] method, so that the
/// parent gets rebuilt.
///
/// The [onSelected] and [TappableChipAttributes.onPressed] callbacks must not
/// both be specified at the same time.
///
/// <callout-box>
///
// TODO(framework): Add unit tests to this code snippet.
// https://github.com/flutter/flutter/issues/188530
///
/// A [StatefulWidget] that illustrates use of onSelected in an [InputChip].
///
/// ```dart
/// class Wood extends StatefulWidget {
/// const Wood({super.key});
///
/// @override
/// State<StatefulWidget> createState() => WoodState();
/// }
///
/// class WoodState extends State<Wood> {
/// bool _useChisel = false;
///
/// @override
/// Widget build(BuildContext context) {
/// return InputChip(
/// label: const Text('Use Chisel'),
/// selected: _useChisel,
/// onSelected: (bool newValue) {
/// setState(() {
/// _useChisel = newValue;
/// });
/// },
/// );
/// }
/// }
/// ```
///
/// </callout-box>
ValueChanged<bool>? get onSelected;
/// Elevation to be applied on the chip relative to its parent during the
/// press motion.
///
/// This controls the size of the shadow below the chip.
///
/// Defaults to 8. The value is always non-negative.
double? get pressElevation;
/// Color to be used for the chip's background, indicating that it is
/// selected.
///
/// The chip is selected when [selected] is true.
Color? get selectedColor;
/// Color of the chip's shadow when the elevation is greater than 0 and the
/// chip is selected.
///
/// The default is [Colors.black].
Color? get selectedShadowColor;
/// Tooltip string to be used for the body area (where the label and avatar
/// are) of the chip.
String? get tooltip;
/// The shape of the translucent highlight painted over the avatar when the
/// [selected] property is true.
///
/// Only the outer path of the shape is used.
///
/// Defaults to [CircleBorder].
ShapeBorder get avatarBorder;
}
/// An interface for Material Design chips that can be enabled and disabled.
///
/// The defaults mentioned in the documentation for each attribute are what
/// the implementing classes typically use for defaults (but this class doesn't
/// provide or enforce them).
///
/// See also:
///
/// * [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.
/// * [FilterChip], uses tags or descriptive words as a way to filter content.
/// * <https://material.io/design/components/chips.html>
abstract interface class DisabledChipAttributes {
/// Whether or not this chip is enabled for input.
///
/// If this is true, but all of the user action callbacks are null (i.e.
/// [SelectableChipAttributes.onSelected], [TappableChipAttributes.onPressed],
/// and [DeletableChipAttributes.onDeleted]), then the
/// control will still be shown as disabled.
///
/// This is typically used if you want the chip to be disabled, but also show
/// a delete button.
///
/// For classes which don't have this as a constructor argument, [isEnabled]
/// returns true if their user action callback is set.
///
/// Defaults to true.
bool get isEnabled;
/// The color used for the chip's background to indicate that it is not
/// enabled.
///
/// The chip is disabled when [isEnabled] is false, or all three of
/// [SelectableChipAttributes.onSelected], [TappableChipAttributes.onPressed],
/// and [DeletableChipAttributes.onDeleted] are null.
///
/// It defaults to [Colors.black38].
Color? get disabledColor;
}
/// An interface for Material Design chips that can be tapped.
///
/// The defaults mentioned in the documentation for each attribute are what
/// the implementing classes typically use for defaults (but this class doesn't
/// provide or enforce them).
///
/// See also:
///
/// * [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.
/// * [FilterChip], uses tags or descriptive words as a way to filter content.
/// * [ActionChip], represents an action related to primary content.
/// * <https://material.io/design/components/chips.html>
abstract interface class TappableChipAttributes {
/// Called when the user taps the chip.
///
/// If [onPressed] is set, then this callback will be called when the user
/// taps on the label or avatar parts of the chip. If [onPressed] is null,
/// then the chip will be disabled.
///
/// <callout-box>
///
// TODO(framework): Add unit tests to this code snippet.
// https://github.com/flutter/flutter/issues/188530
///
/// ```dart
/// class Blacksmith extends StatelessWidget {
/// const Blacksmith({super.key});
///
/// void startHammering() {
/// print('bang bang bang');
/// }
///
/// @override
/// Widget build(BuildContext context) {
/// return InputChip(
/// label: const Text('Apply Hammer'),
/// onPressed: startHammering,
/// );
/// }
/// }
/// ```
///
/// </callout-box>
VoidCallback? get onPressed;
/// Elevation to be applied on the chip relative to its parent during the
/// press motion.
///
/// This controls the size of the shadow below the chip.
///
/// Defaults to 8. The value is always non-negative.
double? get pressElevation;
/// Tooltip string to be used for the body area (where the label and avatar
/// are) of the chip.
String? get tooltip;
}
/// A helper class that overrides the default chip animation parameters.
class ChipAnimationStyle {
/// Creates an instance of Chip Animation Style class.
ChipAnimationStyle({
this.enableAnimation,
this.selectAnimation,
this.avatarDrawerAnimation,
this.deleteDrawerAnimation,
});
/// If [enableAnimation] with duration or reverse duration is provided,
/// it will be used to override the chip enable and disable animation durations.
/// If it is null, then default duration will be 75ms.
final AnimationStyle? enableAnimation;
/// If [selectAnimation] with duration or reverse duration is provided,
/// it will be used to override the chip select and unselect animation durations.
/// If it is null, then default duration will be 195ms.
final AnimationStyle? selectAnimation;
/// If [avatarDrawerAnimation] with duration or reverse duration is provided,
/// it will be used to override the chip checkmark animation duration. If it
/// is null, then default duration will be 150ms.
final AnimationStyle? avatarDrawerAnimation;
/// If [deleteDrawerAnimation] with duration or reverse duration is provided,
/// it will be used to override the chip delete icon animation duration. If it
/// is null, then default duration will be 150ms.
final AnimationStyle? deleteDrawerAnimation;
}
/// A Material Design chip.
///
/// Chips are compact elements that represent an attribute, text, entity, or
/// action.
///
/// Supplying a non-null [onDeleted] callback will cause the chip to include a
/// button for deleting the chip.
///
/// Its ancestors must include [Material], [MediaQuery], [Directionality], and
/// [MaterialLocalizations]. Typically all of these widgets are provided by
/// [MaterialApp] and [Scaffold]. The [label] and [clipBehavior] arguments must
/// not be null.
///
/// <callout-box>
///
// TODO(framework): Add unit tests to this code snippet.
// https://github.com/flutter/flutter/issues/188530
///
/// ```dart
/// Chip(
/// avatar: CircleAvatar(
/// backgroundColor: Colors.grey.shade800,
/// child: const Text('AB'),
/// ),
/// label: const Text('Aaron Burr'),
/// )
/// ```
///
/// </callout-box>
///
/// See also:
///
/// * [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.
/// * [FilterChip], uses tags or descriptive words as a way to filter content.
/// * [ActionChip], represents an action related to primary content.
/// * [CircleAvatar], which shows images or initials of entities.
/// * [Wrap], A widget that displays its children in multiple horizontal or
/// vertical runs.
/// * <https://material.io/design/components/chips.html>
class Chip extends StatelessWidget implements ChipAttributes, DeletableChipAttributes {
/// Creates a Material Design chip.
///
/// The [elevation] must be null or non-negative.
const Chip({
super.key,
this.avatar,
required this.label,
this.labelStyle,
this.labelPadding,
this.deleteIcon,
this.onDeleted,
this.deleteIconColor,
this.deleteButtonTooltipMessage,
this.side,
this.shape,
this.clipBehavior = Clip.none,
this.focusNode,
this.autofocus = false,
this.color,
this.backgroundColor,
this.padding,
this.visualDensity,
this.materialTapTargetSize,
this.elevation,
this.shadowColor,
this.surfaceTintColor,
this.iconTheme,
this.avatarBoxConstraints,
this.deleteIconBoxConstraints,
this.chipAnimationStyle,
this.mouseCursor,
}) : assert(elevation == null || elevation >= 0.0);
@override
final Widget? avatar;
@override
final Widget label;
@override
final TextStyle? labelStyle;
@override
final EdgeInsetsGeometry? labelPadding;
@override
final BorderSide? side;
@override
final OutlinedBorder? shape;
@override
final Clip clipBehavior;
@override
final FocusNode? focusNode;
@override
final bool autofocus;
@override
final WidgetStateProperty<Color?>? color;
@override
final Color? backgroundColor;
@override
final EdgeInsetsGeometry? padding;
@override
final VisualDensity? visualDensity;
@override
final Widget? deleteIcon;
@override
final VoidCallback? onDeleted;
@override
final Color? deleteIconColor;
@override
final String? deleteButtonTooltipMessage;
@override
final MaterialTapTargetSize? materialTapTargetSize;
@override
final double? elevation;
@override
final Color? shadowColor;
@override
final Color? surfaceTintColor;
@override
final IconThemeData? iconTheme;
@override
final BoxConstraints? avatarBoxConstraints;
@override
final BoxConstraints? deleteIconBoxConstraints;
@override
final ChipAnimationStyle? chipAnimationStyle;
@override
final MouseCursor? mouseCursor;
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
return RawChip(
avatar: avatar,
label: label,
labelStyle: labelStyle,
labelPadding: labelPadding,
deleteIcon: deleteIcon,
onDeleted: onDeleted,
deleteIconColor: deleteIconColor,
deleteButtonTooltipMessage: deleteButtonTooltipMessage,
tapEnabled: false,
side: side,
shape: shape,
clipBehavior: clipBehavior,
focusNode: focusNode,
autofocus: autofocus,
color: color,
backgroundColor: backgroundColor,
padding: padding,
visualDensity: visualDensity,
materialTapTargetSize: materialTapTargetSize,
elevation: elevation,
shadowColor: shadowColor,
surfaceTintColor: surfaceTintColor,
iconTheme: iconTheme,
avatarBoxConstraints: avatarBoxConstraints,
deleteIconBoxConstraints: deleteIconBoxConstraints,
chipAnimationStyle: chipAnimationStyle,
mouseCursor: mouseCursor,
);
}
}
/// A raw Material Design chip.
///
/// This serves as the basis for all of the chip widget types to aggregate.
/// It is typically not created directly, one of the other chip types
/// that are appropriate for the use case are used instead:
///
/// * [Chip] a simple chip that can only display information and be deleted.
/// * [InputChip] 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.
/// * [FilterChip] a chip that uses tags or descriptive words as a way to
/// filter content.
/// * [ActionChip]s display a set of actions related to primary content.
///
/// Raw chips are typically only used if you want to create your own custom chip
/// type.
///
/// Raw chips can be selected by setting [onSelected], deleted by setting
/// [onDeleted], and pushed like a button with [onPressed]. They have a [label],
/// and they can have a leading icon (see [avatar]) and a trailing icon
/// ([deleteIcon]). Colors and padding can be customized.
///
/// Requires one of its ancestors to be a [Material] widget.
///
/// See also:
///
/// * [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>
class RawChip extends StatefulWidget
implements
ChipAttributes,
DeletableChipAttributes,
SelectableChipAttributes,
CheckmarkableChipAttributes,
DisabledChipAttributes,
TappableChipAttributes {
/// Creates a RawChip.
///
/// The [onPressed] and [onSelected] callbacks must not both be specified at
/// the same time.
///
/// The [pressElevation] and [elevation] must be null or non-negative.
/// Typically, [pressElevation] is greater than [elevation].
const RawChip({
super.key,
this.defaultProperties,
this.avatar,
required this.label,
this.labelStyle,
this.padding,
this.visualDensity,
this.labelPadding,
Widget? deleteIcon,
this.onDeleted,
this.deleteIconColor,
this.deleteButtonTooltipMessage,
this.onPressed,
this.onSelected,
this.pressElevation,
this.tapEnabled = true,
this.selected = false,
this.isEnabled = true,
this.disabledColor,
this.selectedColor,
this.tooltip,
this.side,
this.shape,
this.clipBehavior = Clip.none,
this.focusNode,
this.autofocus = false,
this.color,
this.backgroundColor,
this.materialTapTargetSize,
this.elevation,
this.shadowColor,
this.surfaceTintColor,
this.iconTheme,
this.selectedShadowColor,
this.showCheckmark,
this.checkmarkColor,
this.avatarBorder = const CircleBorder(),
this.avatarBoxConstraints,
this.deleteIconBoxConstraints,
this.chipAnimationStyle,
this.mouseCursor,
}) : assert(pressElevation == null || pressElevation >= 0.0),
assert(elevation == null || elevation >= 0.0),
deleteIcon = deleteIcon ?? _kDefaultDeleteIcon;
/// Defines the defaults for the chip properties if
/// they are not specified elsewhere.
///
/// If null then [ChipThemeData.fromDefaults] will be used
/// for the default properties.
final ChipThemeData? defaultProperties;
@override
final Widget? avatar;
@override
final Widget label;
@override
final TextStyle? labelStyle;
@override
final EdgeInsetsGeometry? labelPadding;
@override
final Widget deleteIcon;
@override
final VoidCallback? onDeleted;
@override
final Color? deleteIconColor;
@override
final String? deleteButtonTooltipMessage;
@override
final ValueChanged<bool>? onSelected;
@override
final VoidCallback? onPressed;
@override
final double? pressElevation;
@override
final bool selected;
@override
final bool isEnabled;
@override
final Color? disabledColor;
@override
final Color? selectedColor;
@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
final WidgetStateProperty<Color?>? color;
@override
final Color? backgroundColor;
@override
final EdgeInsetsGeometry? padding;
@override
final VisualDensity? visualDensity;
@override
final MaterialTapTargetSize? materialTapTargetSize;
@override
final double? elevation;
@override
final Color? shadowColor;