Sub topic 1
Your First TextField
Prerequisites
This chapter assumes you know Dart nullable types and closures, can write a StatefulWidget that calls setState, and can arrange widgets in a Column. You need a running Flutter project with Material widgets available.
Your first TextField
A TextField is a widget that lets the user type text. Put it inside a StatefulWidget when your code needs to read or change the text later.
import 'package:flutter/material.dart';
class NameField extends StatefulWidget {
const NameField({super.key});
@override
State<NameField> createState() => _NameFieldState();
}
class _NameFieldState extends State<NameField> {
final _nameController = TextEditingController();
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
controller: _nameController,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
labelText: 'Name',
hintText: 'Ada Lovelace',
prefixIcon: Icon(Icons.person),
border: OutlineInputBorder(),
),
),
ElevatedButton(
onPressed: () => debugPrint('Hello ${_nameController.text}'),
child: const Text('Greet'),
),
],
);
}
}textInputAction sets the key on the keyboard, here a "next" arrow. It changes the keyboard only. It does not validate anything.
Reading and controlling text with TextEditingController
A TextEditingController holds the field's current text and cursor selection. Create it as a field of the State class, pass it to the controller parameter, and call dispose in the State class's dispose method. Without dispose, the controller's listeners stay in memory.
Common error: creating the controller inside build. Each rebuild then makes a new controller, and the typed text disappears.
To change the text in code, assign controller.text = 'value'. This moves the cursor to the end. Use controller.value when you need to set the text and the selection together.
Styling a field with InputDecoration
InputDecoration controls the visible parts of a field: labelText, hintText, prefixIcon and border. The label stays visible while the user types. The hint disappears once the field has text.
Configuring the keyboard and obscured text
keyboardType selects the on-screen keyboard, such as TextInputType.emailAddress or TextInputType.number. It does not block other characters. Input formatters do that job.
obscureText: true hides the characters, as in a password field. Pair it with TextInputType.visiblePassword and autofillHints so password managers can fill the field.
TextField(
obscureText: true,
keyboardType: TextInputType.visiblePassword,
autofillHints: const [AutofillHints.password],
decoration: const InputDecoration(labelText: 'Password'),
)Common error: setting obscureText: true together with maxLines greater than 1. Flutter rejects this with an assertion, because a hidden field must be single-line.
Exercise
Why does the sample create the controller once as a field instead of inside build?
Show answer
build runs on every rebuild. A new controller each time would discard the text the user typed.