Forms and TextField Input
TextField
dart
const TextField(
decoration: InputDecoration(
labelText: 'Email',
hintText: 'you@example.com',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
)
Reading the Text: TextEditingController
dart
class _SearchState extends State<Search> {
final _controller = TextEditingController();
@override
void dispose() {
_controller.dispose(); // always clean up
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(controller: _controller),
ElevatedButton(
onPressed: () => debugPrint('You typed: ${_controller.text}'),
child: const Text('Search'),
),
],
);
}
}
Useful TextField Options
| Option | Use |
|---|---|
obscureText: true | Hide text for passwords |
keyboardType | email, number, phone, multiline |
textInputAction | Label of the keyboard action: next, done, search |
maxLength | Limit characters |
maxLines | More lines for comments |
onChanged | Called on each key press |
onSubmitted | Called when the user presses done |
Form and Validation
A Form groups several fields. TextFormField adds a validator that returns an error message, or null when the value is fine.
dart
class LoginForm extends StatefulWidget {
const LoginForm({super.key});
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _formKey = GlobalKey<FormState>();
final _email = TextEditingController();
final _password = TextEditingController();
bool _hide = true;
@override
void dispose() {
_email.dispose();
_password.dispose();
super.dispose();
}
void _submit() {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Logging in...')),
);
}
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _email,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(labelText: 'Email', border: OutlineInputBorder()),
validator: (value) {
if (value == null || value.isEmpty) return 'Email is required';
if (!value.contains('@')) return 'Enter a valid email';
return null;
},
),
const SizedBox(height: 16),
TextFormField(
controller: _password,
obscureText: _hide,
decoration: InputDecoration(
labelText: 'Password',
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(_hide ? Icons.visibility : Icons.visibility_off),
onPressed: () => setState(() => _hide = !_hide),
),
),
validator: (value) =>
(value == null || value.length < 6) ? 'At least 6 characters' : null,
),
const SizedBox(height: 24),
FilledButton(onPressed: _submit, child: const Text('Login')),
],
),
);
}
}
How validation works:
validate() runs all validators. If any returns a text, that text is shown in red under the field and validate() returns false.
Move Focus to the Next Field
dart
TextFormField(
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) => FocusScope.of(context).nextFocus(),
)
// Hide the keyboard
FocusScope.of(context).unfocus();
Other Input Widgets
DropdownButtonFormFieldfor a list of choicesshowDatePickerandshowTimePickerfor dates and timesCheckbox,RadioandSwitchfor options
dart
final picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2100),
);
Tip: The keyboard can cover your form. Wrap the form in a
SingleChildScrollView so the user can scroll while typing.
Conclusion
Forms with validators give a smooth experience and protect your data. Remember to dispose the controllers.
Quick Summary: Use TextFormField with validators inside a Form with a GlobalKey, read text with controllers and dispose them.

