Skip to content

Commit 4f9dba1

Browse files
SvenSchoenedanvick
authored andcommitted
Feature leading checkbox radio (#57)
* implement option for a leading checkbox * implement option for a leading radio button
1 parent 72849a1 commit 4f9dba1

File tree

4 files changed

+64
-31
lines changed

4 files changed

+64
-31
lines changed

lib/src/fields/form_builder_checkbox.dart

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FormBuilderCheckbox extends StatefulWidget {
1010
final InputDecoration decoration;
1111
final ValueChanged onChanged;
1212
final ValueTransformer valueTransformer;
13+
final bool leadingInput;
1314

1415
final Widget label;
1516

@@ -22,6 +23,7 @@ class FormBuilderCheckbox extends StatefulWidget {
2223
this.decoration = const InputDecoration(),
2324
this.onChanged,
2425
this.valueTransformer,
26+
this.leadingInput = false,
2527
});
2628

2729
@override
@@ -47,6 +49,28 @@ class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
4749
super.dispose();
4850
}
4951

52+
Widget _checkbox(FormFieldState<dynamic> field) {
53+
return Checkbox(
54+
value: field.value ?? false,
55+
onChanged: _readonly
56+
? null
57+
: (bool value) {
58+
field.didChange(value);
59+
if (widget.onChanged != null) widget.onChanged(value);
60+
},
61+
);
62+
}
63+
64+
Widget _leading(FormFieldState<dynamic> field) {
65+
if (widget.leadingInput) return _checkbox(field);
66+
return null;
67+
}
68+
69+
Widget _trailing(FormFieldState<dynamic> field) {
70+
if (!widget.leadingInput) return _checkbox(field);
71+
return null;
72+
}
73+
5074
@override
5175
Widget build(BuildContext context) {
5276
return FormField(
@@ -78,15 +102,8 @@ class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
78102
isThreeLine: false,
79103
contentPadding: EdgeInsets.all(0.0),
80104
title: widget.label,
81-
trailing: Checkbox(
82-
value: field.value ?? false,
83-
onChanged: _readonly
84-
? null
85-
: (bool value) {
86-
field.didChange(value);
87-
if (widget.onChanged != null) widget.onChanged(value);
88-
},
89-
),
105+
leading: _leading(field),
106+
trailing: _trailing(field),
90107
onTap: _readonly
91108
? null
92109
: () {

lib/src/fields/form_builder_radio.dart

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FormBuilderRadio extends StatefulWidget {
1010
final InputDecoration decoration;
1111
final ValueChanged onChanged;
1212
final ValueTransformer valueTransformer;
13+
final bool leadingInput;
1314

1415
final List<FormBuilderFieldOption> options;
1516

@@ -22,6 +23,7 @@ class FormBuilderRadio extends StatefulWidget {
2223
this.decoration = const InputDecoration(),
2324
this.onChanged,
2425
this.valueTransformer,
26+
this.leadingInput = false,
2527
});
2628

2729
@override
@@ -47,6 +49,29 @@ class _FormBuilderRadioState extends State<FormBuilderRadio> {
4749
super.dispose();
4850
}
4951

52+
Widget _radio(FormFieldState<dynamic> field, int i) {
53+
return Radio<dynamic>(
54+
value: widget.options[i].value,
55+
groupValue: field.value,
56+
onChanged: _readonly
57+
? null
58+
: (dynamic value) {
59+
field.didChange(value);
60+
if (widget.onChanged != null) widget.onChanged(value);
61+
},
62+
);
63+
}
64+
65+
Widget _leading(FormFieldState<dynamic> field, int i) {
66+
if (widget.leadingInput) return _radio(field, i);
67+
return null;
68+
}
69+
70+
Widget _trailing(FormFieldState<dynamic> field, int i) {
71+
if (!widget.leadingInput) return _radio(field, i);
72+
return null;
73+
}
74+
5075
@override
5176
Widget build(BuildContext context) {
5277
return FormField(
@@ -75,19 +100,10 @@ class _FormBuilderRadioState extends State<FormBuilderRadio> {
75100
dense: true,
76101
isThreeLine: false,
77102
contentPadding: EdgeInsets.all(0.0),
78-
leading: null,
103+
leading: _leading(field, i),
79104
title:
80105
Text("${widget.options[i].label ?? widget.options[i].value}"),
81-
trailing: Radio<dynamic>(
82-
value: widget.options[i].value,
83-
groupValue: field.value,
84-
onChanged: _readonly
85-
? null
86-
: (dynamic value) {
87-
field.didChange(value);
88-
if (widget.onChanged != null) widget.onChanged(value);
89-
},
90-
),
106+
trailing: _trailing(field, i),
91107
onTap: _readonly
92108
? null
93109
: () {

pubspec.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ packages:
2121
name: async
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "2.0.8"
24+
version: "2.2.0"
2525
boolean_selector:
2626
dependency: transitive
2727
description:
@@ -171,7 +171,7 @@ packages:
171171
name: matcher
172172
url: "https://pub.dartlang.org"
173173
source: hosted
174-
version: "0.12.3+1"
174+
version: "0.12.5"
175175
meta:
176176
dependency: transitive
177177
description:
@@ -227,7 +227,7 @@ packages:
227227
name: pedantic
228228
url: "https://pub.dartlang.org"
229229
source: hosted
230-
version: "1.4.0"
230+
version: "1.5.0"
231231
pool:
232232
dependency: transitive
233233
description:
@@ -248,7 +248,7 @@ packages:
248248
name: quiver
249249
url: "https://pub.dartlang.org"
250250
source: hosted
251-
version: "2.0.1"
251+
version: "2.0.3"
252252
shelf:
253253
dependency: transitive
254254
description:
@@ -302,7 +302,7 @@ packages:
302302
name: source_span
303303
url: "https://pub.dartlang.org"
304304
source: hosted
305-
version: "1.5.4"
305+
version: "1.5.5"
306306
stack_trace:
307307
dependency: transitive
308308
description:
@@ -316,7 +316,7 @@ packages:
316316
name: stream_channel
317317
url: "https://pub.dartlang.org"
318318
source: hosted
319-
version: "1.6.8"
319+
version: "2.0.0"
320320
string_scanner:
321321
dependency: transitive
322322
description:
@@ -344,21 +344,21 @@ packages:
344344
name: test
345345
url: "https://pub.dartlang.org"
346346
source: hosted
347-
version: "1.5.3"
347+
version: "1.6.3"
348348
test_api:
349349
dependency: transitive
350350
description:
351351
name: test_api
352352
url: "https://pub.dartlang.org"
353353
source: hosted
354-
version: "0.2.2"
354+
version: "0.2.5"
355355
test_core:
356356
dependency: transitive
357357
description:
358358
name: test_core
359359
url: "https://pub.dartlang.org"
360360
source: hosted
361-
version: "0.2.1+1"
361+
version: "0.2.5"
362362
typed_data:
363363
dependency: transitive
364364
description:
@@ -409,5 +409,5 @@ packages:
409409
source: hosted
410410
version: "2.1.15"
411411
sdks:
412-
dart: ">=2.1.0 <3.0.0"
412+
dart: ">=2.2.0 <3.0.0"
413413
flutter: ">=0.1.4 <2.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ dependencies:
2222
dev_dependencies:
2323
flutter_test:
2424
sdk: flutter
25-
test: 1.5.3
25+
test: 1.6.3
2626

2727
flutter:

0 commit comments

Comments
 (0)