Skip to main content

Switch

Struct Switch 

Source
pub struct Switch {
Show 25 fields pub key: Option<KeyRef>, pub value: bool, pub on_changed: Option<ValueChanged<bool>>, pub active_color: Option<Color>, pub active_thumb_color: Option<Color>, pub active_track_color: Option<Color>, pub inactive_thumb_color: Option<Color>, pub inactive_track_color: Option<Color>, pub thumb_color: Option<WidgetStatePropertyRef<Option<Color>>>, pub track_color: Option<WidgetStatePropertyRef<Option<Color>>>, pub track_outline_color: Option<WidgetStatePropertyRef<Option<Color>>>, pub track_outline_width: Option<WidgetStatePropertyRef<Option<f64>>>, pub thumb_icon: Option<WidgetStatePropertyRef<Option<Rc<Icon>>>>, pub material_tap_target_size: Option<MaterialTapTargetSize>, pub apply_cupertino_theme: Option<bool>, pub drag_start_behavior: DragStartBehavior, pub mouse_cursor: Option<MouseCursorRef>, pub focus_color: Option<Color>, pub hover_color: Option<Color>, pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>, pub splash_radius: Option<f64>, pub focus_node: Option<FocusNodeRef>, pub on_focus_change: Option<ValueChanged<bool>>, pub autofocus: bool, pub padding: Option<EdgeInsetsGeometry>, /* private fields */
}
Expand description

A Material Design switch.

Used to toggle the on/off state of a single setting.

The switch itself does not maintain any state. Instead, when the state of the switch changes, the widget calls the on_changed callback. Most widgets that use a switch will listen for the on_changed callback and rebuild the switch with a new value to update the visual appearance of the switch.

If the on_changed callback is null, then the switch will be disabled (it will not respond to input). A disabled switch’s thumb and track are rendered in shades of grey by default. The default appearance of a disabled switch can be overridden with inactive_thumb_color and inactive_track_color.

Requires one of its ancestors to be a Material widget.

Material Design 3 provides the option to add icons on the thumb of the Switch. If ThemeData::use_material3 is set to true, users can use thumb_icon to add optional Icons based on the different WidgetStates of the Switch.

See also:

Fields§

§key: Option<KeyRef>§value: bool

Whether this switch is on or off.

§on_changed: Option<ValueChanged<bool>>

Called when the user toggles the switch on or off.

The switch passes the new value to the callback but does not actually change state until the parent widget rebuilds the switch with the new value.

If null, the switch will be displayed as disabled.

The callback provided to on_changed should update the state of the parent StatefulWidget using the State::set_state method, so that the parent gets rebuilt.

§active_color: Option<Color>
👎Deprecated:

Use activeThumbColor or activeTrackColor instead. This feature was deprecated after v3.31.0-2.0.pre.

The color to use when this switch is on.

Defaults to ColorScheme::secondary.

If thumb_color returns a non-null color in the WidgetState::Selected state, it will be used instead of this color.

§active_thumb_color: Option<Color>

The color to use when this switch is on.

Defaults to ColorScheme::secondary.

If thumb_color returns a non-null color in the WidgetState::Selected state, it will be used instead of this color.

§active_track_color: Option<Color>

The color to use on the track when this switch is on.

Defaults to ColorScheme::secondary with the opacity set at 50%.

If track_color returns a non-null color in the WidgetState::Selected state, it will be used instead of this color.

§inactive_thumb_color: Option<Color>

The color to use on the thumb when this switch is off.

Defaults to the colors described in the Material design specification.

If thumb_color returns a non-null color in the default state, it will be used instead of this color.

§inactive_track_color: Option<Color>

The color to use on the track when this switch is off.

Defaults to the colors described in the Material design specification.

If track_color returns a non-null color in the default state, it will be used instead of this color.

§thumb_color: Option<WidgetStatePropertyRef<Option<Color>>>

The color of this Switch’s thumb.

Resolved in the following states:

If null, then the value of active_thumb_color is used in the selected state and inactive_thumb_color in the default state. If that is also null, then the value of SwitchThemeData::thumb_color is used. If that is also null, then the following colors are used:

