Button widget creates clickable buttons that produce messages when pressed. Buttons are one of the most common interactive elements in GUI applications.
Basic Usage
Button States
A button without anon_press handler is automatically disabled:
Builder Methods
on_press(message: Message)
Sets the message produced when clicked. Required for an enabled button.
on_press_with(closure: impl Fn() -> Message)
Uses a closure to produce the message. Useful for reducing overhead when message creation is expensive.
on_press_maybe(message: Option<Message>)
Conditionally enables the button.
width(length: impl Into<Length>)
Sets the button width.
height(length: impl Into<Length>)
Sets the button height.
padding(padding: impl Into<Padding>)
Sets the padding around the button content.
Default: Padding { top: 5.0, bottom: 5.0, right: 10.0, left: 10.0 }
clip(clip: bool)
Sets whether contents should be clipped on overflow.
style(style_fn: impl Fn(&Theme, Status) -> Style)
Applies a custom style function.
Built-in Styles
Iced provides several pre-built button styles:button::primary
A primary button for main actions:
button::secondary
A secondary button for complementary actions:
button::success
A success button for positive outcomes:
button::warning
A warning button for risky actions:
button::danger
A danger button for destructive actions:
button::text
A text-only button, useful for links:
button::background
A button with background shading:
button::subtle
A subtle button with weak background:
Button Status
Buttons have four possible states:Status::Active- Can be pressedStatus::Hovered- Can be pressed and is being hoveredStatus::Pressed- Is being pressedStatus::Disabled- Cannot be pressed
Style Structure
Complete Example
Related Widgets
- Text - For button labels
- Container - For custom button layouts
- Row/Column - For arranging multiple buttons
