Skip to content

Commit 26a1163

Browse files
committed
Added leadingInput option for CheckboxList
1 parent d64dc9e commit 26a1163

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [3.1.0] - 15-May-2019
2+
* Added `leadingInput` option for CheckboxList, Checkbox and Radio - Allows the option to have the input before its label (left)
3+
14
## [3.0.1] - 28-April-2019
25
* Fixed bug in where `focuNode` for `FormBuilderTextField` is ignored. Closes [#53](https://github.com/danvick/flutter_form_builder/issues/53)
36
* Fixed bug in where `textEditingConfiguration` for `FormBuilderTypeAhead` ignored

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ Column(
6969
],
7070
),
7171
FormBuilderRadio(
72-
decoration:
73-
InputDecoration(labelText: 'My chosen language'),
72+
decoration: InputDecoration(labelText: 'My chosen language'),
73+
leadingInput: true,
7474
attribute: "best_language",
7575
validators: [FormBuilderValidators.required()],
7676
options: [
@@ -303,7 +303,7 @@ This package is dependent on the following packages and plugins:
303303
- [X] Improve documentation by showing complete list of input types and their usage and options
304304
- [X] Create a `transformer` function option that will convert field value when field id saved - can be used to convert string to number, change to uppercase etc.
305305
- [X] Assert no duplicates in `FormBuilderInput`s `attribute` names
306-
- [ ] Allow options for Checkboxes and Radios to appear left or right
306+
- [X] Allow options for Checkboxes and Radios to appear left or right - Done via `leadingInput` by [Sven Schöne](https://github.com/SvenSchoene)
307307

308308
### New FormBuilder inputs
309309
- [X] SignaturePad - Based on [https://pub.dartlang.org/packages/signature](https://pub.dartlang.org/packages/signature)

example/lib/main.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class MyHomePageState extends State<MyHomePage> {
168168
attribute: 'accept_terms',
169169
initialValue: false,
170170
onChanged: _onChanged,
171+
leadingInput: true,
171172
label: Text(
172173
"I have read and agree to the terms and conditions"),
173174
validators: [
@@ -233,6 +234,7 @@ class MyHomePageState extends State<MyHomePage> {
233234
decoration:
234235
InputDecoration(labelText: 'My chosen language'),
235236
attribute: "best_language",
237+
leadingInput: true,
236238
onChanged: _onChanged,
237239
validators: [FormBuilderValidators.required()],
238240
options: [
@@ -267,8 +269,10 @@ class MyHomePageState extends State<MyHomePage> {
267269
initialValue: 10,
268270
step: 1,
269271
validators: [
270-
(val){
271-
if(!_fbKey.currentState.fields["accept_terms_switch"].currentState.value && val >= 10){
272+
(val) {
273+
if (!_fbKey.currentState.fields["accept_terms_switch"]
274+
.currentState.value &&
275+
val >= 10) {
272276
return "You can only put more than 10 if you've accepted terms";
273277
}
274278
}
@@ -287,6 +291,7 @@ class MyHomePageState extends State<MyHomePage> {
287291
labelText: "The language of my people"),
288292
attribute: "languages",
289293
initialValue: ["Dart"],
294+
leadingInput: true,
290295
options: [
291296
FormBuilderFieldOption(value: "Dart"),
292297
FormBuilderFieldOption(value: "Kotlin"),

lib/src/fields/form_builder_checkbox_list.dart

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ class FormBuilderCheckboxList extends StatefulWidget {
1212
final ValueTransformer valueTransformer;
1313

1414
final List<FormBuilderFieldOption> options;
15+
final bool leadingInput;
1516

1617
FormBuilderCheckboxList({
1718
@required this.attribute,
1819
@required this.options,
1920
this.initialValue = const [],
2021
this.validators = const [],
2122
this.readonly = false,
23+
this.leadingInput = false,
2224
this.decoration = const InputDecoration(),
2325
this.onChanged,
2426
this.valueTransformer,
@@ -48,6 +50,34 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
4850
super.dispose();
4951
}
5052

53+
Widget _checkbox(FormFieldState<dynamic> field, int i) {
54+
return Checkbox(
55+
value: field.value.contains(widget.options[i].value),
56+
onChanged: _readonly
57+
? null
58+
: (bool value) {
59+
var currValue = field.value;
60+
if (value)
61+
currValue.add(widget.options[i].value);
62+
else
63+
currValue.remove(widget.options[i].value);
64+
field.didChange(currValue);
65+
if (widget.onChanged != null)
66+
widget.onChanged(currValue);
67+
},
68+
);
69+
}
70+
71+
Widget _leading(FormFieldState<dynamic> field, int i) {
72+
if (widget.leadingInput) return _checkbox(field, i);
73+
return null;
74+
}
75+
76+
Widget _trailing(FormFieldState<dynamic> field, int i) {
77+
if (!widget.leadingInput) return _checkbox(field, i);
78+
return null;
79+
}
80+
5181
@override
5282
Widget build(BuildContext context) {
5383
return FormField(
@@ -76,21 +106,8 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
76106
dense: true,
77107
isThreeLine: false,
78108
contentPadding: EdgeInsets.all(0.0),
79-
leading: Checkbox(
80-
value: field.value.contains(widget.options[i].value),
81-
onChanged: _readonly
82-
? null
83-
: (bool value) {
84-
var currValue = field.value;
85-
if (value)
86-
currValue.add(widget.options[i].value);
87-
else
88-
currValue.remove(widget.options[i].value);
89-
field.didChange(currValue);
90-
if (widget.onChanged != null)
91-
widget.onChanged(currValue);
92-
},
93-
),
109+
leading: _leading(field, i),
110+
trailing: _trailing(field, i),
94111
title: Text(
95112
"${widget.options[i].label ?? widget.options[i].value}"),
96113
onTap: _readonly

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_form_builder
22
description: Package to build Material Form with fields like TextField, DropDown, Switches etc. with ability to create custom FormFields and composability and reuse validation functions.
3-
version: 3.0.1
3+
version: 3.1.0
44
author: Danvick Miller <[email protected]>
55
homepage: https://github.com/danvick/flutter_form_builder
66

0 commit comments

Comments
 (0)