StateLight themeDark theme
DefaultColors.grey.shade50Colors.grey.shade400
SelectedColorScheme::secondaryColorScheme::secondary
DisabledColors.grey.shade400Colors.grey.shade800
§track_color: Option<WidgetStatePropertyRef<Option<Color>>>

The color of this Switch’s track.

Resolved in the following states:

If null, then the value of active_track_color is used in the selected state and inactive_track_color in the default state. If that is also null, then the value of SwitchThemeData::track_color is used. If that is also null, then the following colors are used:

StateLight themeDark theme
DefaultColor(0x52000000)Colors.white30
Selectedactive_thumb_color with alpha 0x80active_thumb_color with alpha 0x80
DisabledColors.black12Colors.white10
§track_outline_color: Option<WidgetStatePropertyRef<Option<Color>>>

The outline color of this Switch’s track.

Resolved in the following states:

In Material 3, the outline color defaults to transparent in the selected state and ColorScheme::outline in the unselected state. In Material 2, the Switch track has no outline by default.

§track_outline_width: Option<WidgetStatePropertyRef<Option<f64>>>

The outline width of this Switch’s track.

Resolved in the following states:

Defaults to 2.0.

§thumb_icon: Option<WidgetStatePropertyRef<Option<Rc<Icon>>>>

The icon to use on the thumb of this switch

Resolved in the following states:

If null, then the value of SwitchThemeData::thumb_icon is used. If this is also null, then the Switch does not have any icons on the thumb.

§material_tap_target_size: Option<MaterialTapTargetSize>

Configures the minimum size of the tap target.

If null, then the value of SwitchThemeData::material_tap_target_size is used. If that is also null, then the value of ThemeData::material_tap_target_size is used.

See also:

§apply_cupertino_theme: Option<bool>

Whether to apply the ambient CupertinoThemeData.

If true, the track uses CupertinoThemeData::primary_color for the track when the switch is on.

Defaults to CupertinoThemeData::apply_theme_to_all.

§drag_start_behavior: DragStartBehavior

Determines the way that drag start behavior is handled.

If set to DragStartBehavior::Start, the drag behavior used to move the switch from on to off will begin at the position where the drag gesture won the arena. If set to DragStartBehavior::Down it will begin at the position where a down event was first detected.

In general, setting this to DragStartBehavior::Start will make drag animation smoother and setting it to DragStartBehavior::Down will make drag behavior feel slightly more reactive.

By default, the drag start behavior is DragStartBehavior::Start.

§mouse_cursor: Option<MouseCursorRef>

The cursor for a mouse pointer when it enters or is hovering over the widget.

If mouse_cursor is a WidgetStateMouseCursor, WidgetStateProperty::resolve is used for the following WidgetStates:

If null, then the value of SwitchThemeData::mouse_cursor is used. If that is also null, then WidgetStateMouseCursor::clickable is used.

§focus_color: Option<Color>

The color for the button’s Material when it has the input focus.

If overlay_color returns a non-null color in the WidgetState::Focused state, it will be used instead.

If null, then the value of SwitchThemeData::overlay_color is used in the focused state. If that is also null, then the value of ThemeData::focus_color is used.

§hover_color: Option<Color>

The color for the button’s Material when a pointer is hovering over it.

If overlay_color returns a non-null color in the WidgetState::Hovered state, it will be used instead.

If null, then the value of SwitchThemeData::overlay_color is used in the hovered state. If that is also null, then the value of ThemeData::hover_color is used.

§overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>

The color for the switch’s Material.

Resolves in the following states:

If null, then the value of active_thumb_color with alpha K_RADIAL_REACTION_ALPHA, focus_color and hover_color is used in the pressed, focused and hovered state. If that is also null, the value of SwitchThemeData::overlay_color is used. If that is also null, then the value of ColorScheme::secondary with alpha K_RADIAL_REACTION_ALPHA, ThemeData::focus_color and ThemeData::hover_color is used in the pressed, focused and hovered state.

