@@ -209,23 +209,24 @@ class GroupedCheckbox<T> extends StatefulWidget {
209
209
}
210
210
211
211
class _GroupedCheckboxState <T > extends State <GroupedCheckbox <T >> {
212
- List < T > selectedListItems = < T > [];
212
+ final selectedListItems = < T > [];
213
213
214
214
@override
215
- Widget build (BuildContext context) {
216
- var finalWidget = generateItems ();
217
- return finalWidget;
218
- }
215
+ void initState () {
216
+ super .initState ();
219
217
220
- Widget generateItems () {
221
- Widget finalWidget;
222
218
if (widget.value != null ) {
223
- selectedListItems = widget.value;
219
+ selectedListItems. addAll ( widget.value) ;
224
220
}
225
- var widgetList = < Widget > [];
221
+ }
222
+
223
+ @override
224
+ Widget build (BuildContext context) {
225
+ final widgetList = < Widget > [];
226
226
for (var i = 0 ; i < widget.options.length; i++ ) {
227
227
widgetList.add (item (i));
228
228
}
229
+ Widget finalWidget;
229
230
if (widget.orientation == OptionsOrientation .vertical) {
230
231
finalWidget = SingleChildScrollView (
231
232
scrollDirection: Axis .vertical,
@@ -262,35 +263,36 @@ class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
262
263
}
263
264
264
265
Widget item (int index) {
265
- var control = Checkbox (
266
+ final option = widget.options[index];
267
+ final optionValue = option.value;
268
+ final isOptionDisabled = true == widget.disabled? .contains (optionValue);
269
+ final control = Checkbox (
266
270
activeColor: widget.activeColor,
267
271
checkColor: widget.checkColor,
268
272
focusColor: widget.focusColor,
269
273
hoverColor: widget.hoverColor,
270
274
materialTapTargetSize: widget.materialTapTargetSize,
271
- value: selectedListItems.contains (widget.options[index].value ),
275
+ value: selectedListItems.contains (optionValue ),
272
276
tristate: widget.tristate,
273
- onChanged: (widget.disabled != null &&
274
- widget.disabled.contains (widget.options.elementAt (index).value))
277
+ onChanged: isOptionDisabled
275
278
? null
276
279
: (bool selected) {
277
280
selected
278
- ? selectedListItems.add (widget.options[index].value )
279
- : selectedListItems.remove (widget.options[index].value );
281
+ ? selectedListItems.add (optionValue )
282
+ : selectedListItems.remove (optionValue );
280
283
setState (() {
281
284
widget.onChanged (selectedListItems);
282
285
});
283
286
},
284
287
);
285
- var label = GestureDetector (
286
- child: widget.options[index],
287
- onTap: (widget.disabled != null &&
288
- widget.disabled.contains (widget.options.elementAt (index).value))
288
+ final label = GestureDetector (
289
+ child: option,
290
+ onTap: isOptionDisabled
289
291
? null
290
292
: () {
291
- ! selectedListItems.contains (widget.options[index].value )
292
- ? selectedListItems.add (widget.options[index].value )
293
- : selectedListItems.remove (widget.options[index].value );
293
+ selectedListItems.contains (optionValue )
294
+ ? selectedListItems.remove (optionValue )
295
+ : selectedListItems.add (optionValue );
294
296
setState (() {
295
297
widget.onChanged (selectedListItems);
296
298
});
@@ -300,16 +302,10 @@ class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
300
302
return Row (
301
303
mainAxisSize: MainAxisSize .min,
302
304
children: < Widget > [
303
- if (widget.controlAffinity == ControlAffinity .leading) ...[
304
- control,
305
- Flexible (flex: 1 , child: label),
306
- ],
307
- if (widget.controlAffinity == ControlAffinity .trailing) ...[
308
- Flexible (flex: 1 , child: label),
309
- control
310
- ],
311
- if (widget.separator != null &&
312
- widget.options[index] != widget.options.last)
305
+ if (widget.controlAffinity == ControlAffinity .leading) control,
306
+ Flexible (flex: 1 , child: label),
307
+ if (widget.controlAffinity == ControlAffinity .trailing) control,
308
+ if (widget.separator != null && index != widget.options.length - 1 )
313
309
widget.separator,
314
310
],
315
311
);
0 commit comments