Skip to content

Commit a81c3f3

Browse files
Merge pull request #1106 from flutter-form-builder-ecosystem/1104
Improve FormBuilderRadioGroup separator
2 parents 956cd08 + f8abda8 commit a81c3f3

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

lib/src/widgets/grouped_radio.dart

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class GroupedRadio<T> extends StatefulWidget {
173173
final ControlAffinity controlAffinity;
174174

175175
const GroupedRadio({
176-
Key? key,
176+
super.key,
177177
required this.options,
178178
required this.orientation,
179179
required this.onChanged,
@@ -193,7 +193,7 @@ class GroupedRadio<T> extends StatefulWidget {
193193
this.wrapVerticalDirection = VerticalDirection.down,
194194
this.separator,
195195
this.controlAffinity = ControlAffinity.leading,
196-
}) : super(key: key);
196+
});
197197

198198
@override
199199
State<GroupedRadio<T?>> createState() => _GroupedRadioState<T>();
@@ -203,46 +203,43 @@ class _GroupedRadioState<T> extends State<GroupedRadio<T?>> {
203203
@override
204204
Widget build(BuildContext context) {
205205
final widgetList = <Widget>[];
206-
for (var i = 0; i < widget.options.length; i++) {
207-
widgetList.add(item(i));
206+
for (int i = 0; i < widget.options.length; i++) {
207+
widgetList.add(_buildRadioButton(i));
208208
}
209-
Widget finalWidget;
210-
if (widget.orientation == OptionsOrientation.vertical) {
211-
finalWidget = SingleChildScrollView(
212-
scrollDirection: Axis.vertical,
213-
child: Column(
214-
crossAxisAlignment: CrossAxisAlignment.start,
215-
children: widgetList,
216-
),
217-
);
218-
} else if (widget.orientation == OptionsOrientation.horizontal) {
219-
finalWidget = SingleChildScrollView(
220-
scrollDirection: Axis.horizontal,
221-
child: Row(
222-
children: widgetList.map((item) {
223-
return Column(children: <Widget>[item]);
224-
}).toList(),
225-
),
226-
);
227-
} else {
228-
finalWidget = SingleChildScrollView(
229-
child: Wrap(
230-
spacing: widget.wrapSpacing,
231-
runSpacing: widget.wrapRunSpacing,
232-
textDirection: widget.wrapTextDirection,
233-
crossAxisAlignment: widget.wrapCrossAxisAlignment,
234-
verticalDirection: widget.wrapVerticalDirection,
235-
alignment: widget.wrapAlignment,
236-
direction: Axis.horizontal,
237-
runAlignment: widget.wrapRunAlignment,
238-
children: widgetList,
239-
),
240-
);
209+
210+
switch (widget.orientation) {
211+
case OptionsOrientation.vertical:
212+
return SingleChildScrollView(
213+
scrollDirection: Axis.vertical,
214+
child: Column(
215+
crossAxisAlignment: CrossAxisAlignment.start,
216+
children: widgetList,
217+
),
218+
);
219+
case OptionsOrientation.horizontal:
220+
return SingleChildScrollView(
221+
scrollDirection: Axis.horizontal,
222+
child: Row(children: widgetList),
223+
);
224+
case OptionsOrientation.wrap:
225+
default:
226+
return SingleChildScrollView(
227+
child: Wrap(
228+
spacing: widget.wrapSpacing,
229+
runSpacing: widget.wrapRunSpacing,
230+
textDirection: widget.wrapTextDirection,
231+
crossAxisAlignment: widget.wrapCrossAxisAlignment,
232+
verticalDirection: widget.wrapVerticalDirection,
233+
alignment: widget.wrapAlignment,
234+
direction: Axis.horizontal,
235+
runAlignment: widget.wrapRunAlignment,
236+
children: widgetList,
237+
),
238+
);
241239
}
242-
return finalWidget;
243240
}
244241

245-
Widget item(int index) {
242+
Widget _buildRadioButton(int index) {
246243
final option = widget.options[index];
247244
final optionValue = option.value;
248245
final isOptionDisabled = true == widget.disabled?.contains(optionValue);
@@ -266,16 +263,28 @@ class _GroupedRadioState<T> extends State<GroupedRadio<T?>> {
266263
: () {
267264
widget.onChanged(optionValue);
268265
},
269-
child: widget.options[index],
266+
child: option,
270267
);
271268

272-
return Row(
269+
return Column(
273270
mainAxisSize: MainAxisSize.min,
271+
crossAxisAlignment: CrossAxisAlignment.start,
274272
children: [
275-
if (widget.controlAffinity == ControlAffinity.leading) control,
276-
Flexible(child: label),
277-
if (widget.controlAffinity == ControlAffinity.trailing) control,
278-
if (widget.separator != null && index != widget.options.length - 1)
273+
Row(
274+
mainAxisSize: MainAxisSize.min,
275+
children: [
276+
if (widget.controlAffinity == ControlAffinity.leading) control,
277+
Flexible(child: label),
278+
if (widget.controlAffinity == ControlAffinity.trailing) control,
279+
if (widget.orientation != OptionsOrientation.vertical &&
280+
widget.separator != null &&
281+
index != widget.options.length - 1)
282+
widget.separator!,
283+
],
284+
),
285+
if (widget.orientation == OptionsOrientation.vertical &&
286+
widget.separator != null &&
287+
index != widget.options.length - 1)
279288
widget.separator!,
280289
],
281290
);

0 commit comments

Comments
 (0)