§splash_radius: Option<f64>

The splash radius of the circular Material ink response.

If null, then the value of SwitchThemeData::splash_radius is used. If that is also null, then K_RADIAL_REACTION_RADIUS is used.

§focus_node: Option<FocusNodeRef>

An optional focus node to use as the focus node for this widget.

If one is not supplied, then one will be automatically allocated, owned, and managed by this widget. The widget will be focusable even if a focus_node is not supplied. If supplied, the given focus_node will be hosted by this widget, but not owned.

§on_focus_change: Option<ValueChanged<bool>>

Handler called when the focus changes.

Called with true if this widget’s node gains focus, and false if it loses focus.

§autofocus: bool

True if this widget will be selected as the initial focus when no other node in its scope is currently focused.

Defaults to false.

§padding: Option<EdgeInsetsGeometry>

The amount of space to surround the child inside the bounds of the Switch.

Defaults to horizontal padding of 4 pixels. If ThemeData::use_material3 is false, then there is no padding by default.

Implementations§

Source§

impl Switch

Source

pub fn new(value: bool, on_changed: Option<ValueChanged<bool>>) -> Switch

Creates a Material Design switch.

The switch itself does not maintain any state. Instead, when the state of the switch changes, the widget calls the on_changed callback. Most widgets that use a switch will listen for the on_changed callback and rebuild the switch with a new value to update the visual appearance of the switch.

The following arguments are required:

  • value determines whether this switch is on or off.
  • on_changed is called when the user toggles the switch on or off.

Dart’s Switch has no applyCupertinoTheme parameter: its applyCupertinoTheme is false, so an apply_cupertino_theme set on it is ignored (effective_apply_cupertino_theme).

Source

pub fn adaptive(value: bool, on_changed: Option<ValueChanged<bool>>) -> Switch

Creates an adaptive Switch based on whether the target platform is iOS or macOS, following Material design’s Cross-platform guidelines.

Creates a switch that looks and feels native when the ThemeData::platform is iOS or macOS, otherwise a Material Design switch is created.

To provide a custom switch theme that’s only used by this factory constructor, pass a custom Adaptation<SwitchThemeData> class to the adaptations parameter of ThemeData. This can be useful in situations where you don’t want the overall ThemeData::switch_theme to apply when this adaptive constructor is used.

The target platform is based on the current Theme: ThemeData::platform.

Source

pub fn key(self, key: KeyRef) -> Switch

Dart Switch(key:).

Source

pub fn active_color(self, active_color: Color) -> Switch

👎Deprecated:

Use activeThumbColor instead. This feature was deprecated after v3.31.0-2.0.pre.

Dart Switch(activeColor:).

Source

pub fn active_thumb_color(self, active_thumb_color: Color) -> Switch

Dart Switch(activeThumbColor:).

Source

pub fn active_track_color(self, active_track_color: Color) -> Switch

Dart Switch(activeTrackColor:).

Source

pub fn inactive_thumb_color(self, inactive_thumb_color: Color) -> Switch

Dart Switch(inactiveThumbColor:).

Source

pub fn inactive_track_color(self, inactive_track_color: Color) -> Switch

Dart Switch(inactiveTrackColor:).

Source

