Learn/Forms & Input/TextField Widget

TextField Widget

Capture user text input

5 min~5 min read+30 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. TextField Properties
  2. 2. Input UX Best Practices
  3. 3. This Lesson In Simple Words
  4. 4. Your level focus
  5. 5. One practical habit
  6. 6. Deep Understanding Guide
  7. 7. Why this lesson matters
  8. 8. Study flow for better retention
  9. 9. Quick Glossary

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

TextField Widget

TextField is the primary input widget for capturing text from users. It displays an editable text area where users can type, and it supports features like placeholder text, labels, icons, and different keyboard types. You control a TextField through a TextEditingController, which lets you read the current value, set text programmatically, and listen for changes.

dart
1// Basic TextField
2TextField(
3 decoration: InputDecoration(
4 labelText: 'Email',
5 hintText: 'Enter your email',
6 prefixIcon: Icon(Icons.email),
7 border: OutlineInputBorder(),
8 ),
9)
10
11// With controller (recommended for reading value)
12class _MyFormState extends State<MyForm> {
13 final _emailController = TextEditingController();
14
15 @override
16 void dispose() {
17 _emailController.dispose(); // Always dispose!
18 super.dispose();
19 }
20
21 @override
22 Widget build(BuildContext context) {
23 return TextField(
24 controller: _emailController,
25 onSubmitted: (value) {
26 print('Email: ${_emailController.text}');
27 },
28 );
29 }
30}

TextField Properties

TextField comes with a wide range of properties for customizing how it looks and behaves. You can set the keyboard type, limit character count, obscure text for passwords, and style the field with labels, hints, and error messages through InputDecoration.

dart
1TextField(
2 controller: _controller,
3
4 // Input type
5 keyboardType: TextInputType.emailAddress,
6
7 // Obscure for passwords
8 obscureText: true,
9
10 // Character limit
11 maxLength: 50,
12 maxLines: 1,
13
14 // Callbacks
15 onChanged: (value) => print('Changed: $value'),
16 onSubmitted: (value) => print('Submitted: $value'),
17
18 // Styling
19 decoration: InputDecoration(
20 labelText: 'Password',
21 helperText: 'Min 8 characters',
22 errorText: _hasError ? 'Invalid password' : null,
23 suffixIcon: IconButton(
24 icon: Icon(Icons.visibility),
25 onPressed: _toggleVisibility,
26 ),
27 ),
28)

Always Dispose Controllers

💡 Always Dispose Controllers

TextEditingController must be disposed in the dispose() method to prevent memory leaks.

Input UX Best Practices

Great text input fields reduce friction:

  • choose correct keyboard type per field purpose
  • provide clear labels and helpful hints
  • show validation feedback near the field
  • keep error language actionable and specific

Fast, clear input experiences improve completion rates for signup, checkout, and profile flows.

TextField Quality Checklist

  • Keyboard type matches input intent (email, phone, number).
  • Obscure and visibility toggle are handled for passwords.
  • Error and helper text are concise and user-friendly.
  • Controller lifecycle is fully managed in dispose.

This Lesson In Simple Words

Capture user text input. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.

Your level focus

Connect each concept to real app features you can build.

One practical habit

Sketch the widget tree first, then code from top to bottom.

Deep Understanding Guide

Why this lesson matters

Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.

Study flow for better retention

  1. Map the lesson to one screen, one user action, and one expected output.
  2. When a UI bug appears, inspect constraints and parent-child relations before changing random values.
  3. If you can choose between two approaches and explain why, you are improving.

Common Mistakes To Avoid

  • Nesting too many widgets without extracting reusable pieces.
  • Ignoring constraints and guessing with random SizedBox heights.
  • Using one giant build method for unrelated UI sections.
  • Putting heavy calculations directly in build methods.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one card layout with Row/Column and rebuild it using Stack for comparison.
  • Split a large widget into 3 smaller widgets with clear responsibilities.
  • Use const constructors where possible and observe rebuild behavior.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "How do you read the current TextField value?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.
  • validation: Checks that confirm input is correct before processing.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "TextField Widget" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Nesting too many widgets without extracting reusable pieces.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

How do you read the current TextField value?

TextEditingController.text contains the current input value.

What property hides input for passwords?

obscureText replaces characters with dots for password fields.

Why must you dispose TextEditingController?

Controllers allocate resources. Disposing them in dispose() prevents memory leaks.

What does InputDecoration.prefixIcon do?

prefixIcon displays an icon on the left side inside the TextField, like a search or email icon.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue