If you're building Flutter apps, you'll spend a lot of time working with user input.
Whether it's a login form, signup page, search bar, or profile screen, you'll almost always use a TextField.
Creating Your First TextField
The simplest TextField looks like this:
TextField()
Most apps add a placeholder to help users understand what information is expected.
TextField(
decoration: InputDecoration(
hintText: 'Enter your name',
),
)
Reading User Input
You can listen for changes using the onChanged callback.
TextField(
onChanged: (value) {
print(value);
},
)
Flutter will call this function every time the user types.
Getting Text Later
For forms, you'll usually want to access the value when the user taps a button.
That's where TextEditingController comes in.
final TextEditingController nameController =
TextEditingController();
TextField(
controller: nameController,
)
Read the value like this:
print(nameController.text);
Setting Default Values
You can pre-fill a TextField using a controller.
final TextEditingController nameController =
TextEditingController(
text: 'Flutter Sensei',
);
This is useful for profile screens, settings pages, and editing existing data.
Choosing the Right Keyboard
Flutter lets you customize the keyboard based on the type of input.
TextField(
keyboardType: TextInputType.emailAddress,
)
Other common options include:
TextInputType.phoneTextInputType.numberTextInputType.urlTextInputType.multiline
Common Beginner Mistakes
A few mistakes I see all the time:
- Creating
TextEditingControllerinsidebuild() - Forgetting to dispose controllers
- Confusing
hintTextwith actual values - Using the wrong keyboard type
- Using
TextFieldwhen validation is required
Avoiding these early will save you a lot of debugging later.
Final Thoughts
TextField is one of the most important widgets in Flutter. Once you understand how to read input, use controllers, set values, and handle keyboard types, building forms becomes much easier.
Want the complete guide?
The full tutorial covers:
- TextField vs TextFormField
- TextEditingController in depth
- Labels and helper text
- Styling basics
- Default values
- Updating TextFields
- Common mistakes and best practices
Read the complete guide on Flutter Sensei:
Top comments (0)