Function button

Source
pub fn button(
    args: impl Into<ButtonArgs>,
    ripple_state: Arc<RippleState>,
    child: impl FnOnce(),
)
Expand description

Creates an interactive button component that can wrap any custom child content.

The button component provides a clickable surface with a ripple effect, customizable appearance, and event handling. It’s built on top of the surface component and handles user interactions like clicks and hover states.

§Parameters

  • args: An instance of ButtonArgs or ButtonArgsBuilder that defines the button’s properties, such as color, shape, padding, and the on_click callback.
  • ripple_state: An Arc<RippleState> that manages the visual state of the ripple effect. This should be created and managed by the parent component to persist the ripple animation state across recompositions.
  • child: A closure that defines the content to be displayed inside the button. This can be any other component, such as text, image, or a combination of them.

§Example

// 1. Create a ripple state to manage the effect.
let ripple_state = Arc::new(RippleState::new());

// 2. Define the button's properties using the builder pattern.
let args = ButtonArgsBuilder::default()
    .color(Color::new(0.2, 0.5, 0.8, 1.0)) // A nice blue
    .padding(Dp(12.0))
    .on_click(Arc::new(|| {
        println!("Button was clicked!");
    }))
    .build()
    .unwrap();

// 3. Call the button component, passing the args, state, and a child content closure.
button(args, ripple_state, || {
    text(
        TextArgsBuilder::default()
            .text("Click Me".to_string())
            .color(Color::WHITE)
            .build()
            .unwrap(),
    );
});