Skip to content

Commit 4f358c7

Browse files
committed
Added CheckboxGroup field and fixed RadioGroup. Closes #188, #335
1 parent d3918b0 commit 4f358c7

File tree

8 files changed

+780
-42
lines changed

8 files changed

+780
-42
lines changed

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
);
245245
inputPaths = (
246246
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
247-
"${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework",
247+
"${PODS_ROOT}/../Flutter/Flutter.framework",
248248
);
249249
name = "[CP] Embed Pods Frameworks";
250250
outputPaths = (

example/lib/sources/complete_form.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class CompleteFormState extends State<CompleteForm> {
3939
initialValue: {
4040
'movie_rating': 5,
4141
'best_language': 'Dart',
42+
'age': '13',
43+
'gender': 'Male'
4244
},
4345
readOnly: false,
4446
child: Column(
@@ -232,6 +234,7 @@ class CompleteFormState extends State<CompleteForm> {
232234
FormBuilderValidators.numeric(context),
233235
FormBuilderValidators.max(context, 70),
234236
]),
237+
// initialValue: '12',
235238
keyboardType: TextInputType.number,
236239
textInputAction: TextInputAction.next,
237240
),
@@ -281,7 +284,7 @@ class CompleteFormState extends State<CompleteForm> {
281284
}
282285
},
283286
),
284-
FormBuilderRadioList(
287+
FormBuilderRadioGroup(
285288
decoration: InputDecoration(
286289
labelText: 'My chosen language',
287290
),
@@ -295,6 +298,7 @@ class CompleteFormState extends State<CompleteForm> {
295298
child: Text('$lang'),
296299
))
297300
.toList(growable: false),
301+
controlAffinity: ControlAffinity.trailing,
298302
),
299303
FormBuilderSegmentedControl(
300304
decoration:
@@ -336,7 +340,7 @@ class CompleteFormState extends State<CompleteForm> {
336340
max: 5.0,
337341
onChanged: _onChanged,
338342
),
339-
FormBuilderCheckboxList(
343+
FormBuilderCheckboxGroup(
340344
decoration:
341345
InputDecoration(labelText: 'The language of my people'),
342346
attribute: 'languages',
@@ -349,6 +353,11 @@ class CompleteFormState extends State<CompleteForm> {
349353
FormBuilderFieldOption(value: 'Objective-C'),
350354
],
351355
onChanged: _onChanged,
356+
separator: VerticalDivider(
357+
width: 10,
358+
thickness: 5,
359+
color: Colors.red,
360+
),
352361
),
353362
FormBuilderField(
354363
attribute: 'custom',
@@ -481,7 +490,8 @@ class CompleteFormState extends State<CompleteForm> {
481490
width: 20,
482491
),
483492
Expanded(
484-
child: MaterialButton(
493+
child: OutlineButton(
494+
focusNode: FocusNode(),
485495
color: Theme.of(context).accentColor,
486496
child: Text(
487497
'Reset',

lib/flutter_form_builder.dart

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

77
export './localization/form_builder_localizations.dart';
88
export './src/fields/form_builder_checkbox.dart';
9+
export './src/fields/form_builder_checkbox_group.dart';
910
export './src/fields/form_builder_checkbox_list.dart';
1011
export './src/fields/form_builder_chips_input.dart';
1112
export './src/fields/form_builder_choice_chips.dart';
@@ -33,6 +34,8 @@ export './src/fields/form_builder_text_field.dart';
3334
export './src/fields/form_builder_touch_spin.dart';
3435
export './src/fields/form_builder_typeahead.dart';
3536
export './src/form_builder.dart';
37+
export './src/widgets/grouped_checkbox.dart';
38+
export './src/widgets/grouped_radio.dart';
3639
export './src/form_builder_field.dart';
3740
export './src/form_builder_field_option.dart';
3841
export './src/form_builder_validators.dart';
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 FormBuilderField<List<T>> {
7+
8+
// final Widget secondary;
9+
10+
FormBuilderCheckboxGroup({
11+
Key key,
12+
//From Super
13+
@required String attribute,
14+
FormFieldValidator validator,
15+
List<T> initialValue,
16+
bool readOnly = false,
17+
InputDecoration decoration = const InputDecoration(),
18+
ValueChanged onChanged,
19+
ValueTransformer valueTransformer,
20+
bool enabled = true,
21+
FormFieldSetter onSaved,
22+
bool autovalidate = false,
23+
VoidCallback onReset,
24+
FocusNode focusNode,
25+
@required List<FormBuilderFieldOption> options,
26+
Color activeColor,
27+
Color checkColor,
28+
Color focusColor,
29+
Color hoverColor,
30+
List<T> disabled,
31+
MaterialTapTargetSize materialTapTargetSize,
32+
bool tristate = false,
33+
Axis wrapDirection = Axis.horizontal,
34+
WrapAlignment wrapAlignment = WrapAlignment.start,
35+
double wrapSpacing = 0.0,
36+
WrapAlignment wrapRunAlignment = WrapAlignment.start,
37+
double wrapRunSpacing = 0.0,
38+
WrapCrossAlignment wrapCrossAxisAlignment = WrapCrossAlignment.start,
39+
TextDirection wrapTextDirection,
40+
VerticalDirection wrapVerticalDirection = VerticalDirection.down,
41+
Widget separator,
42+
ControlAffinity controlAffinity = ControlAffinity.leading,
43+
GroupedCheckboxOrientation orientation = GroupedCheckboxOrientation.wrap,
44+
}) : super(
45+
key: key,
46+
initialValue: initialValue,
47+
attribute: attribute,
48+
validator: validator,
49+
valueTransformer: valueTransformer,
50+
onChanged: onChanged,
51+
readOnly: readOnly,
52+
autovalidate: autovalidate,
53+
onSaved: onSaved,
54+
enabled: enabled,
55+
onReset: onReset,
56+
decoration: decoration,
57+
builder: (FormFieldState field) {
58+
final _FormBuilderCheckboxGroupState<T> state = field;
59+
60+
return InputDecorator(
61+
decoration: decoration.copyWith(
62+
enabled: !state.readOnly,
63+
errorText: field.errorText,
64+
),
65+
child: GroupedCheckbox(
66+
orientation: orientation,
67+
value: initialValue,
68+
options: options,
69+
onChanged: (val){
70+
field.didChange(val);
71+
},
72+
activeColor: activeColor,
73+
focusColor: focusColor,
74+
checkColor: checkColor,
75+
materialTapTargetSize: materialTapTargetSize,
76+
disabled: disabled,
77+
hoverColor: hoverColor,
78+
tristate: tristate,
79+
wrapAlignment: wrapAlignment,
80+
wrapCrossAxisAlignment: wrapCrossAxisAlignment,
81+
wrapDirection: wrapDirection,
82+
wrapRunAlignment: wrapRunAlignment,
83+
wrapRunSpacing: wrapRunSpacing,
84+
wrapSpacing: wrapSpacing,
85+
wrapTextDirection: wrapTextDirection,
86+
wrapVerticalDirection: wrapVerticalDirection,
87+
separator: separator,
88+
controlAffinity: controlAffinity,
89+
),
90+
);
91+
},
92+
);
93+
94+
@override
95+
_FormBuilderCheckboxGroupState<T> createState() =>
96+
_FormBuilderCheckboxGroupState();
97+
}
98+
99+
class _FormBuilderCheckboxGroupState<T> extends FormBuilderFieldState<List<T>> {
100+
@override
101+
FormBuilderCheckboxGroup<T> get widget =>
102+
super.widget as FormBuilderCheckboxGroup;
103+
}

lib/src/fields/form_builder_radio_group.dart

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/widgets.dart';
33
import 'package:flutter_form_builder/flutter_form_builder.dart';
4+
import 'package:flutter_form_builder/src/widgets/grouped_checkbox.dart';
5+
import 'package:flutter_form_builder/src/widgets/grouped_radio.dart';
46
import 'package:group_radio_button/group_radio_button.dart';
57

68
class FormBuilderRadioGroup<T> extends FormBuilderField<T> {
7-
final bool leadingInput;
8-
final List<FormBuilderFieldOption> options;
9-
final MaterialTapTargetSize materialTapTargetSize;
10-
final Color activeColor;
11-
final EdgeInsets contentPadding;
12-
final Axis direction;
13-
final MainAxisAlignment horizontalAlignment;
14-
final double spaceBetween;
159

1610
FormBuilderRadioGroup({
1711
Key key,
@@ -28,14 +22,23 @@ class FormBuilderRadioGroup<T> extends FormBuilderField<T> {
2822
bool autovalidate = false,
2923
VoidCallback onReset,
3024
FocusNode focusNode,
31-
@required this.options,
32-
this.leadingInput = false,
33-
this.materialTapTargetSize,
34-
this.activeColor,
35-
this.contentPadding = const EdgeInsets.all(0.0),
36-
this.direction = Axis.horizontal,
37-
this.horizontalAlignment = MainAxisAlignment.start,
38-
this.spaceBetween,
25+
@required List<FormBuilderFieldOption> options,
26+
Color activeColor,
27+
Color focusColor,
28+
Color hoverColor,
29+
List<T> disabled,
30+
MaterialTapTargetSize materialTapTargetSize,
31+
Axis wrapDirection = Axis.horizontal,
32+
WrapAlignment wrapAlignment = WrapAlignment.start,
33+
double wrapSpacing = 0.0,
34+
WrapAlignment wrapRunAlignment = WrapAlignment.start,
35+
double wrapRunSpacing = 0.0,
36+
WrapCrossAlignment wrapCrossAxisAlignment = WrapCrossAlignment.start,
37+
TextDirection wrapTextDirection,
38+
VerticalDirection wrapVerticalDirection = VerticalDirection.down,
39+
Widget separator,
40+
ControlAffinity controlAffinity = ControlAffinity.leading,
41+
GroupedRadioOrientation orientation = GroupedRadioOrientation.wrap,
3942
}) : super(
4043
key: key,
4144
initialValue: initialValue,
@@ -57,27 +60,28 @@ class FormBuilderRadioGroup<T> extends FormBuilderField<T> {
5760
enabled: !state.readOnly,
5861
errorText: decoration?.errorText ?? field.errorText,
5962
),
60-
child: RadioGroup.builder(
61-
groupValue: field.value,
62-
onChanged: state.readOnly
63-
? null
64-
: (value) {
65-
state.requestFocus();
66-
field.didChange(value);
67-
},
68-
items: options
69-
.map((option) => option.value)
70-
.toList(growable: false),
71-
itemBuilder: (item) {
72-
// return item;
73-
return RadioButtonBuilder(
74-
item.toString(),
75-
textPosition: RadioButtonTextPosition.right,
76-
);
63+
child: GroupedRadio(
64+
orientation: orientation,
65+
value: initialValue,
66+
options: options,
67+
onChanged: (val){
68+
field.didChange(val);
7769
},
78-
direction: direction,
79-
horizontalAlignment: horizontalAlignment,
80-
spacebetween: spaceBetween,
70+
activeColor: activeColor,
71+
focusColor: focusColor,
72+
materialTapTargetSize: materialTapTargetSize,
73+
disabled: disabled,
74+
hoverColor: hoverColor,
75+
wrapAlignment: wrapAlignment,
76+
wrapCrossAxisAlignment: wrapCrossAxisAlignment,
77+
wrapDirection: wrapDirection,
78+
wrapRunAlignment: wrapRunAlignment,
79+
wrapRunSpacing: wrapRunSpacing,
80+
wrapSpacing: wrapSpacing,
81+
wrapTextDirection: wrapTextDirection,
82+
wrapVerticalDirection: wrapVerticalDirection,
83+
separator: separator,
84+
controlAffinity: controlAffinity,
8185
),
8286
);
8387
},

lib/src/form_builder_field_option.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:flutter/material.dart';
22

3-
class FormBuilderFieldOption extends StatelessWidget {
3+
class FormBuilderFieldOption<T> extends StatelessWidget {
44
final Widget child;
5-
final dynamic value;
5+
final T value;
66

77
const FormBuilderFieldOption({
88
Key key,

0 commit comments

Comments
 (0)