TextField
Accept text input from the user.
Overview
TextField
is a widget that allows users to enter and edit text. It provides features like placeholders, password obscuring, multiline input, and more.
Basic Usage
@Observable class State {
var text = ""
}
TextField(
placeholder: "Enter text",
onChanged: { value in
state.text = value
}
)
TextField Examples
With Controller
For programmatic control:
let controller = TextEditingController()
TextField(
controller: controller,
placeholder: "Controlled input"
)
// Later: read or set value
print(controller.text)
controller.text = "New value"
Key Parameters
Parameter | Type | Description |
---|---|---|
placeholder | String? | Hint text when empty |
controller | TextEditingController? | Control text programmatically |
onChanged | (String) -> Void | Called when text changes |
onSubmitted | (String) -> Void | Called when user submits (Enter key) |
obscureText | Bool | Hide text (for passwords) |
maxLines | Int? | Maximum lines (1 = single line, nil = unlimited) |
maxLength | Int? | Maximum character count |
enabled | Bool | Whether field is editable |
readOnly | Bool | Show keyboard but don’t allow edits |
autofocus | Bool | Focus on creation |
padding | EdgeInsetsGeometry | Internal padding |
prefix | Widget? | Widget before text (icon, etc.) |
style | TextStyle? | Text style |
placeholderStyle | TextStyle? | Placeholder text style |
See Also
- Button - Submit forms
- Text - Display text
- State Management - Managing form state