Skip to content

Commit 194610b

Browse files
committed
Started working on radio group
1 parent f8e55d6 commit 194610b

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

example/lib/main.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class MyApp extends StatelessWidget {
1515
title: 'Flutter FormBuilder Demo',
1616
theme: ThemeData(
1717
primarySwatch: Colors.blue,
18-
brightness: Brightness.dark,
18+
// brightness: Brightness.dark,
1919
inputDecorationTheme: InputDecorationTheme(
2020
// labelStyle: TextStyle(color: Colors.purple),
2121
border: OutlineInputBorder(
2222
gapPadding: 10,
23-
borderSide: BorderSide(color: Colors.purple),
2423
),
24+
2525
),
2626
),
2727
home: MyHomePage(),
@@ -287,6 +287,10 @@ class MyHomePageState extends State<MyHomePage> {
287287
],
288288
),
289289
SizedBox(height: 15),
290+
Text(
291+
"This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.",
292+
style: Theme.of(context).inputDecorationTheme.labelStyle,
293+
),
290294
FormBuilderTextField(
291295
attribute: "age",
292296
decoration: InputDecoration(
@@ -407,9 +411,10 @@ class MyHomePageState extends State<MyHomePage> {
407411
.toList(growable: false),
408412
),
409413
SizedBox(height: 15),
410-
FormBuilderRadio(
414+
FormBuilderRadioGroup(
411415
decoration: InputDecoration(labelText: 'Pick a number'),
412416
attribute: "number",
417+
readOnly: true,
413418
options: [
414419
FormBuilderFieldOption(
415420
value: 1,

lib/flutter_form_builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export './src/fields/form_builder_image_picker.dart';
1616
export './src/fields/form_builder_image_picker.dart';
1717
export './src/fields/form_builder_phone_field.dart';
1818
export './src/fields/form_builder_radio.dart';
19+
export './src/fields/form_builder_radio_group.dart';
1920
export './src/fields/form_builder_range_slider.dart';
2021
export './src/fields/form_builder_rate.dart';
2122
export './src/fields/form_builder_segmented_control.dart';
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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:group_radio_button/group_radio_button.dart';
5+
6+
class FormBuilderRadioGroup extends StatefulWidget {
7+
final String attribute;
8+
final List<FormFieldValidator> validators;
9+
final dynamic initialValue;
10+
final bool readOnly;
11+
final InputDecoration decoration;
12+
final ValueChanged onChanged;
13+
final ValueTransformer valueTransformer;
14+
15+
final bool leadingInput;
16+
final List<FormBuilderFieldOption> options;
17+
final MaterialTapTargetSize materialTapTargetSize;
18+
final Color activeColor;
19+
final FormFieldSetter onSaved;
20+
final EdgeInsets contentPadding;
21+
22+
FormBuilderRadioGroup({
23+
Key key,
24+
@required this.attribute,
25+
@required this.options,
26+
this.initialValue,
27+
this.validators = const [],
28+
this.readOnly = false,
29+
this.decoration = const InputDecoration(),
30+
this.onChanged,
31+
this.valueTransformer,
32+
this.leadingInput = false,
33+
this.materialTapTargetSize,
34+
this.activeColor,
35+
this.onSaved,
36+
this.contentPadding = const EdgeInsets.all(0.0),
37+
}) : super(key: key);
38+
39+
@override
40+
_FormBuilderRadioGroupState createState() => _FormBuilderRadioGroupState();
41+
}
42+
43+
class _FormBuilderRadioGroupState extends State<FormBuilderRadioGroup> {
44+
bool _readOnly = false;
45+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
46+
FormBuilderState _formState;
47+
dynamic _initialValue;
48+
49+
@override
50+
void initState() {
51+
_formState = FormBuilder.of(context);
52+
_formState?.registerFieldKey(widget.attribute, _fieldKey);
53+
_initialValue = widget.initialValue ??
54+
(_formState.initialValue.containsKey(widget.attribute)
55+
? _formState.initialValue[widget.attribute]
56+
: null);
57+
super.initState();
58+
}
59+
60+
@override
61+
void dispose() {
62+
_formState?.unregisterFieldKey(widget.attribute);
63+
super.dispose();
64+
}
65+
66+
@override
67+
Widget build(BuildContext context) {
68+
_readOnly = (_formState?.readOnly == true) ? true : widget.readOnly;
69+
70+
return FormField(
71+
key: _fieldKey,
72+
enabled: !_readOnly,
73+
initialValue: _initialValue,
74+
validator: (val) {
75+
for (int i = 0; i < widget.validators.length; i++) {
76+
if (widget.validators[i](val) != null) {
77+
return widget.validators[i](val);
78+
}
79+
}
80+
return null;
81+
},
82+
onSaved: (val) {
83+
var transformed;
84+
if (widget.valueTransformer != null) {
85+
transformed = widget.valueTransformer(val);
86+
_formState?.setAttributeValue(widget.attribute, transformed);
87+
} else {
88+
_formState?.setAttributeValue(widget.attribute, val);
89+
}
90+
if (widget.onSaved != null) {
91+
widget.onSaved(transformed ?? val);
92+
}
93+
},
94+
builder: (FormFieldState<dynamic> field) {
95+
return InputDecorator(
96+
decoration: widget.decoration.copyWith(
97+
enabled: !_readOnly,
98+
errorText: field.errorText,
99+
),
100+
child: RadioGroup.builder(
101+
groupValue: field.value,
102+
onChanged: _readOnly ? null : (value) {
103+
FocusScope.of(context).requestFocus(FocusNode());
104+
field.didChange(value);
105+
if (widget.onChanged != null) widget.onChanged(value);
106+
},
107+
items: widget.options.map((option) => option.value).toList(growable: false),
108+
itemBuilder: (item) {
109+
return RadioButtonBuilder(
110+
item.toString(),
111+
textPosition: RadioButtonTextPosition.right,
112+
);
113+
},
114+
direction: Axis.horizontal,
115+
horizontalAlignment: MainAxisAlignment.start,
116+
spacebetween: 40,
117+
),
118+
);
119+
},
120+
);
121+
}
122+
}

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ packages:
165165
url: "https://pub.dartlang.org"
166166
source: hosted
167167
version: "1.2.0"
168+
group_radio_button:
169+
dependency: "direct main"
170+
description:
171+
name: group_radio_button
172+
url: "https://pub.dartlang.org"
173+
source: hosted
174+
version: "1.0.0"
168175
html:
169176
dependency: transitive
170177
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies:
2323
rating_bar: ^0.2.0
2424
country_pickers: ^1.2.1
2525
phone_number: ^0.6.2+2
26+
group_radio_button: ^1.0.0
2627

2728
dev_dependencies:
2829
flutter_test:

0 commit comments

Comments
 (0)