Skip to content

Commit 7d06fd6

Browse files
committed
Merge branch 'version_4' of https://github.com/danvick/flutter_form_builder into version_4
2 parents eb5fe74 + 45120cd commit 7d06fd6

File tree

2 files changed

+50
-60
lines changed

2 files changed

+50
-60
lines changed

lib/src/widgets/grouped_checkbox.dart

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,24 @@ class GroupedCheckbox<T> extends StatefulWidget {
209209
}
210210

211211
class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
212-
List<T> selectedListItems = <T>[];
212+
final selectedListItems = <T>[];
213213

214214
@override
215-
Widget build(BuildContext context) {
216-
var finalWidget = generateItems();
217-
return finalWidget;
218-
}
215+
void initState() {
216+
super.initState();
219217

220-
Widget generateItems() {
221-
Widget finalWidget;
222218
if (widget.value != null) {
223-
selectedListItems = widget.value;
219+
selectedListItems.addAll(widget.value);
224220
}
225-
var widgetList = <Widget>[];
221+
}
222+
223+
@override
224+
Widget build(BuildContext context) {
225+
final widgetList = <Widget>[];
226226
for (var i = 0; i < widget.options.length; i++) {
227227
widgetList.add(item(i));
228228
}
229+
Widget finalWidget;
229230
if (widget.orientation == OptionsOrientation.vertical) {
230231
finalWidget = SingleChildScrollView(
231232
scrollDirection: Axis.vertical,
@@ -262,35 +263,36 @@ class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
262263
}
263264

264265
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(
266270
activeColor: widget.activeColor,
267271
checkColor: widget.checkColor,
268272
focusColor: widget.focusColor,
269273
hoverColor: widget.hoverColor,
270274
materialTapTargetSize: widget.materialTapTargetSize,
271-
value: selectedListItems.contains(widget.options[index].value),
275+
value: selectedListItems.contains(optionValue),
272276
tristate: widget.tristate,
273-
onChanged: (widget.disabled != null &&
274-
widget.disabled.contains(widget.options.elementAt(index).value))
277+
onChanged: isOptionDisabled
275278
? null
276279
: (bool selected) {
277280
selected
278-
? selectedListItems.add(widget.options[index].value)
279-
: selectedListItems.remove(widget.options[index].value);
281+
? selectedListItems.add(optionValue)
282+
: selectedListItems.remove(optionValue);
280283
setState(() {
281284
widget.onChanged(selectedListItems);
282285
});
283286
},
284287
);
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
289291
? null
290292
: () {
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);
294296
setState(() {
295297
widget.onChanged(selectedListItems);
296298
});
@@ -300,16 +302,10 @@ class _GroupedCheckboxState<T> extends State<GroupedCheckbox<T>> {
300302
return Row(
301303
mainAxisSize: MainAxisSize.min,
302304
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)
313309
widget.separator,
314310
],
315311
);

lib/src/widgets/grouped_radio.dart

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,19 @@ class _GroupedRadioState<T> extends State<GroupedRadio<T>> {
202202
T selectedValue;
203203

204204
@override
205-
Widget build(BuildContext context) {
206-
var finalWidget = generateItems();
207-
return finalWidget;
205+
void initState() {
206+
super.initState();
207+
208+
selectedValue = widget.value;
208209
}
209210

210-
Widget generateItems() {
211-
Widget finalWidget;
212-
if (widget.value != null) {
213-
selectedValue = widget.value;
214-
}
215-
var widgetList = <Widget>[];
211+
@override
212+
Widget build(BuildContext context) {
213+
final widgetList = <Widget>[];
216214
for (var i = 0; i < widget.options.length; i++) {
217215
widgetList.add(item(i));
218216
}
217+
Widget finalWidget;
219218
if (widget.orientation == OptionsOrientation.vertical) {
220219
finalWidget = SingleChildScrollView(
221220
scrollDirection: Axis.vertical,
@@ -252,50 +251,45 @@ class _GroupedRadioState<T> extends State<GroupedRadio<T>> {
252251
}
253252

254253
Widget item(int index) {
255-
var control = Radio<T>(
254+
final option = widget.options[index];
255+
final optionValue = option.value;
256+
final isOptionDisabled = true == widget.disabled?.contains(optionValue);
257+
final control = Radio<T>(
256258
groupValue: selectedValue,
257259
activeColor: widget.activeColor,
258260
focusColor: widget.focusColor,
259261
hoverColor: widget.hoverColor,
260262
materialTapTargetSize: widget.materialTapTargetSize,
261-
value: widget.options[index].value,
262-
onChanged: (widget.disabled != null &&
263-
widget.disabled.contains(widget.options.elementAt(index).value))
263+
value: optionValue,
264+
onChanged: isOptionDisabled
264265
? null
265266
: (T selected) {
266267
setState(() {
267268
selectedValue = selected;
268269
});
269-
widget.onChanged?.call(selectedValue);
270+
widget.onChanged(selectedValue);
270271
},
271272
);
272273

273274
var label = GestureDetector(
274275
child: widget.options[index],
275-
onTap: (widget.disabled != null &&
276-
widget.disabled.contains(widget.options.elementAt(index).value))
276+
onTap: isOptionDisabled
277277
? null
278278
: () {
279279
setState(() {
280-
selectedValue = widget.options[index].value;
280+
selectedValue = optionValue;
281281
});
282-
widget.onChanged?.call(selectedValue);
282+
widget.onChanged(selectedValue);
283283
},
284284
);
285285

286286
return Row(
287287
mainAxisSize: MainAxisSize.min,
288288
children: <Widget>[
289-
if (widget.controlAffinity == ControlAffinity.leading) ...[
290-
control,
291-
Flexible(child: label),
292-
],
293-
if (widget.controlAffinity == ControlAffinity.trailing) ...[
294-
Flexible(child: label),
295-
control,
296-
],
297-
if (widget.separator != null &&
298-
widget.options[index] != widget.options.last)
289+
if (widget.controlAffinity == ControlAffinity.leading) control,
290+
Flexible(child: label),
291+
if (widget.controlAffinity == ControlAffinity.trailing) control,
292+
if (widget.separator != null && index != widget.options.length - 1)
299293
widget.separator,
300294
],
301295
);

0 commit comments

Comments
 (0)