Skip to content

Commit 53fdd6e

Browse files
committed
Added FormBuilderCheckboxGroup. Closes #188
1 parent 1672864 commit 53fdd6e

File tree

4 files changed

+487
-6
lines changed

4 files changed

+487
-6
lines changed

example/lib/main.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,10 @@ class MyHomePageState extends State<MyHomePage> {
360360
},
361361
),
362362
SizedBox(height: 15),
363-
FormBuilderRadio(
363+
FormBuilderRadioGroup(
364364
decoration:
365365
InputDecoration(labelText: 'My chosen language'),
366366
attribute: 'best_language',
367-
leadingInput: true,
368367
onChanged: _onChanged,
369368
validators: [FormBuilderValidators.required()],
370369
options: ['Dart', 'Kotlin', 'Java', 'Swift', 'Objective-C']
@@ -424,12 +423,11 @@ class MyHomePageState extends State<MyHomePage> {
424423
isHalfAllowed: true,
425424
),
426425
SizedBox(height: 15),
427-
FormBuilderCheckboxList(
426+
FormBuilderCheckboxGroup(
428427
decoration:
429428
InputDecoration(labelText: 'The language of my people'),
430429
attribute: 'languages',
431430
initialValue: ['Dart'],
432-
leadingInput: true,
433431
options: [
434432
FormBuilderFieldOption(value: 'Dart'),
435433
FormBuilderFieldOption(value: 'Kotlin'),
@@ -513,8 +511,8 @@ class MyHomePageState extends State<MyHomePage> {
513511
decoration: const InputDecoration(labelText: 'Radio Group'),
514512
onChanged: _onChanged,
515513
options: [
516-
FormBuilderFieldOption(value: 'Male'),
517-
FormBuilderFieldOption(value: 'Female'),
514+
FormBuilderFieldOption(value: 'M', child: Text('Male')),
515+
FormBuilderFieldOption(value: 'F', child: Text('Female')),
518516
],
519517
),
520518
SizedBox(height: 15),

lib/flutter_form_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export 'package:signature/signature.dart';
55

66
export './src/fields/form_builder_checkbox.dart';
77
export './src/fields/form_builder_checkbox_list.dart';
8+
export './src/fields/form_builder_checkbox_group.dart';
89
export './src/fields/form_builder_chips_choice.dart';
910
export './src/fields/form_builder_chips_filter.dart';
1011
export './src/fields/form_builder_chips_input.dart';
@@ -28,6 +29,8 @@ export './src/fields/form_builder_switch.dart';
2829
export './src/fields/form_builder_text_field.dart';
2930
export './src/fields/form_builder_touch_spin.dart';
3031
export './src/fields/form_builder_typeahead.dart';
32+
export './src/widgets/grouped_checkbox.dart';
33+
export './src/widgets/grouped_radio.dart';
3134
export './src/form_builder.dart';
3235
export './src/form_builder_custom_field.dart';
3336
export './src/form_builder_field_option.dart';
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter/widgets.dart';
3+
import 'package:flutter_form_builder/flutter_form_builder.dart';
4+
import 'package:flutter_form_builder/src/widgets/grouped_checkbox.dart';
5+
6+
class FormBuilderCheckboxGroup<T> extends StatefulWidget {
7+
final String attribute;
8+
final List<FormFieldValidator> validators;
9+
final List<T> initialValue;
10+
final bool readOnly;
11+
final InputDecoration decoration;
12+
final ValueChanged onChanged;
13+
final ValueTransformer valueTransformer;
14+
final bool enabled;
15+
final FormFieldSetter onSaved;
16+
final bool autovalidate;
17+
final List<FormBuilderFieldOption> options;
18+
final Color activeColor;
19+
final Color checkColor;
20+
final Color focusColor;
21+
final Color hoverColor;
22+
final List<T> disabled;
23+
final MaterialTapTargetSize materialTapTargetSize;
24+
final bool tristate;
25+
final Axis wrapDirection;
26+
27+
final WrapAlignment wrapAlignment;
28+
29+
final double wrapSpacing;
30+
31+
final WrapAlignment wrapRunAlignment;
32+
33+
final double wrapRunSpacing;
34+
35+
final WrapCrossAlignment wrapCrossAxisAlignment;
36+
37+
final TextDirection wrapTextDirection;
38+
final VerticalDirection wrapVerticalDirection;
39+
final Widget separator;
40+
final ControlAffinity controlAffinity;
41+
42+
final GroupedCheckboxOrientation orientation;
43+
44+
FormBuilderCheckboxGroup({
45+
Key key,
46+
@required this.attribute,
47+
this.initialValue,
48+
this.readOnly = false,
49+
this.decoration = const InputDecoration(),
50+
this.onChanged,
51+
this.valueTransformer,
52+
this.enabled = true,
53+
this.onSaved,
54+
this.autovalidate = false,
55+
@required this.options,
56+
this.activeColor,
57+
this.checkColor,
58+
this.focusColor,
59+
this.hoverColor,
60+
this.disabled,
61+
this.materialTapTargetSize,
62+
this.tristate = false,
63+
this.wrapDirection = Axis.horizontal,
64+
this.wrapAlignment = WrapAlignment.start,
65+
this.wrapSpacing = 0.0,
66+
this.wrapRunAlignment = WrapAlignment.start,
67+
this.wrapRunSpacing = 0.0,
68+
this.wrapCrossAxisAlignment = WrapCrossAlignment.start,
69+
this.wrapTextDirection,
70+
this.wrapVerticalDirection = VerticalDirection.down,
71+
this.separator,
72+
this.controlAffinity = ControlAffinity.leading,
73+
this.orientation = GroupedCheckboxOrientation.wrap,
74+
this.validators = const [],
75+
}) : super(
76+
key: key,
77+
);
78+
79+
@override
80+
_FormBuilderCheckboxGroupState<T> createState() =>
81+
_FormBuilderCheckboxGroupState();
82+
}
83+
84+
class _FormBuilderCheckboxGroupState<T>
85+
extends State<FormBuilderCheckboxGroup> {
86+
bool _readOnly = false;
87+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
88+
FormBuilderState _formState;
89+
dynamic _initialValue;
90+
91+
@override
92+
void initState() {
93+
_formState = FormBuilder.of(context);
94+
_formState?.registerFieldKey(widget.attribute, _fieldKey);
95+
_initialValue = widget.initialValue ??
96+
(_formState.initialValue.containsKey(widget.attribute)
97+
? _formState.initialValue[widget.attribute]
98+
: null);
99+
super.initState();
100+
}
101+
102+
@override
103+
void dispose() {
104+
_formState?.unregisterFieldKey(widget.attribute);
105+
super.dispose();
106+
}
107+
108+
@override
109+
Widget build(BuildContext context) {
110+
_readOnly = _formState?.readOnly == true || widget.readOnly;
111+
112+
return FormField(
113+
key: _fieldKey,
114+
enabled: !_readOnly,
115+
initialValue: _initialValue,
116+
validator: (val) =>
117+
FormBuilderValidators.validateValidators(val, widget.validators),
118+
onSaved: (val) {
119+
var transformed;
120+
if (widget.valueTransformer != null) {
121+
transformed = widget.valueTransformer(val);
122+
_formState?.setAttributeValue(widget.attribute, transformed);
123+
} else {
124+
_formState?.setAttributeValue(widget.attribute, val);
125+
}
126+
widget.onSaved?.call(transformed ?? val);
127+
},
128+
builder: (FormFieldState field) {
129+
return InputDecorator(
130+
decoration: widget.decoration.copyWith(
131+
enabled: _readOnly,
132+
errorText: field.errorText,
133+
),
134+
child: GroupedCheckbox(
135+
orientation: widget.orientation,
136+
value: widget.initialValue,
137+
options: widget.options,
138+
onChanged: (val) {
139+
field.didChange(val);
140+
widget.onChanged?.call(val);
141+
},
142+
activeColor: widget.activeColor,
143+
focusColor: widget.focusColor,
144+
checkColor: widget.checkColor,
145+
materialTapTargetSize: widget.materialTapTargetSize,
146+
disabled: widget.disabled,
147+
hoverColor: widget.hoverColor,
148+
tristate: widget.tristate,
149+
wrapAlignment: widget.wrapAlignment,
150+
wrapCrossAxisAlignment: widget.wrapCrossAxisAlignment,
151+
wrapDirection: widget.wrapDirection,
152+
wrapRunAlignment: widget.wrapRunAlignment,
153+
wrapRunSpacing: widget.wrapRunSpacing,
154+
wrapSpacing: widget.wrapSpacing,
155+
wrapTextDirection: widget.wrapTextDirection,
156+
wrapVerticalDirection: widget.wrapVerticalDirection,
157+
separator: widget.separator,
158+
controlAffinity: widget.controlAffinity,
159+
),
160+
);
161+
},
162+
);
163+
}
164+
}

0 commit comments

Comments
 (0)