User input and forms
User input has two layers:
- Gestures —
GestureDetector(tap, drag, long press) andInkWell(the same plus a Material ripple). If a ready-made button widget exists, use it;GestureDetectoris for the cases it does not cover. - Text input —
TextFieldon its own,TextFormFieldinside aFormwith 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 indispose() - `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
- Adding interactivityofficialdocs.flutter.dev
- Building a form with validationofficialdocs.flutter.dev
- Form classofficialapi.flutter.dev
- TextEditingControllerofficialapi.flutter.dev