TextFormField lets users type text in your app. It helps collect information like names or emails.
TextFormField in Flutter
TextFormField(
controller: TextEditingController(),
decoration: InputDecoration(
labelText: 'Label',
hintText: 'Hint',
),
keyboardType: TextInputType.text,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onSaved: (value) {
// Save the value
},
)controller manages the text inside the field.
decoration adds labels and hints to guide users.
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
)TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(labelText: 'Email'),
)TextFormField( obscureText: true, decoration: InputDecoration(labelText: 'Password'), )
This app shows a form with a text field for the user to enter their name. When the user taps Submit, it checks if the field is empty. If not, it shows a greeting message with the entered name.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('TextFormField Example')), body: Padding( padding: EdgeInsets.all(16), child: MyCustomForm(), ), ), ); } } class MyCustomForm extends StatefulWidget { @override _MyCustomFormState createState() => _MyCustomFormState(); } class _MyCustomFormState extends State<MyCustomForm> { final _formKey = GlobalKey<FormState>(); final _controller = TextEditingController(); @override Widget build(BuildContext context) { return Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ TextFormField( controller: _controller, decoration: InputDecoration( labelText: 'Enter your name', hintText: 'John Doe', ), validator: (value) { if (value == null || value.isEmpty) { return 'Please enter your name'; } return null; }, ), SizedBox(height: 20), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Hello, ${_controller.text}!')), ); } }, child: Text('Submit'), ), ], ), ); } @override void dispose() { _controller.dispose(); super.dispose(); } }
Always use a Form widget to group TextFormField widgets for validation.
Use TextEditingController to read or change the text programmatically.
Remember to dispose the controller in dispose() to avoid memory leaks.
TextFormField lets users type and edit text in your app.
Use decoration to add labels and hints for better user guidance.
Use validator inside a Form to check user input before saving.
Practice
TextFormField widget in Flutter?Solution
Step 1: Understand the widget's role
TextFormFieldis designed to accept text input from users.Step 2: Compare with other widget functions
Other widgets like Image or ElevatedButton serve different purposes, not text input.Final Answer:
To let users type and edit text input -> Option AQuick Check:
Text input = TextFormField [OK]
- Confusing TextFormField with buttons or images
- Thinking it handles navigation
- Assuming it only displays static text
TextFormField?Solution
Step 1: Recall how decoration works
Labels and hints are added inside thedecorationproperty usingInputDecoration.Step 2: Check each option's syntax
Only TextFormField(decoration: InputDecoration(labelText: 'Name')) correctly usesdecoration: InputDecoration(labelText: 'Name').Final Answer:
TextFormField(decoration: InputDecoration(labelText: 'Name')) -> Option CQuick Check:
Label goes inside decoration [OK]
- Putting labelText directly on TextFormField
- Using hint instead of labelText for labels
- Confusing text property with label
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,
child: TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter text';
}
return null;
},
),
)Solution
Step 1: Understand validator behavior
The validator checks if the input is empty and returns an error message if true.Step 2: Analyze empty input case
If the user submits empty text, the validator returns 'Please enter text', showing an error below the field.Final Answer:
The validator returns 'Please enter text' and shows an error -> Option DQuick Check:
Empty input triggers validator error [OK]
- Assuming form submits without validation
- Thinking missing onSaved causes crash
- Believing TextFormField clears on submit
TextFormField:TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Required';
}
return null;
},
)Solution
Step 1: Check validator parameter type
The validator receives a nullable String? value, so it can be null.Step 2: Identify missing null check
The code callsvalue.isEmptywithout checking if value is null, causing a runtime error.Final Answer:
Missing null check for value before calling isEmpty -> Option BQuick Check:
Always check for null before isEmpty [OK]
- Ignoring nullable value in validator
- Expecting validator to return boolean
- Thinking onChanged is mandatory
TextFormField that only accepts numbers and shows a hint 'Enter age'. Which code snippet correctly implements this?Solution
Step 1: Identify required properties for numeric input with hint
The correct implementation requireskeyboardType: TextInputType.number,decoration: InputDecoration(hintText: 'Enter age'), and a validator checking for null/empty and valid integer parse.Step 2: Verify validator handles all cases
Validator must return error for null, empty, or non-numeric input usingif (value == null || value.isEmpty)andint.tryParse(value) == null.Final Answer:
TextFormField( keyboardType: TextInputType.number, decoration: InputDecoration(hintText: 'Enter age'), validator: (value) { if (value == null || value.isEmpty) return 'Age required'; if (int.tryParse(value) == null) return 'Enter a valid number'; return null; }, ) -> Option AQuick Check:
number keyboard + exact hint + null/empty/parse validator [OK]
- Using text keyboard for numeric input
- Not validating numeric format
- Confusing hintText and labelText
Ready to prove it?
Pass the test and this pattern moves unknown
