Sparround

User input and forms

User input has two layers:

  • GesturesGestureDetector (tap, drag, long press) and InkWell (the same plus a Material ripple). If a ready-made button widget exists, use it; GestureDetector is for the cases it does not cover.
  • Text inputTextField on its own, TextFormField inside a Form with validation.

The Form + GlobalKey<FormState> combination is the standard pattern: _formKey.currentState!.validate() runs every field's validator and returns the overall result as a bool.

There are two ways to read a text field's value, and the choice matters:

  • `TextEditingController` — when you need to read or set the value at arbitrary times. Created in State, closed in dispose()
  • `onChanged` / `onSaved` — when collecting the value is enough; lighter than a controller

autovalidateMode controls when validation runs. AutovalidateMode.onUserInteraction is usually the best UX: it does not shout at a user who has typed nothing yet, but gives immediate feedback once they start.

Interview tip. In a forms question the UX details set you apart: keeping content visible when the keyboard opens (Scaffold helps, but you may need a SingleChildScrollView), "Next/Done" keys via textInputAction, the right keyboard via keyboardType, and disabling the submit button while a request is in flight so it cannot be double-submitted. These are the things users notice immediately on a real device.

📚 Sources and documentation