Skip to content

Commit fd5565c

Browse files
committed
Prevent readOnly attribute and its value being passed to final form value. Closes #501
1 parent 6e90a09 commit fd5565c

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

README.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@ Column(
323323
},
324324
),
325325
),
326-
SizedBox(
327-
width: 20,
328-
),
326+
SizedBox(width: 20),
329327
Expanded(
330328
child: MaterialButton(
331329
color: Theme.of(context).accentColor,
@@ -394,13 +392,14 @@ In order to create an input field in the form, along with the label, and any app
394392

395393
| Attribute | Type | Default | Required | Description |
396394
|-----------|-------|---------|-------------|----------|
397-
| `name` | `String` | `null` | `true` | This will form the key in the form value Map |
398-
| `initialValue` | `dynamic` | `null` | `false` | The initial value of the input field |
399-
| `readOnly` | `bool` | `false` | `false` | Determines whether the field widget will accept user input. This value will be ignored if the `readOnly` attribute of `FormBuilder` widget is set to `true` |
400-
| `decoration` | `InputDecoration` | `InputDecoration()` | `false` | |
401-
| `validator` | `FormFieldValidator` | `null` | `false` | A `FormFieldValidator` that will check the validity of value in the `FormField` |
402-
| `onChanged` | `ValueChanged<T>` | `null` | `false` | This event function will fire immediately the the field value changes |
403-
| `valueTransformer` | `ValueTransformer<T>` | `null` | `false` | Function that transforms field value before saving to form value. e.g. transform TextField value for numeric field from `String` to `num` |
395+
| `name` | `String` | `null` | `Yes` | This will form the key in the form value Map |
396+
| `initialValue` | `dynamic` | `null` | `No` | The initial value of the input field |
397+
| `readOnly` | `bool` | `false` | `No` | Determines whether the field widget will accept user input. This value will be ignored if the `readOnly` attribute of `FormBuilder` widget is set to `true` |
398+
| `decoration` | `InputDecoration` | `InputDecoration()` | `No` | Defines the border, labels, icons, and styles used to decorate the field. |
399+
| `validator` | `FormFieldValidator` | `null` | `No` | A `FormFieldValidator` that will check the validity of value in the `FormField` |
400+
| `onChanged` | `ValueChanged<T>` | `null` | `No` | This event function will fire immediately the the field value changes |
401+
| `valueTransformer` | `ValueTransformer<T>` | `null` | `No` | Function that transforms field value before saving to form value. e.g. transform TextField value for numeric field from `String` to `num` |
402+
| `skipReadOnly` | `bool` | `false` | `No` | Determines whether the field and its value will be included in the final form value `Map` |
404403
The rest of the attributes will be determined by the type of Widget being used.
405404

406405
### Building your own custom field
@@ -571,27 +570,6 @@ FormBuilderRadioGroup(
571570
),
572571
```
573572

574-
## CREDITS
575-
### Contributors
576-
<a href="https://github.com/danvick/flutter_form_builder/graphs/contributors">
577-
<img src="https://contributors-img.firebaseapp.com/image?repo=danvick/flutter_form_builder" />
578-
</a>
579-
580-
Made with [contributors-img](https://contributors-img.firebaseapp.com).
581-
582-
### Dependencies
583-
This package is dependent on the following packages and plugins:
584-
* [country_code_picker](https://github.com/anicdh/country_code_picker) by [](https://github.com/)
585-
* [date_range_picker](https://github.com/anicdh/date_range_picker) by [anicdh](https://github.com/anicdh)
586-
* [datetime_picker_formfield](https://pub.dev/packages/datetime_picker_formfield) by [Jacob Phillips](https://github.com/jifalops)
587-
* [flutter_colorpicker](https://pub.dev/packages/flutter_colorpicker) by [mchome](https://github.com/mchome)
588-
* [flutter_typeahead](https://pub.dev/packages/flutter_typeahead) by [AbdulRahmanAlHamali](https://github.com/AbdulRahmanAlHamali)
589-
* [phone_number](https://pub.dev/packages/phone_number) by [](https://github.com/)
590-
* [rating_bar](https://pub.dev/packages/rating_bar) by [Joshua Matta](https://github.com/joshmatta)
591-
* [signature](https://pub.dev/packages/signature) by [4Q s.r.o.](https://github.com/4Q-s-r-o)
592-
* [validators](https://pub.dev/packages/validators) by [dart-league](https://github.com/dart-league)
593-
* [flutter_chips_input](https://pub.dev/packages/flutter_chips_input) & [flutter_touch_spin](https://pub.dev/packages/flutter_touch_spin) by [Yours truly :-)](https://github.com/danvick)
594-
595573
## SUPPORT
596574
### Issues and PRs
597575
Any kind of support in the form of reporting bugs, answering questions or PRs is always appreciated.
@@ -628,3 +606,24 @@ If this package was helpful to you in delivering your project or you just wanna
628606
package, a cup of coffee would be highly appreciated ;-)
629607

630608
[![Buy me a coffee](https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png)](https://buymeacoff.ee/wb5M9y2Sz)
609+
610+
## CREDITS
611+
### Contributors
612+
<a href="https://github.com/danvick/flutter_form_builder/graphs/contributors">
613+
<img src="https://contributors-img.firebaseapp.com/image?repo=danvick/flutter_form_builder" />
614+
</a>
615+
616+
Made with [contributors-img](https://contributors-img.firebaseapp.com).
617+
618+
### Dependencies
619+
This package is dependent on the following packages and plugins:
620+
* [country_code_picker](https://github.com/anicdh/country_code_picker) by [](https://github.com/)
621+
* [date_range_picker](https://github.com/anicdh/date_range_picker) by [anicdh](https://github.com/anicdh)
622+
* [datetime_picker_formfield](https://pub.dev/packages/datetime_picker_formfield) by [Jacob Phillips](https://github.com/jifalops)
623+
* [flutter_colorpicker](https://pub.dev/packages/flutter_colorpicker) by [mchome](https://github.com/mchome)
624+
* [flutter_typeahead](https://pub.dev/packages/flutter_typeahead) by [AbdulRahmanAlHamali](https://github.com/AbdulRahmanAlHamali)
625+
* [phone_number](https://pub.dev/packages/phone_number) by [](https://github.com/)
626+
* [rating_bar](https://pub.dev/packages/rating_bar) by [Joshua Matta](https://github.com/joshmatta)
627+
* [signature](https://pub.dev/packages/signature) by [4Q s.r.o.](https://github.com/4Q-s-r-o)
628+
* [validators](https://pub.dev/packages/validators) by [dart-league](https://github.com/dart-league)
629+
* [flutter_chips_input](https://pub.dev/packages/flutter_chips_input) & [flutter_touch_spin](https://pub.dev/packages/flutter_touch_spin) by [Yours truly :-)](https://github.com/danvick)

example/lib/sources/complete_form.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ class CompleteFormState extends State<CompleteForm> {
9898
),
9999
FormBuilderColorPickerField(
100100
name: 'color_picker',
101-
// initialValue: Colors.yellow,
101+
initialValue: Colors.yellow,
102+
// readOnly: true,
102103
colorPickerType: ColorPickerType.MaterialPicker,
103104
decoration: const InputDecoration(labelText: 'Pick Color'),
104105
),

lib/src/form_builder.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ class FormBuilderState extends State<FormBuilder> {
107107
});
108108
}
109109

110+
void removeInternalFieldValue(String name) {
111+
setState(() {
112+
_value = {..._value..remove(name)};
113+
});
114+
}
115+
110116
void registerField(String name, FormBuilderFieldState field) {
111117
_fields[name] = field;
112118
}

lib/src/form_builder_field.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,12 @@ class FormBuilderFieldState<T> extends FormFieldState<T> with AfterInitMixin {
116116
@override
117117
void save() {
118118
super.save();
119-
if (_formBuilderState.widget.skipReadOnly) {
120-
if (!readOnly) {
121-
_formBuilderState?.setInternalFieldValue(
122-
widget.name, widget.valueTransformer?.call(value) ?? value);
123-
}
124-
} else {
119+
if (!_formBuilderState.widget.skipReadOnly ||
120+
(_formBuilderState.widget.skipReadOnly && !readOnly)) {
125121
_formBuilderState?.setInternalFieldValue(
126122
widget.name, widget.valueTransformer?.call(value) ?? value);
123+
} else {
124+
_formBuilderState?.removeInternalFieldValue(widget.name);
127125
}
128126
}
129127

0 commit comments

Comments
 (0)