pub struct Switch {
pub on: bool,
/* private fields */
}Expand description
A toggle switch — the reference “premium widget” (Q0 quality-bar exemplar).
Ships beautiful with zero config and stays fully overridable. It layers every state a real toggle needs on top of one animated value:
- Motion — the thumb slides (theme-eased) between off/on; because the
thumb radius is derived from that same 0→1 position, it also grows as it
settles on, Material-3 style, for free (one node scalar, see
animate_to). - States — idle · hover · pressed (thumb stretches) · focus-visible (ring + state-layer halo) · disabled (dimmed, inert) · on/off.
- Elevation — the thumb casts a soft drop shadow so it reads as a physical knob above the track.
- Theming — track/thumb/halo all come from theme tokens (
primary,surface_variant,on_primary,outline,shadow); light+dark adapt automatically. Any of them can be overridden per-instance. - A11y —
Role::Switchwith an on/off value, a Tab-focusable node, and an optional label for screen readers. - Interactive-by-identity — always owns its hit region, wired or not, so a tap can never fall through to a pannable surface behind it.
Fields§
§on: boolThe current value.
Implementations§
Source§impl Switch
impl Switch
pub fn new(on: bool) -> Self
Sourcepub fn on_change(self, f: impl Fn(bool) + Send + Sync + 'static) -> Self
pub fn on_change(self, f: impl Fn(bool) + Send + Sync + 'static) -> Self
Called with the NEW value when the switch is toggled (D094).
Sourcepub fn disabled(self) -> Self
pub fn disabled(self) -> Self
Non-interactive, dimmed, and inert (still absorbs the tap so nothing behind it reacts).
pub fn disabled_if(self, c: bool) -> Self
Sourcepub fn label(self, l: impl Into<String>) -> Self
pub fn label(self, l: impl Into<String>) -> Self
Accessibility label announced by screen readers alongside the on/off value.
Sourcepub fn size(self, width: f32, height: f32) -> Self
pub fn size(self, width: f32, height: f32) -> Self
Override the track size (default 44×24). Proportions stay tasteful at any reasonable size.
Sourcepub fn off_color(self, c: Color) -> Self
pub fn off_color(self, c: Color) -> Self
Override the off-track color (default: theme surface_variant).
Sourcepub fn thumb_color(self, c: Color) -> Self
pub fn thumb_color(self, c: Color) -> Self
Override the thumb color (default: theme on_primary on / outline off).
Trait Implementations§
Source§impl Widget for Switch
impl Widget for Switch
Source§fn layout(&self, ctx: &LayoutCtx<'_>) -> Size
fn layout(&self, ctx: &LayoutCtx<'_>) -> Size
Measure under
ctx.constraints and return a size within them. Read moreSource§fn children(&self) -> Children<'_>
fn children(&self) -> Children<'_>
Declare this widget’s children. Drives every default below.
Source§fn flex_factor(&self) -> f32
fn flex_factor(&self) -> f32
Flex weight inside Row/Column. Wrappers are transparent by default.
Auto Trait Implementations§
impl !RefUnwindSafe for Switch
impl !UnwindSafe for Switch
impl Freeze for Switch
impl Send for Switch
impl Sync for Switch
impl Unpin for Switch
impl UnsafeUnpin for Switch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<W> FocusApi for W
impl<W> FocusApi for W
Source§fn focus_node(self, node: FocusNode) -> WithFocus<Self>
fn focus_node(self, node: FocusNode) -> WithFocus<Self>
Attach a focus node. This enables focus-ring rendering and explicit
neighbor wiring.