What is TextField in Flutter?
TextField is a Material Design widget that allows users to enter text. It's one of the most commonly used input widgets in Flutter. For forms with validation, you'll often use TextFormField, which is a wrapper around TextField that integrates with the Form widget. This guide covers both, with a focus on practical usage, customization, and best practices.
Basic TextField
The simplest form of a TextField uses an InputDecoration to provide labels, hint text, borders, and icons. The labelText appears inside the field when empty and moves above when focused.
Controlling Text with TextEditingController
To read or modify the text programmatically, use a TextEditingController. You can also listen to changes with addListener.
Input Decoration
InputDecoration is used to customize the appearance. Key properties:
labelText: A label that animates above the field when focused.
hintText: A hint that disappears when the user starts typing.
prefixIcon/suffixIcon: Icons at the start/end of the field.
prefixText/suffixText: Text before/after the input.
border/enabledBorder/focusedBorder: Define the field's border.
filledandfillColor: For filled background.
errorText: Displays an error message below the field.
counterText: Character counter text.
Keyboard Types and Actions
Control the keyboard type and action buttons with keyboardType and textInputAction.
Common TextInputAction values: next, done, go, search, send. The action button on the keyboard changes accordingly.
Obscure Text (Password Fields)
Focus Management
Use FocusNode to control focus programmatically and respond to focus changes. Always dispose of focus nodes to avoid memory leaks.
For moving focus between fields, use FocusScope.of(context).nextFocus() or set textInputAction to TextInputAction.next and handle onFieldSubmitted.
Input Formatters
inputFormatters allow you to restrict or modify the input as the user types. Common formatters: FilteringTextInputFormatter, LengthLimitingTextInputFormatter, and custom ones.
TextFormField for Forms
TextFormField is a variant that integrates with Form and provides validation and saving capabilities. Use it inside a Form widget with a GlobalKey<FormState>.
Handling User Input in Real Time
Best Practices
- Always dispose of
TextEditingControllerandFocusNodeobjects to prevent memory leaks.
- Always dispose of
- Use
TextFormFieldwithFormfor validation and saving.
- Use
- Provide clear
InputDecorationwith appropriatelabelTextandhintText.
- Provide clear
- For numeric inputs, set
keyboardType: TextInputType.numberand consider usinginputFormattersto enforce digits only.
- For numeric inputs, set
- Use
textInputActionto control keyboard behavior (next, done, etc.).
- Use
- Avoid heavy logic inside
onChanged; use throttling or debouncing if needed.
- Avoid heavy logic inside
Common Mistakes
- Not disposing controllers and focus nodes – leads to memory leaks.
- Using
TextFieldinside aFormwithoutTextFormField– validation won't work.
- Using
- Forgetting to wrap
TextFieldin aMaterialorMaterialApp– text field will look plain and won't get Material styling.
- Forgetting to wrap
- Using
onChangedfor validation without debouncing – may cause performance issues.
- Using
- Not handling the case when
controller.textis null – always check before using.
- Not handling the case when
- Setting
obscureTextto true but still storing password in plain text – remember to hash before storing.
- Setting
Key Takeaways
- Use
TextFieldfor simple text input;TextFormFieldfor validation and forms.
- Use
- -Control text with
TextEditingController. - Customize appearance with
InputDecoration.
- Customize appearance with
- Manage focus with
FocusNode.
- Manage focus with
- Restrict input with
inputFormatters.
- Restrict input with
- Use
keyboardTypeandtextInputActionfor keyboard behavior.
- Use
- Always dispose of resources in
dispose().
- Always dispose of resources in