Skip to content

Commit 71178b4

Browse files
committed
Form’s readonly applies to all, check if form builder parent exist calling setValue
Also added icon to signature’s clear button and colour to red
1 parent b6e131a commit 71178b4

18 files changed

+437
-294
lines changed

.idea/workspace.xml

Lines changed: 211 additions & 197 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class MyHomePageState extends State<MyHomePage> {
4949
context,
5050
key: _fbKey,
5151
autovalidate: true,
52+
readonly: true,
5253
child: Column(
5354
children: <Widget>[
5455
FormBuilderField(
@@ -62,7 +63,7 @@ class MyHomePageState extends State<MyHomePage> {
6263
builder: (FormFieldState<dynamic> field) {
6364
return InputDecorator(
6465
decoration: InputDecoration(
65-
errorText: field.errorText,
66+
labelText: "Select option",
6667
contentPadding:
6768
EdgeInsets.only(top: 10.0, bottom: 0.0),
6869
border: InputBorder.none,
@@ -87,7 +88,7 @@ class MyHomePageState extends State<MyHomePage> {
8788
FormBuilderChipsInput(
8889
decoration: InputDecoration(labelText: "Chips"),
8990
attribute: 'chips_test',
90-
// require: true,
91+
readonly: true,
9192
initialValue: [
9293
Contact('Andrew', '[email protected]',
9394
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
@@ -143,6 +144,7 @@ class MyHomePageState extends State<MyHomePage> {
143144
format: DateFormat("yyyy-MM-dd"),
144145
decoration:
145146
InputDecoration(labelText: "Appointment Time"),
147+
readonly: true,
146148
),
147149
FormBuilderSlider(
148150
attribute: "slider",

lib/src/form_builder.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ class FormBuilder extends StatefulWidget {
2929
class FormBuilderState extends State<FormBuilder> {
3030
//FIXME: Find way to assert no duplicates in control attributes
3131
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
32+
3233
Map<String, dynamic> _value = {};
3334

3435
Map<String, dynamic> get value => _value;
3536

36-
setValue(String attribute, dynamic value){
37+
bool get readonly => widget.readonly;
38+
39+
setValue(String attribute, dynamic value) {
3740
setState(() {
3841
_value[attribute] = value;
3942
});
@@ -58,8 +61,8 @@ class FormBuilderState extends State<FormBuilder> {
5861
child: widget.child,
5962
autovalidate: widget.autovalidate,
6063
onWillPop: widget.onWillPop,
61-
onChanged: (){
62-
if(widget.onChanged != null){
64+
onChanged: () {
65+
if (widget.onChanged != null) {
6366
save();
6467
widget.onChanged(value);
6568
}

lib/src/form_builder_field.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class FormBuilderFieldState<T> extends State<FormBuilderField<T>> {
2121
Widget build(BuildContext context) {
2222
/*return widget.formField
2323
..onSaved = (T val) {
24-
FormBuilder.of(context).setValue(widget.attribute, val);
24+
FormBuilder.of(context)?.setValue(widget.attribute, val);
2525
if (widget.formField.onSaved != null) widget.formField.onSaved(val);
2626
}
2727
..validator = (val) {
@@ -36,7 +36,7 @@ class FormBuilderFieldState<T> extends State<FormBuilderField<T>> {
3636
key: Key(widget.attribute),
3737
child: FormField(
3838
onSaved: (val) {
39-
FormBuilder.of(context).setValue(widget.attribute, val);
39+
FormBuilder.of(context)?.setValue(widget.attribute, val);
4040
if (widget.formField.onSaved != null) widget.formField.onSaved(val);
4141
},
4242
validator: (val) {

lib/src/inputs/form_builder_checkbox.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ class FormBuilderCheckbox extends StatefulWidget {
2525
}
2626

2727
class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
28+
bool _readonly = false;
29+
30+
@override
31+
void initState() {
32+
_readonly =
33+
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
34+
super.initState();
35+
}
36+
2837
@override
2938
Widget build(BuildContext context) {
3039
return FormField(
3140
// key: _fieldKey,
32-
enabled: !(widget.readonly),
41+
enabled: !_readonly,
3342
initialValue: widget.initialValue ?? false,
3443
validator: (val) {
3544
for (int i = 0; i < widget.validators.length; i++) {
@@ -43,7 +52,7 @@ class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
4352
builder: (FormFieldState<dynamic> field) {
4453
return InputDecorator(
4554
decoration: widget.decoration.copyWith(
46-
enabled: !(widget.readonly),
55+
enabled: !_readonly,
4756
errorText: field.errorText,
4857
),
4958
child: ListTile(
@@ -53,18 +62,18 @@ class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
5362
title: widget.label,
5463
trailing: Checkbox(
5564
value: field.value ?? false,
56-
onChanged: (widget.readonly)
65+
onChanged: _readonly
5766
? null
5867
: (bool value) {
59-
field.didChange(value);
60-
},
68+
field.didChange(value);
69+
},
6170
),
62-
onTap: (widget.readonly)
71+
onTap: _readonly
6372
? null
6473
: () {
65-
bool newValue = !(field.value ?? false);
66-
field.didChange(newValue);
67-
},
74+
bool newValue = !(field.value ?? false);
75+
field.didChange(newValue);
76+
},
6877
),
6978
);
7079
},

lib/src/inputs/form_builder_checkbox_list.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ class FormBuilderCheckboxList extends StatefulWidget {
2626
}
2727

2828
class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
29+
bool _readonly = false;
30+
31+
@override
32+
void initState() {
33+
_readonly =
34+
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
35+
super.initState();
36+
}
37+
2938
@override
3039
Widget build(BuildContext context) {
3140
return FormField(
3241
// key: _fieldKey,
33-
enabled: !(widget.readonly),
42+
enabled: !_readonly,
3443
initialValue: widget.initialValue,
3544
validator: (val) {
3645
for (int i = 0; i < widget.validators.length; i++) {
@@ -51,7 +60,7 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
5160
contentPadding: EdgeInsets.all(0.0),
5261
leading: Checkbox(
5362
value: field.value.contains(widget.options[i].value),
54-
onChanged: (widget.readonly || widget.readonly)
63+
onChanged: _readonly
5564
? null
5665
: (bool value) {
5766
var currValue = field.value;
@@ -64,7 +73,7 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
6473
),
6574
title: Text(
6675
"${widget.options[i].label ?? widget.options[i].value}"),
67-
onTap: (widget.readonly || widget.readonly)
76+
onTap: _readonly
6877
? null
6978
: () {
7079
var currentValue = field.value;
@@ -82,7 +91,7 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
8291
}
8392
return InputDecorator(
8493
decoration: widget.decoration.copyWith(
85-
enabled: !(widget.readonly || widget.readonly),
94+
enabled: !_readonly,
8695
errorText: field.errorText,
8796
contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
8897
border: InputBorder.none,

lib/src/inputs/form_builder_chips_input.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class FormBuilderChipsInput<T> extends StatefulWidget {
1111
final InputDecoration decoration;
1212

1313
final ChipsInputSuggestions findSuggestions;
14+
1415
// final ValueChanged<List<T>> onChanged;
1516
final ValueChanged<T> onChipTapped;
1617
final ChipsBuilder<T> chipBuilder;
@@ -35,13 +36,22 @@ class FormBuilderChipsInput<T> extends StatefulWidget {
3536
}
3637

3738
class _FormBuilderChipsInputState extends State<FormBuilderChipsInput> {
39+
bool _readonly = false;
40+
41+
@override
42+
void initState() {
43+
_readonly =
44+
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
45+
super.initState();
46+
}
47+
3848
@override
3949
Widget build(BuildContext context) {
4050
return SizedBox(
4151
// height: 200.0,
4252
child: FormField(
4353
// key: _fieldKey,
44-
enabled: !(widget.readonly || widget.readonly),
54+
enabled: !_readonly,
4555
initialValue: widget.initialValue,
4656
validator: (val) {
4757
for (int i = 0; i < widget.validators.length; i++) {
@@ -55,9 +65,9 @@ class _FormBuilderChipsInputState extends State<FormBuilderChipsInput> {
5565
builder: (FormFieldState<dynamic> field) {
5666
return ChipsInput(
5767
initialValue: field.value,
58-
enabled: !(widget.readonly || widget.readonly),
68+
enabled: !_readonly,
5969
decoration: widget.decoration.copyWith(
60-
enabled: !(widget.readonly || widget.readonly),
70+
enabled: !_readonly,
6171
errorText: field.errorText,
6272
),
6373
findSuggestions: widget.findSuggestions,

lib/src/inputs/form_builder_date_time_picker.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class FormBuilderDateTimePicker extends StatefulWidget {
3333
}
3434

3535
class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
36+
bool _readonly = false;
37+
38+
@override
39+
void initState() {
40+
_readonly =
41+
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
42+
super.initState();
43+
}
44+
3645
final _dateTimeFormats = {
3746
InputType.both: DateFormat("EEEE, MMMM d, yyyy 'at' h:mma"),
3847
InputType.date: DateFormat('yyyy-MM-dd'),
@@ -47,14 +56,14 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
4756
format: widget.format != null
4857
? widget.format
4958
: _dateTimeFormats[widget.inputType],
50-
enabled: !widget.readonly,
59+
enabled: !_readonly,
5160
firstDate: widget.firstDate,
5261
lastDate: widget.lastDate,
5362
decoration: widget.decoration.copyWith(
54-
enabled: !(widget.readonly),
63+
enabled: !_readonly,
5564
),
5665
onSaved: (val) {
57-
FormBuilder.of(context).setValue(widget.attribute, val);
66+
FormBuilder.of(context)?.setValue(widget.attribute, val);
5867
},
5968
validator: (val) {
6069
for (int i = 0; i < widget.validators.length; i++) {

lib/src/inputs/form_builder_dropdown.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ class FormBuilderDropdown extends StatefulWidget {
3939
}
4040

4141
class _FormBuilderDropdownState extends State<FormBuilderDropdown> {
42+
bool _readonly = false;
43+
44+
@override
45+
void initState() {
46+
_readonly =
47+
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
48+
super.initState();
49+
}
50+
4251
@override
4352
Widget build(BuildContext context) {
4453
return FormField(
45-
enabled: !(widget.readonly),
54+
enabled: !_readonly,
4655
initialValue: widget.initialValue,
4756
validator: (val) {
4857
for (int i = 0; i < widget.validators.length; i++) {
@@ -69,7 +78,7 @@ class _FormBuilderDropdownState extends State<FormBuilderDropdown> {
6978
disabledHint: widget.disabledHint,
7079
elevation: widget.elevation,
7180
iconSize: widget.iconSize,
72-
onChanged: (value) {
81+
onChanged: _readonly ? null : (value) {
7382
field.didChange(value);
7483
},
7584
),

lib/src/inputs/form_builder_radio.dart

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ class FormBuilderRadio extends StatefulWidget {
2525
}
2626

2727
class _FormBuilderRadioState extends State<FormBuilderRadio> {
28+
bool _readonly = false;
29+
30+
@override
31+
void initState() {
32+
_readonly =
33+
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
34+
super.initState();
35+
}
36+
2837
@override
2938
Widget build(BuildContext context) {
3039
return FormField(
3140
// key: _fieldKey,
32-
enabled: !widget.readonly && !widget.readonly,
41+
enabled: !_readonly && !_readonly,
3342
initialValue: widget.initialValue,
3443
validator: (val) {
3544
for (int i = 0; i < widget.validators.length; i++) {
@@ -49,22 +58,22 @@ class _FormBuilderRadioState extends State<FormBuilderRadio> {
4958
isThreeLine: false,
5059
contentPadding: EdgeInsets.all(0.0),
5160
leading: null,
52-
title: Text(
53-
"${widget.options[i].label ?? widget.options[i].value}"),
61+
title:
62+
Text("${widget.options[i].label ?? widget.options[i].value}"),
5463
trailing: Radio<dynamic>(
5564
value: widget.options[i].value,
5665
groupValue: field.value,
57-
onChanged: (widget.readonly || widget.readonly)
66+
onChanged: _readonly
5867
? null
5968
: (dynamic value) {
60-
field.didChange(value);
61-
},
69+
field.didChange(value);
70+
},
6271
),
63-
onTap: (widget.readonly || widget.readonly)
72+
onTap: _readonly
6473
? null
6574
: () {
66-
field.didChange(widget.options[i].value);
67-
},
75+
field.didChange(widget.options[i].value);
76+
},
6877
),
6978
Divider(
7079
height: 0.0,
@@ -73,7 +82,7 @@ class _FormBuilderRadioState extends State<FormBuilderRadio> {
7382
}
7483
return InputDecorator(
7584
decoration: widget.decoration.copyWith(
76-
enabled: !(widget.readonly || widget.readonly),
85+
enabled: !_readonly,
7786
errorText: field.errorText,
7887
contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
7988
border: InputBorder.none,

0 commit comments

Comments
 (0)