flutter_form_builder
flutter_form_builder copied to clipboard
[New feature] Property to know if the form has changes
How can I know the form is dirty. I'm aware of the onChange listener but it currently brakes the signature field (#457). I'm looking for a bool for example, isDirty in the form builder state.
Did we find a solution or any workaround for this issue? As it's basic functionality, I think it should be taken on priority.
Hi, any update for this?
I only requested this feature because onChange was broken at the time. The question "Does the form have any changes" feels like a little bit subjective, since not everybody wants to detect ALL the changes to the form. So I suggest you to implement your own isDirty flag using the onChange callback.
isDirty in FormState is a required feature. I have a use case where I have a form containing 10 fields if the user changed any field I have to do a post request to the server to update on that screen pop I could do this on every screen navigation and pop. Even if no fields are updated. So this feature can help me to reduce code .
isDirtyin FormState is a required feature. I have a use case where I have a form containing 10 fields if the user changed any field I have to do a post request to the server to update on that screen pop I could do this on every screen navigation and pop. Even if no fields are updated. So this feature can help me to reduce code .
Like I mentioned in the previous comment, you can use onChange callaback. you can set a bool flag to true when onChange is called, and reset it to false when the post request is done.
@nipunasudha currently I am doing that only 🙂.
A solution that I think is compare initialValue with instantValue. If is different, has changes
I create this solution to get new changes
final formKey = GlobalKey<FormBuilderState>();
/// Get only the changed values.
/// Compare the initial value and actual value of form and get the difference
///
/// By default, use saved values [formValues] as actual value
Map<String, dynamic> getChangedValues({bool useInstantValues = false}) {
final _formValues = useInstantValues
? formKey.currentState?.instantValue ?? {}
: formKey.currentState?.value ?? {};
final _initialValues = formKey.currentState?.initialValue ?? {};
final _changedValues = <String, dynamic>{};
for (var entry in _formValues.entries) {
if (_formValues[entry.key] != _initialValues[entry.key]) {
_changedValues[entry.key] = entry.value;
}
}
return _changedValues;
}