TextInput widget creates a single-line text field that allows users to enter and edit text. It’s one of the most common input widgets in GUI applications.
Basic Usage
Builder Methods
new(placeholder: &str, value: &str)
Creates a new text input with a placeholder and current value.
on_input(callback: impl Fn(String) -> Message)
Sets the callback for when text changes. Without this, the input is disabled.
on_input_maybe(callback: Option<impl Fn(String) -> Message>)
Conditionally enables the input.
on_submit(message: Message)
Sets the message produced when Enter is pressed.
on_paste(callback: impl Fn(String) -> Message)
Sets the callback for paste events.
secure(is_secure: bool)
Makes this a password input (shows dots instead of characters).
id(id: impl Into<widget::Id>)
Sets the widget ID for programmatic focus control.
width(width: impl Into<Length>)
Sets the input width.
padding(padding: impl Into<Padding>)
Sets the padding inside the input.
Default: Padding::new(5.0)
size(size: impl Into<Pixels>)
Sets the text size.
line_height(line_height: impl Into<LineHeight>)
Sets the line height.
font(font: impl Into<Font>)
Sets the font.
alignment(alignment: alignment::Horizontal)
Sets text alignment.
icon(icon: Icon<Font>)
Adds an icon to the input.
style(style_fn: impl Fn(&Theme, Status) -> Style)
Applies custom styling.
Input States
Text inputs have several states:- Active - Normal state, ready for input
- Focused - Currently selected for input
- Hovered - Mouse is over the input
- Disabled - No
on_inputcallback set
Controlling Focus
You can programmatically focus text inputs using operations:Form Example
Search Input Example
Validation Example
Tips and Best Practices
- Always provide a placeholder - Helps users understand what to enter
- Use
on_submitfor forms - Allows Enter key submission - Validate input - Check values and provide feedback
- Use
.secure(true)for passwords - Protects sensitive data - Set appropriate width - Use
Length::Fillin most forms - Provide clear labels - Use text widgets to label inputs
Related Widgets
- TextEditor - Multi-line text editing
- Button - For form submission
- Text - For labels
- Container - For input grouping
