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,49 @@ 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: tristate
260
+ ? value? .contains (optionValue)
261
+ : true == value? .contains (optionValue),
262
+ tristate: tristate,
277
263
onChanged: isOptionDisabled
278
264
? null
279
265
: (selected) {
266
+ List <T > selectedListItems = value == null ? [] : List .of (value! );
280
267
selected!
281
268
? selectedListItems.add (optionValue)
282
269
: selectedListItems.remove (optionValue);
283
- setState (() {
284
- widget.onChanged (selectedListItems);
285
- });
270
+ onChanged (selectedListItems);
286
271
},
287
272
);
288
273
final label = GestureDetector (
289
274
onTap: isOptionDisabled
290
275
? null
291
276
: () {
277
+ List <T > selectedListItems = value == null ? [] : List .of (value! );
292
278
selectedListItems.contains (optionValue)
293
279
? selectedListItems.remove (optionValue)
294
280
: selectedListItems.add (optionValue);
295
- setState (() {
296
- widget.onChanged (selectedListItems);
297
- });
281
+ onChanged (selectedListItems);
298
282
},
299
283
child: option,
300
284
);
301
285
302
286
return Row (
303
287
mainAxisSize: MainAxisSize .min,
304
288
children: < Widget > [
305
- if (widget. controlAffinity == ControlAffinity .leading) control,
289
+ if (controlAffinity == ControlAffinity .leading) control,
306
290
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! ,
291
+ if (controlAffinity == ControlAffinity .trailing) control,
292
+ if (separator != null && index != options.length - 1 ) separator! ,
310
293
],
311
294
);
312
295
}
0 commit comments