flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

[New feature] Property to know if the form has changes

Open nipunasudha opened this issue 4 years ago • 8 comments

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.

nipunasudha avatar Dec 04 '20 07:12 nipunasudha

Did we find a solution or any workaround for this issue? As it's basic functionality, I think it should be taken on priority.

THALLIVA avatar Apr 30 '21 06:04 THALLIVA

Hi, any update for this?

quanglefed avatar May 21 '21 11:05 quanglefed

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.

nipunasudha avatar May 21 '21 11:05 nipunasudha

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 .

Shiba-Kar avatar Oct 25 '21 11:10 Shiba-Kar

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 .

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 avatar Oct 25 '21 11:10 nipunasudha

@nipunasudha currently I am doing that only 🙂.

Shiba-Kar avatar Oct 25 '21 11:10 Shiba-Kar

A solution that I think is compare initialValue with instantValue. If is different, has changes

deandreamatias avatar Jun 13 '22 11:06 deandreamatias

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;
  }

deandreamatias avatar Jul 21 '22 07:07 deandreamatias