Forms and TextField Input

Google Flutter 13 min min read Updated: Nov 16, 2026 Intermediate
Forms and TextField Input
Intermediate Topic 7 of 9

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

OptionUse
obscureText: trueHide text for passwords
keyboardTypeemail, number, phone, multiline
textInputActionLabel of the keyboard action: next, done, search
maxLengthLimit characters
maxLinesMore lines for comments
onChangedCalled on each key press
onSubmittedCalled 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

  • DropdownButtonFormField for a list of choices
  • showDatePicker and showTimePicker for dates and times
  • Checkbox, Radio and Switch for 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators