Sub topic 3
Styling a field with InputDecoration
This chapter assumes basic Dart syntax, nullable types, closures, Flutter widgets, Column, and Scaffold. You should also know how a StatefulWidget uses setState.
InputDecoration controls the visual structure and supporting text around a TextField or TextFormField. It can provide a label, hint, icons, borders, helper text, and error text without changing how the field stores its value.
Labels, hints, and supporting text
A labelText identifies the field. When the field is empty and unfocused, the label appears inside the field. When the field has focus or text, it moves above the input.
A hintText shows an example of the expected value. It disappears when the user starts typing. Use a hint for format guidance, not as the only label.
helperText gives additional guidance below the field. errorText communicates a problem and usually comes from validation.
| Property | Purpose | Example |
|---|---|---|
| `labelText` | Persistent field name | `Email` |
| `hintText` | Example or format | `[email protected]` |
| `helperText` | Extra guidance | `We will not share your email` |
| `errorText` | Current error message | `Enter a valid email` |
Icons and borders
prefixIcon places an icon before the editable text. suffixIcon places one after it, often for actions such as clearing text or showing a password.
Borders describe the field in different states:
enabledBorderapplies when the field is enabled but not focused.focusedBorderapplies while the field has focus.errorBorderapplies whenerrorTextis present.focusedErrorBorderapplies when an invalid field also has focus.
If you set only one border, Flutter may use theme defaults for the other states. Define related states together when you need consistent styling.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: EmailPage()));
}
class EmailPage extends StatelessWidget {
const EmailPage({super.key});
@override
Widget build(BuildContext context) {
const normalBorder = OutlineInputBorder();
return Scaffold(
appBar: AppBar(title: const Text('Account')),
body: Padding(
padding: const EdgeInsets.all(16),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email address',
hintText: '[email protected]',
helperText: 'Use the address associated with your account',
prefixIcon: const Icon(Icons.email_outlined),
border: normalBorder,
enabledBorder: normalBorder,
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(width: 2),
),
errorBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Email is required';
}
if (!value.contains('@')) {
return 'Enter a valid email address';
}
return null;
},
),
),
);
}
}Theme and accessibility
Prefer theme colors and sufficient contrast instead of hard-coding every color. Keep labels visible even when a hint is present. Icons should support the label, not replace it. For icon-only actions, provide a tooltip and a semantic label.
Exercise
Which property should show an example such as “[email protected]”, and which property should identify the field?
Show answer
Use hintText for the example and labelText to identify the field.