Line data Source code
1 : part of 'foss_select.dart';
2 :
3 : /// {@category Inputs}
4 : /// A pick-several-from-list control with no typing.
5 : ///
6 : /// Like [FossSelect] but the value is a set: each row carries a checkbox, a
7 : /// pick toggles the row in [values] and keeps the popup open, and the trigger
8 : /// shows a count of picks rather than a single label. Selection is controlled:
9 : /// pass [values] and rebuild on [onChanged]. A null [onChanged] (or
10 : /// `enabled: false`) disables the field.
11 : ///
12 : /// For a single choice, use [FossSelect]. To search a long list by typing, that
13 : /// is a combobox, not a select.
14 : ///
15 : /// ```dart
16 : /// FossMultiSelect<String>(
17 : /// values: tags,
18 : /// placeholder: 'Choose tags',
19 : /// onChanged: (next) => setState(() => tags = next),
20 : /// items: const [
21 : /// FossSelectItem(value: 'design', label: 'Design'),
22 : /// FossSelectItem(value: 'eng', label: 'Engineering'),
23 : /// ],
24 : /// );
25 : /// ```
26 : class FossMultiSelect<T> extends StatelessWidget {
27 : /// Creates a multi-select control over [items].
28 3 : const FossMultiSelect({
29 : required this.items,
30 : this.values = const {},
31 : this.onChanged,
32 : this.placeholder,
33 : this.label,
34 : this.errorText,
35 : this.size = FossSelectSize.md,
36 : this.enabled = true,
37 : this.selectionLabelBuilder,
38 : this.style,
39 : super.key,
40 : });
41 :
42 : /// The options to choose from.
43 : final List<FossSelectItem<T>> items;
44 :
45 : /// The current set of picked values.
46 : final Set<T> values;
47 :
48 : /// Called with the next set when a row is toggled. A null callback disables
49 : /// the field.
50 : final ValueChanged<Set<T>>? onChanged;
51 :
52 : /// Text shown in the trigger when [values] is empty.
53 : final String? placeholder;
54 :
55 : /// Optional label rendered above the trigger.
56 : final String? label;
57 :
58 : /// When non-null, marks the field invalid and recolors the trigger border.
59 : final String? errorText;
60 :
61 : /// The trigger height and type scale.
62 : final FossSelectSize size;
63 :
64 : /// Whether the field accepts input. Disabled when false or [onChanged] is
65 : /// null.
66 : final bool enabled;
67 :
68 : /// Builds the trigger text from the number of picks. Defaults to
69 : /// "N selected"; override for a different wording or another language.
70 : final String Function(int count)? selectionLabelBuilder;
71 :
72 : /// Per-instance overrides layered on the theme-resolved style.
73 : final FossSelectStyle? style;
74 :
75 3 : @override
76 : Widget build(BuildContext context) {
77 3 : return _FossSelectField<T>(
78 3 : items: items,
79 3 : size: size,
80 3 : style: style,
81 3 : label: label,
82 3 : placeholder: placeholder,
83 3 : errorText: errorText,
84 6 : enabled: enabled && onChanged != null,
85 12 : triggerLabel: values.isEmpty ? null : _countLabel(values.length),
86 : indicator: _SelectIndicator.checkbox,
87 6 : isSelected: values.contains,
88 : closeOnPick: false,
89 3 : onPick: _toggle,
90 : );
91 : }
92 :
93 2 : String _countLabel(int count) =>
94 5 : selectionLabelBuilder?.call(count) ?? '$count selected';
95 :
96 1 : void _toggle(T value) {
97 2 : final next = {...values};
98 2 : if (!next.remove(value)) next.add(value);
99 2 : onChanged?.call(next);
100 : }
101 : }
|