1
1
import 'package:flutter/material.dart' ;
2
2
import 'package:flutter_form_builder/flutter_form_builder.dart' ;
3
3
4
- class GroupedCheckbox <T > extends StatefulWidget {
4
+ class GroupedCheckbox <T > extends StatelessWidget {
5
5
/// A list of string that describes each checkbox. Each item must be distinct.
6
6
final List <FormBuilderFieldOption <T >> options;
7
7
@@ -204,38 +204,22 @@ class GroupedCheckbox<T> extends StatefulWidget {
204
204
this .controlAffinity = ControlAffinity .leading,
205
205
}) : super (key: key);
206
206
207
- @override
208
- _GroupedCheckboxState <T > createState () => _GroupedCheckboxState <T >();
209
- }
210
-
211
- class _GroupedCheckboxState <T > extends State <GroupedCheckbox <T >> {
212
- final selectedListItems = < T > [];
213
-
214
- @override
215
- void initState () {
216
- super .initState ();
217
-
218
- if (widget.value != null ) {
219
- selectedListItems.addAll (widget.value! );
220
- }
221
- }
222
-
223
207
@override
224
208
Widget build (BuildContext context) {
225
209
final widgetList = < Widget > [];
226
- for (var i = 0 ; i < widget. options.length; i++ ) {
210
+ for (var i = 0 ; i < options.length; i++ ) {
227
211
widgetList.add (item (i));
228
212
}
229
213
Widget finalWidget;
230
- if (widget. orientation == OptionsOrientation .vertical) {
214
+ if (orientation == OptionsOrientation .vertical) {
231
215
finalWidget = SingleChildScrollView (
232
216
scrollDirection: Axis .vertical,
233
217
child: Column (
234
218
crossAxisAlignment: CrossAxisAlignment .start,
235
219
children: widgetList,
236
220
),
237
221
);
238
- } else if (widget. orientation == OptionsOrientation .horizontal) {
222
+ } else if (orientation == OptionsOrientation .horizontal) {
239
223
finalWidget = SingleChildScrollView (
240
224
scrollDirection: Axis .horizontal,
241
225
child: Row (
@@ -247,14 +231,14 @@ class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
247
231
} else {
248
232
finalWidget = SingleChildScrollView (
249
233
child: Wrap (
250
- spacing: widget. wrapSpacing,
251
- runSpacing: widget. wrapRunSpacing,
252
- textDirection: widget. wrapTextDirection,
253
- crossAxisAlignment: widget. wrapCrossAxisAlignment,
254
- verticalDirection: widget. wrapVerticalDirection,
255
- alignment: widget. wrapAlignment,
234
+ spacing: wrapSpacing,
235
+ runSpacing: wrapRunSpacing,
236
+ textDirection: wrapTextDirection,
237
+ crossAxisAlignment: wrapCrossAxisAlignment,
238
+ verticalDirection: wrapVerticalDirection,
239
+ alignment: wrapAlignment,
256
240
direction: Axis .horizontal,
257
- runAlignment: widget. wrapRunAlignment,
241
+ runAlignment: wrapRunAlignment,
258
242
children: widgetList,
259
243
),
260
244
);
@@ -263,50 +247,47 @@ class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
263
247
}
264
248
265
249
Widget item (int index) {
266
- final option = widget. options[index];
250
+ final option = options[index];
267
251
final optionValue = option.value;
268
- final isOptionDisabled = true == widget. disabled? .contains (optionValue);
252
+ final isOptionDisabled = true == disabled? .contains (optionValue);
269
253
final control = Checkbox (
270
- activeColor: widget. activeColor,
271
- checkColor: widget. checkColor,
272
- focusColor: widget. focusColor,
273
- hoverColor: widget. hoverColor,
274
- materialTapTargetSize: widget. materialTapTargetSize,
275
- value: selectedListItems .contains (optionValue),
276
- tristate: widget. tristate,
254
+ activeColor: activeColor,
255
+ checkColor: checkColor,
256
+ focusColor: focusColor,
257
+ hoverColor: hoverColor,
258
+ materialTapTargetSize: materialTapTargetSize,
259
+ value: true == value ? .contains (optionValue),
260
+ tristate: tristate,
277
261
onChanged: isOptionDisabled
278
262
? null
279
263
: (selected) {
264
+ List <T > selectedListItems = value == null ? [] : List .of (value! );
280
265
selected!
281
266
? selectedListItems.add (optionValue)
282
267
: selectedListItems.remove (optionValue);
283
- setState (() {
284
- widget.onChanged (selectedListItems);
285
- });
268
+ onChanged (selectedListItems);
286
269
},
287
270
);
288
271
final label = GestureDetector (
289
272
onTap: isOptionDisabled
290
273
? null
291
274
: () {
275
+ List <T > selectedListItems = value == null ? [] : List .of (value! );
292
276
selectedListItems.contains (optionValue)
293
277
? selectedListItems.remove (optionValue)
294
278
: selectedListItems.add (optionValue);
295
- setState (() {
296
- widget.onChanged (selectedListItems);
297
- });
279
+ onChanged (selectedListItems);
298
280
},
299
281
child: option,
300
282
);
301
283
302
284
return Row (
303
285
mainAxisSize: MainAxisSize .min,
304
286
children: < Widget > [
305
- if (widget. controlAffinity == ControlAffinity .leading) control,
287
+ if (controlAffinity == ControlAffinity .leading) control,
306
288
Flexible (flex: 1 , child: label),
307
- if (widget.controlAffinity == ControlAffinity .trailing) control,
308
- if (widget.separator != null && index != widget.options.length - 1 )
309
- widget.separator! ,
289
+ if (controlAffinity == ControlAffinity .trailing) control,
290
+ if (separator != null && index != options.length - 1 ) separator! ,
310
291
],
311
292
);
312
293
}
0 commit comments