pub fn thumb_color( self, thumb_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch

Dart Switch(thumbColor:).

Source

pub fn track_color( self, track_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch

Dart Switch(trackColor:).

Source

pub fn track_outline_color( self, track_outline_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch

Dart Switch(trackOutlineColor:).

Source

pub fn track_outline_width( self, track_outline_width: WidgetStatePropertyRef<Option<f64>>, ) -> Switch

Dart Switch(trackOutlineWidth:).

Source

pub fn thumb_icon( self, thumb_icon: WidgetStatePropertyRef<Option<Rc<Icon>>>, ) -> Switch

Dart Switch(thumbIcon:).

Source

pub fn material_tap_target_size( self, material_tap_target_size: MaterialTapTargetSize, ) -> Switch

Dart Switch(materialTapTargetSize:).

Source

pub fn apply_cupertino_theme(self, apply_cupertino_theme: bool) -> Switch

Dart Switch.adaptive(applyCupertinoTheme:).

Source

pub fn drag_start_behavior( self, drag_start_behavior: DragStartBehavior, ) -> Switch

Dart Switch(dragStartBehavior:).

Source

pub fn mouse_cursor(self, mouse_cursor: impl Into<MouseCursorRef>) -> Switch

Dart Switch(mouseCursor:).

Source

pub fn focus_color(self, focus_color: Color) -> Switch

Dart Switch(focusColor:).

Source

pub fn hover_color(self, hover_color: Color) -> Switch

Dart Switch(hoverColor:).

Source

pub fn overlay_color( self, overlay_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch

Dart Switch(overlayColor:).

Source

pub fn splash_radius(self, splash_radius: f64) -> Switch

Dart Switch(splashRadius:).

Source

pub fn focus_node(self, focus_node: FocusNodeRef) -> Switch

Dart Switch(focusNode:).

Source

pub fn on_focus_change( self, on_focus_change: impl Fn(&mut App, bool) + 'static, ) -> Switch

Dart Switch(onFocusChange:).

Source

pub fn autofocus(self, autofocus: bool) -> Switch

Dart Switch(autofocus:).

Source

pub fn padding(self, padding: impl Into<EdgeInsetsGeometry>) -> Switch

Dart Switch(padding:).

Source

pub fn effective_apply_cupertino_theme(&self) -> Option<bool>

Dart’s applyCupertinoTheme field: what apply_cupertino_theme was given, and false for a switch made with Switch::new, whose constructor sets it so.

Trait Implementations§

Source§

impl Clone for Switch

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Switch

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StatelessWidget for Switch

Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef

Describes the part of the user interface represented by this widget. Read more
Source§

fn debug_check_constructor(&self)

Dart’s constructor asserts that relate two or more arguments. Read more
Source§

fn as_preferred_size_widget( &self, ) -> Option<&(dyn PreferredSizeWidget + 'static)>

Dart’s this is PreferredSizeWidget: a widget that implements PreferredSizeWidget answers with itself, which is what a parent asking an erased child for its preferred size reads.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Switch

§

impl !Send for Switch

§

impl !Sync for Switch

§

impl !UnwindSafe for Switch

§

impl Forget for Switch

§

impl Freeze for Switch
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn Fn(&mut App, bool)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, bool)>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<Color>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<Color>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<f64>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<f64>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<Rc<Icon>>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<Rc<Icon>>>>>: Freeze, *const RcInner<dyn MouseCursor> is !null: Freeze, PhantomData<RcInner<dyn MouseCursor>>: Freeze, NonZero<u32>: Freeze, &'static FocusNodeVTable: Freeze, *const RcInner<Keep> is !null: Freeze, PhantomData<RcInner<Keep>>: Freeze,

§

impl Move for Switch

§

impl Unpin for Switch
where Rc<dyn Key>: Unpin, Rc<dyn Fn(&mut App, bool)>: Unpin, Rc<dyn WidgetStateProperty<Option<Color>>>: Unpin, Rc<dyn WidgetStateProperty<Option<f64>>>: Unpin, Rc<dyn WidgetStateProperty<Option<Rc<Icon>>>>: Unpin, Rc<dyn MouseCursor>: Unpin, NonZero<u32>: Unpin, &'static FocusNodeVTable: Unpin, Rc<Keep>: Unpin,

§

impl UnsafeUnpin for Switch
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, bool)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, bool)>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<Color>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<Color>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<f64>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<f64>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<Rc<Icon>>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<Rc<Icon>>>>>: UnsafeUnpin, *const RcInner<dyn MouseCursor> is !null: UnsafeUnpin, PhantomData<RcInner<dyn MouseCursor>>: UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, &'static FocusNodeVTable: UnsafeUnpin, *const RcInner<Keep> is !null: UnsafeUnpin, PhantomData<RcInner<Keep>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<W> IntoWidget<StatelessKind> for W
where W: StatelessWidget,

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>