flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.18k stars 26.64k forks source link

Add getter for Form's _hasInteractedByUser #147462

Closed mwoelk closed 2 weeks ago

mwoelk commented 2 weeks ago

Use case

Sometimes we want to check if a user has interacted with a form so we can offer him the option to save when e.g. popping the route. Currently this can be quite cumbersome to do by listening to onChange events of every single field and updating some state.

Form has a private flag for this, _hasInteractedByUser, which could provide the required information.

Proposal

There already was a similar issue to expose this method for FormField. The proposal would be to do the same for Form: expose a boolean getter for the value of _hasInteractedByUser.

This way, it could be easily accessed via a key in e.g. a pop handler.

final key = GlobalKey<FormState>;

Form(
  key: key,
  // ... 
),

final changed = key.currentState!.hasInteractedByUser;
mwoelk commented 2 weeks ago

Just realized that form itself has an onChanged that is called whenever a field changes. It could easily be listened to and just set a flag accordingly when any field has changed.

Closing because not an issue anymore.

github-actions[bot] commented 4 days ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.