Skip to main content

switch

Function switch 

Source
pub fn switch() -> SwitchBuilder
Expand description

§switch

Render a Material switch for boolean on/off input in settings or forms.

§Usage

Use when you want a standard on/off switch without custom thumb content.

§Parameters

  • modifier — optional modifier chain applied to the switch subtree.
  • focus_requester — optional handle targeting the switch for focus requests.
  • on_toggle — optional callback invoked when the switch toggles; omit for a noninteractive state display that retains its accessibility value.
  • enabled — optional enabled state; defaults to true.
  • presentation_state — optional visual interaction state; live input and disabled state take precedence.
  • checked — externally controlled checked state; omit to use the controller.
  • width — optional track width override.
  • height — optional track height override.
  • track_color — optional track color when unchecked.
  • track_checked_color — optional track color when checked.
  • track_outline_color — optional track outline color when unchecked.
  • track_outline_width — optional outline width.
  • thumb_color — optional thumb color when unchecked.
  • thumb_checked_color — optional thumb color when checked.
  • thumb_checked_icon_color — optional icon color when checked.
  • thumb_icon_color — optional icon color when unchecked.
  • accessibility_label — optional accessibility label.
  • accessibility_description — optional accessibility description.
  • controller — optional external controller.
  • child — optional content rendered at the thumb center; icons default to 16dp and inherit the current thumb icon color.

§Examples

use tessera_components::switch::{SwitchController, switch};
use tessera_ui::remember;
material_theme()
    .theme(|| MaterialTheme::default())
    .child(|| {
        let controller = remember(|| {
            let mut controller = SwitchController::new(false);
            controller.toggle();
            assert!(controller.is_checked());
            controller
        });
        switch().controller(controller).on_toggle(|_| {});
    });