1
+ import 'package:flutter/material.dart' ;
2
+ import 'package:flutter/widgets.dart' ;
3
+ import 'package:flutter_form_builder/flutter_form_builder.dart' ;
4
+
5
+ /// Field with chips that acts like a list checkboxes.
6
+ class FormBuilderFilterChip <T > extends FormBuilderField <List <T >> {
7
+ //TODO: Add documentation
8
+ final List <FormBuilderFieldOption <T >> options;
9
+ final double elevation, pressElevation;
10
+ final Color selectedColor;
11
+ final Color disabledColor;
12
+ final Color backgroundColor;
13
+ final Color selectedShadowColor;
14
+ final Color shadowColor;
15
+ final OutlinedBorder shape;
16
+ final MaterialTapTargetSize materialTapTargetSize;
17
+
18
+ // Wrap Settings
19
+ final Axis direction;
20
+ final WrapAlignment alignment;
21
+ final WrapCrossAlignment crossAxisAlignment;
22
+ final WrapAlignment runAlignment;
23
+ final double runSpacing, spacing;
24
+ final TextDirection textDirection;
25
+ final VerticalDirection verticalDirection;
26
+ final EdgeInsets padding;
27
+ final Color checkmarkColor;
28
+ final Clip clipBehavior;
29
+ final TextStyle labelStyle;
30
+ final bool showCheckmark;
31
+ final EdgeInsets labelPadding;
32
+
33
+ // final VisualDensity visualDensity;
34
+ final int maxChips;
35
+
36
+ /// Creates field with chips that acts like a list checkboxes.
37
+ FormBuilderFilterChip ({
38
+ Key key,
39
+ //From Super
40
+ @required String name,
41
+ FormFieldValidator <List <T >> validator,
42
+ List <T > initialValue = const [],
43
+ InputDecoration decoration = const InputDecoration (),
44
+ ValueChanged <List <T >> onChanged,
45
+ ValueTransformer <List <T >> valueTransformer,
46
+ bool enabled = true ,
47
+ FormFieldSetter <List <T >> onSaved,
48
+ AutovalidateMode autovalidateMode = AutovalidateMode .disabled,
49
+ VoidCallback onReset,
50
+ FocusNode focusNode,
51
+ @required this .options,
52
+ this .selectedColor,
53
+ this .disabledColor,
54
+ this .backgroundColor,
55
+ this .shadowColor,
56
+ this .selectedShadowColor,
57
+ this .shape,
58
+ this .elevation,
59
+ this .pressElevation,
60
+ this .materialTapTargetSize,
61
+ this .direction = Axis .horizontal,
62
+ this .alignment = WrapAlignment .start,
63
+ this .crossAxisAlignment = WrapCrossAlignment .start,
64
+ this .runAlignment = WrapAlignment .start,
65
+ this .runSpacing = 0.0 ,
66
+ this .spacing = 0.0 ,
67
+ this .textDirection,
68
+ this .verticalDirection = VerticalDirection .down,
69
+ this .padding,
70
+ this .checkmarkColor,
71
+ this .clipBehavior = Clip .none,
72
+ this .labelStyle,
73
+ this .showCheckmark = true ,
74
+ this .labelPadding,
75
+ this .maxChips,
76
+ // this.visualDensity,
77
+ }) : assert ((maxChips == null || initialValue == null ) ||
78
+ (initialValue.length <= maxChips)),
79
+ super (
80
+ key: key,
81
+ initialValue: initialValue,
82
+ name: name,
83
+ validator: validator,
84
+ valueTransformer: valueTransformer,
85
+ onChanged: onChanged,
86
+ autovalidateMode: autovalidateMode,
87
+ onSaved: onSaved,
88
+ enabled: enabled,
89
+ onReset: onReset,
90
+ decoration: decoration,
91
+ focusNode: focusNode,
92
+ builder: (FormFieldState <List <T >> field) {
93
+ final state = field as _FormBuilderFilterChipState <T >;
94
+ return InputDecorator (
95
+ decoration: state.decoration (),
96
+ child: Wrap (
97
+ direction: direction,
98
+ alignment: alignment,
99
+ crossAxisAlignment: crossAxisAlignment,
100
+ runAlignment: runAlignment,
101
+ runSpacing: runSpacing,
102
+ spacing: spacing,
103
+ textDirection: textDirection,
104
+ verticalDirection: verticalDirection,
105
+ children: < Widget > [
106
+ for (FormBuilderFieldOption <T > option in options)
107
+ FilterChip (
108
+ label: option,
109
+ selected: field.value.contains (option.value),
110
+ onSelected: state.enabled &&
111
+ (null == maxChips ||
112
+ field.value.length < maxChips ||
113
+ field.value.contains (option.value))
114
+ ? (selected) {
115
+ final currentValue = [...field.value];
116
+ if (selected) {
117
+ currentValue.add (option.value);
118
+ } else {
119
+ currentValue.remove (option.value);
120
+ }
121
+ state.requestFocus ();
122
+ field.didChange (currentValue);
123
+ }
124
+ : null ,
125
+ selectedColor: selectedColor,
126
+ disabledColor: disabledColor,
127
+ backgroundColor: backgroundColor,
128
+ shadowColor: shadowColor,
129
+ selectedShadowColor: selectedShadowColor,
130
+ shape: shape,
131
+ elevation: elevation,
132
+ pressElevation: pressElevation,
133
+ materialTapTargetSize: materialTapTargetSize,
134
+ padding: padding,
135
+ checkmarkColor: checkmarkColor,
136
+ clipBehavior: clipBehavior,
137
+ labelStyle: labelStyle,
138
+ showCheckmark: showCheckmark,
139
+ labelPadding: labelPadding,
140
+ // visualDensity: visualDensity,
141
+ ),
142
+ ],
143
+ ),
144
+ );
145
+ },
146
+ );
147
+
148
+ @override
149
+ _FormBuilderFilterChipState <T > createState () =>
150
+ _FormBuilderFilterChipState <T >();
151
+ }
152
+
153
+ class _FormBuilderFilterChipState <T >
154
+ extends FormBuilderFieldState <FormBuilderFilterChip <T >, List <T >> {}
0 commit comments