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:
SwitchListTile, which combines this widget with aListTileso that you can give the switch a label.Checkbox, another widget with similar semantics.Radio, for selecting among a set of explicit values.Slider, for selecting a value in a range.WidgetStateProperty, an interface for objects that “resolve” to different values depending on a widget’s material state.- https://material.io/design/components/selection-controls.html#switches
Fields§
§key: Option<KeyRef>§value: boolWhether 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>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:
| State | Light theme | Dark theme |
|---|---|---|
| Default | Colors.grey.shade50 | Colors.grey.shade400 |
| Selected | ColorScheme::secondary | ColorScheme::secondary |
| Disabled | Colors.grey.shade400 | Colors.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:
| State | Light theme | Dark theme |
|---|---|---|
| Default | Color(0x52000000) | Colors.white30 |
| Selected | active_thumb_color with alpha 0x80 | active_thumb_color with alpha 0x80 |
| Disabled | Colors.black12 | Colors.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>>>§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:
MaterialTapTargetSize, for a description of how this affects tap targets.
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: DragStartBehaviorDetermines 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: boolTrue 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
impl Switch
Sourcepub fn new(value: bool, on_changed: Option<ValueChanged<bool>>) -> Switch
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:
valuedetermines whether this switch is on or off.on_changedis 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).
Sourcepub fn adaptive(value: bool, on_changed: Option<ValueChanged<bool>>) -> Switch
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.
Sourcepub fn active_color(self, active_color: Color) -> Switch
👎Deprecated: Use activeThumbColor instead. This feature was deprecated after v3.31.0-2.0.pre.
pub fn active_color(self, active_color: Color) -> Switch
Use activeThumbColor instead. This feature was deprecated after v3.31.0-2.0.pre.
Dart Switch(activeColor:).
Sourcepub fn active_thumb_color(self, active_thumb_color: Color) -> Switch
pub fn active_thumb_color(self, active_thumb_color: Color) -> Switch
Dart Switch(activeThumbColor:).
Sourcepub fn active_track_color(self, active_track_color: Color) -> Switch
pub fn active_track_color(self, active_track_color: Color) -> Switch
Dart Switch(activeTrackColor:).
Sourcepub fn inactive_thumb_color(self, inactive_thumb_color: Color) -> Switch
pub fn inactive_thumb_color(self, inactive_thumb_color: Color) -> Switch
Dart Switch(inactiveThumbColor:).
Sourcepub fn inactive_track_color(self, inactive_track_color: Color) -> Switch
pub fn inactive_track_color(self, inactive_track_color: Color) -> Switch
Dart Switch(inactiveTrackColor:).
Sourcepub fn thumb_color(
self,
thumb_color: WidgetStatePropertyRef<Option<Color>>,
) -> Switch
pub fn thumb_color( self, thumb_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch
Dart Switch(thumbColor:).
Sourcepub fn track_color(
self,
track_color: WidgetStatePropertyRef<Option<Color>>,
) -> Switch
pub fn track_color( self, track_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch
Dart Switch(trackColor:).
Sourcepub fn track_outline_color(
self,
track_outline_color: WidgetStatePropertyRef<Option<Color>>,
) -> Switch
pub fn track_outline_color( self, track_outline_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch
Dart Switch(trackOutlineColor:).
Sourcepub fn track_outline_width(
self,
track_outline_width: WidgetStatePropertyRef<Option<f64>>,
) -> Switch
pub fn track_outline_width( self, track_outline_width: WidgetStatePropertyRef<Option<f64>>, ) -> Switch
Dart Switch(trackOutlineWidth:).
Sourcepub fn thumb_icon(
self,
thumb_icon: WidgetStatePropertyRef<Option<Rc<Icon>>>,
) -> Switch
pub fn thumb_icon( self, thumb_icon: WidgetStatePropertyRef<Option<Rc<Icon>>>, ) -> Switch
Dart Switch(thumbIcon:).
Sourcepub fn material_tap_target_size(
self,
material_tap_target_size: MaterialTapTargetSize,
) -> Switch
pub fn material_tap_target_size( self, material_tap_target_size: MaterialTapTargetSize, ) -> Switch
Dart Switch(materialTapTargetSize:).
Sourcepub fn apply_cupertino_theme(self, apply_cupertino_theme: bool) -> Switch
pub fn apply_cupertino_theme(self, apply_cupertino_theme: bool) -> Switch
Dart Switch.adaptive(applyCupertinoTheme:).
Sourcepub fn drag_start_behavior(
self,
drag_start_behavior: DragStartBehavior,
) -> Switch
pub fn drag_start_behavior( self, drag_start_behavior: DragStartBehavior, ) -> Switch
Dart Switch(dragStartBehavior:).
Sourcepub fn mouse_cursor(self, mouse_cursor: impl Into<MouseCursorRef>) -> Switch
pub fn mouse_cursor(self, mouse_cursor: impl Into<MouseCursorRef>) -> Switch
Dart Switch(mouseCursor:).
Sourcepub fn focus_color(self, focus_color: Color) -> Switch
pub fn focus_color(self, focus_color: Color) -> Switch
Dart Switch(focusColor:).
Sourcepub fn hover_color(self, hover_color: Color) -> Switch
pub fn hover_color(self, hover_color: Color) -> Switch
Dart Switch(hoverColor:).
Sourcepub fn overlay_color(
self,
overlay_color: WidgetStatePropertyRef<Option<Color>>,
) -> Switch
pub fn overlay_color( self, overlay_color: WidgetStatePropertyRef<Option<Color>>, ) -> Switch
Dart Switch(overlayColor:).
Sourcepub fn splash_radius(self, splash_radius: f64) -> Switch
pub fn splash_radius(self, splash_radius: f64) -> Switch
Dart Switch(splashRadius:).
Sourcepub fn focus_node(self, focus_node: FocusNodeRef) -> Switch
pub fn focus_node(self, focus_node: FocusNodeRef) -> Switch
Dart Switch(focusNode:).
Sourcepub fn on_focus_change(
self,
on_focus_change: impl Fn(&mut App, bool) + 'static,
) -> Switch
pub fn on_focus_change( self, on_focus_change: impl Fn(&mut App, bool) + 'static, ) -> Switch
Dart Switch(onFocusChange:).
Sourcepub fn padding(self, padding: impl Into<EdgeInsetsGeometry>) -> Switch
pub fn padding(self, padding: impl Into<EdgeInsetsGeometry>) -> Switch
Dart Switch(padding:).
Sourcepub fn effective_apply_cupertino_theme(&self) -> Option<bool>
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 StatelessWidget for Switch
impl StatelessWidget for Switch
Source§fn debug_fill_properties(
&self,
_app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )
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
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef
Source§fn debug_check_constructor(&self)
fn debug_check_constructor(&self)
Source§fn as_preferred_size_widget(
&self,
) -> Option<&(dyn PreferredSizeWidget + 'static)>
fn as_preferred_size_widget( &self, ) -> Option<&(dyn PreferredSizeWidget + 'static)>
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 Switchwhere
*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 Switchwhere
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 Switchwhere
*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> 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<W> IntoWidget<StatelessKind> for Wwhere
W: StatelessWidget,
impl<W> IntoWidget<StatelessKind> for Wwhere
W: StatelessWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.