Skip to main content

Slider

Struct Slider 

Source
pub struct Slider {
Show 23 fields pub key: Option<KeyRef>, pub value: f64, pub secondary_track_value: Option<f64>, pub on_changed: Option<ValueChanged<f64>>, pub on_change_start: Option<ValueChanged<f64>>, pub on_change_end: Option<ValueChanged<f64>>, pub min: f64, pub max: f64, pub divisions: Option<i32>, pub label: Option<String>, pub active_color: Option<Color>, pub inactive_color: Option<Color>, pub secondary_active_color: Option<Color>, pub thumb_color: Option<Color>, pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>, pub mouse_cursor: Option<MouseCursorRef>, pub semantic_formatter_callback: Option<SemanticFormatterCallback>, pub focus_node: Option<FocusNodeRef>, pub autofocus: bool, pub allowed_interaction: Option<SliderInteraction>, pub padding: Option<EdgeInsetsGeometry>, pub show_value_indicator: Option<ShowValueIndicator>, pub year2023: Option<bool>, /* private fields */
}
Expand description

A Material Design slider.

Used to select from a range of values.

A slider can be used to select from either a continuous or a discrete set of values. The default is to use a continuous range of values from min to max. To use discrete values, use a non-null value for divisions, which indicates the number of discrete intervals. For example, if min is 0.0 and max is 50.0 and divisions is 5, then the slider can take on the discrete values 0.0, 10.0, 20.0, 30.0, 40.0, and 50.0.

The terms for the parts of a slider are:

  • The “thumb”, which is a shape that slides horizontally when the user drags it.
  • The “track”, which is the line that the slider thumb slides along.
  • The “value indicator”, which is a shape that pops up when the user is dragging the thumb to indicate the value being selected.
  • The “active” side of the slider is the side between the thumb and the minimum value.
  • The “inactive” side of the slider is the side between the thumb and the maximum value.

The slider will be disabled if on_changed is null or if the range given by min..max is empty (i.e. if min is equal to max).

The slider widget itself does not maintain any state. Instead, when the state of the slider changes, the widget calls the on_changed callback. Most widgets that use a slider will listen for the on_changed callback and rebuild the slider with a new value to update the visual appearance of the slider. To know when the value starts to change, or when it is done changing, set the optional callbacks on_change_start and/or on_change_end.

By default, a slider will be as wide as possible, centered vertically. When given unbounded constraints, it will attempt to make the track 144 pixels wide (with margins on each side) and will shrink-wrap vertically.

Requires one of its ancestors to be a Material widget.

Requires one of its ancestors to be a MediaQuery widget. Typically, these are introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree.

To determine how it should be displayed (e.g. colors, thumb shape, etc.), a slider uses the SliderThemeData available from either a SliderTheme widget or the ThemeData::slider_theme a Theme widget above it in the widget tree. You can also override some of the colors with the active_color and inactive_color properties, although more fine-grained control of the look is achieved using a SliderThemeData.

Dart’s constructor asserts run when the slider is turned into a WidgetRef (into_widget), once every setter has run.

See also:

Fields§

§key: Option<KeyRef>§value: f64

The currently selected value for this slider.

The slider’s thumb is drawn at a position that corresponds to this value.

§secondary_track_value: Option<f64>

The secondary track value for this slider.

If not null, a secondary track using secondary_active_color color is drawn between the thumb and this value, over the inactive track.

If less than value, then the secondary track is not shown.

It can be ideal for media scenarios such as showing the buffering progress while the value shows the play progress.

§on_changed: Option<ValueChanged<f64>>

Called during a drag when the user is selecting a new value for the slider by dragging.

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

If null, the slider 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.

See also:

  • on_change_start for a callback that is called when the user starts changing the value.
  • on_change_end for a callback that is called when the user stops changing the value.
§on_change_start: Option<ValueChanged<f64>>

Called when the user starts selecting a new value for the slider.

This callback shouldn’t be used to update the slider value (use on_changed for that), but rather to be notified when the user has started selecting a new value by starting a drag or with a tap.

The value passed will be the last value that the slider had before the change began.

See also:

  • on_change_end for a callback that is called when the value change is complete.
§on_change_end: Option<ValueChanged<f64>>

Called when the user is done selecting a new value for the slider.

This callback shouldn’t be used to update the slider value (use on_changed for that), but rather to know when the user has completed selecting a new value by ending a drag or a click.

See also:

  • on_change_start for a callback that is called when a value change begins.
§min: f64

The minimum value the user can select.

Defaults to 0.0. Must be less than or equal to max.

If the max is equal to the min, then the slider is disabled.

§max: f64

The maximum value the user can select.

Defaults to 1.0. Must be greater than or equal to min.

If the max is equal to the min, then the slider is disabled.

§divisions: Option<i32>

The number of discrete divisions.

Typically used with label to show the current discrete value.

If null, the slider is continuous.

§label: Option<String>

A label to show inside the value indicator above the slider’s thumb.

By default, the label is shown while dragging a discrete slider, where divisions is non-null. It can also be shown for a continuous slider, where divisions is null, by setting show_value_indicator to ShowValueIndicator::OnlyForContinuous, ShowValueIndicator::OnDrag, or ShowValueIndicator::AlwaysVisible.

If show_value_indicator is null, the ambient SliderThemeData::show_value_indicator determines when the label is shown.

The label is rendered using the active ThemeData’s TextTheme::body_large text style, with the theme data’s ColorScheme::on_primary color. The label’s text style can be overridden with SliderThemeData::value_indicator_text_style.

If null, then the value indicator will not be displayed.

Ignored if this slider is created with Slider::adaptive.

See also:

  • SliderComponentShape for how to create a custom value indicator shape.
§active_color: Option<Color>

The color to use for the portion of the slider track that is active.

The “active” side of the slider is the side between the thumb and the minimum value.

If null, SliderThemeData::active_track_color of the ambient SliderTheme is used. If that is null, ColorScheme::primary of the surrounding ThemeData is used.

Using a SliderTheme gives much more fine-grained control over the appearance of various components of the slider.

§inactive_color: Option<Color>

The color for the inactive portion of the slider track.

The “inactive” side of the slider is the side between the thumb and the maximum value.

If null, SliderThemeData::inactive_track_color of the ambient SliderTheme is used. If year2023 is false and ThemeData::use_material3 is true, then ColorScheme::secondary_container is used and if ThemeData::use_material3 is false, ColorScheme::primary with an opacity of 0.24 is used. Otherwise, ColorScheme::surface_container_highest is used.

Using a SliderTheme gives much more fine-grained control over the appearance of various components of the slider.

Ignored if this slider is created with Slider::adaptive.

§secondary_active_color: Option<Color>

The color to use for the portion of the slider track between the thumb and the secondary_track_value.

Defaults to the SliderThemeData::secondary_active_track_color of the current SliderTheme.

If that is also null, defaults to ColorScheme::primary with an opacity of 0.54.

Using a SliderTheme gives much more fine-grained control over the appearance of various components of the slider.

Ignored if this slider is created with Slider::adaptive.

§thumb_color: Option<Color>

The color of the thumb.

If this color is null, Slider will use active_color, If active_color is also null, Slider will use SliderThemeData::thumb_color.

If that is also null, defaults to ColorScheme::primary.

  • CupertinoSlider will have a white thumb (like the native default iOS slider).
§overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>

The highlight color that’s typically used to indicate that the slider thumb is focused, hovered, or dragged.

If this property is null, Slider will use active_color with an opacity of 0.12, If null, SliderThemeData::overlay_color will be used.

If that is also null, If ThemeData::use_material3 is true, Slider will use ColorScheme::primary with an opacity of 0.08 when slider thumb is hovered and with an opacity of 0.1 when slider thumb is focused or dragged, If ThemeData::use_material3 is false, defaults to ColorScheme::primary with an opacity of 0.12.

§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 SliderThemeData::mouse_cursor is used. If that is also null, then WidgetStateMouseCursor::clickable is used.

§semantic_formatter_callback: Option<SemanticFormatterCallback>

The callback used to create a semantic value from a slider value.

Defaults to formatting values as a percentage.

This is used by accessibility frameworks like TalkBack on Android to inform users what the currently selected value is with more context.

Ignored if this slider is created with Slider::adaptive

§focus_node: Option<FocusNodeRef>

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

§autofocus: bool

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

§allowed_interaction: Option<SliderInteraction>

Allowed way for the user to interact with the Slider.

For example, if this is set to SliderInteraction::TapOnly, the user can interact with the slider only by tapping anywhere on the track. Sliding will have no effect.

Defaults to SliderInteraction::TapAndSlide.

§padding: Option<EdgeInsetsGeometry>

Determines the padding around the Slider.

If specified, this padding overrides the default vertical padding of the Slider, defaults to the height of the overlay shape, and the horizontal padding, defaults to the width of the thumb shape or overlay shape, whichever is larger.

§show_value_indicator: Option<ShowValueIndicator>

Determines the conditions under which the value indicator is shown.

If show_value_indicator is null then the ambient SliderThemeData::show_value_indicator is used. If that is also null, defaults to ShowValueIndicator::OnlyForDiscrete.

§year2023: Option<bool>

When true, the Slider will use the 2023 Material Design 3 appearance. Defaults to true.

If this is set to false, the Slider will use the latest Material Design 3 appearance, which was introduced in December 2023.

If ThemeData::use_material3 is false, then this property is ignored.

Deprecated in Flutter after v3.27.0-0.1.pre; its setter carries the deprecation.

Implementations§

Source§

impl Slider

Source

pub fn new(value: f64, on_changed: Option<ValueChanged<f64>>) -> Slider

Creates a Material Design slider.

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

  • value determines currently selected value for this slider.
  • on_changed is called while the user is selecting a new value for the slider.
  • on_change_start is called when the user starts to select a new value for the slider.
  • on_change_end is called when the user is done selecting a new value for the slider.

You can override some of the colors with the active_color and inactive_color properties, although more fine-grained control of the appearance is achieved using a SliderThemeData.

Source

pub fn adaptive(value: f64, on_changed: Option<ValueChanged<f64>>) -> Slider

Creates an adaptive Slider based on the target platform, following Material design’s Cross-platform guidelines.

Creates a CupertinoSlider if the target platform is iOS or macOS, creates a Material Design slider otherwise.

If a CupertinoSlider is created, the following parameters are ignored: secondary_track_value, label, inactive_color, secondary_active_color, semantic_formatter_callback, show_value_indicator.

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

Dart’s Slider.adaptive has no padding parameter: its padding is null, so a padding set on it is ignored (effective_padding).

Source

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

Dart Slider(key:).

Source

pub fn secondary_track_value(self, secondary_track_value: f64) -> Slider

Dart Slider(secondaryTrackValue:).

Source

pub fn on_change_start(self, on_change_start: ValueChanged<f64>) -> Slider

Dart Slider(onChangeStart:).

Source

pub fn on_change_end(self, on_change_end: ValueChanged<f64>) -> Slider

Dart Slider(onChangeEnd:).

Source

pub fn min(self, min: f64) -> Slider

Dart Slider(min:).

Source

pub fn max(self, max: f64) -> Slider

Dart Slider(max:).

Source

pub fn divisions(self, divisions: i32) -> Slider

Dart Slider(divisions:).

Source

pub fn label(self, label: impl Into<String>) -> Slider

Dart Slider(label:).

Source

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

Dart Slider(activeColor:).

Source

pub fn inactive_color(self, inactive_color: Color) -> Slider

Dart Slider(inactiveColor:).

Source

pub fn secondary_active_color(self, secondary_active_color: Color) -> Slider

Dart Slider(secondaryActiveColor:).

Source

pub fn thumb_color(self, thumb_color: Color) -> Slider

Dart Slider(thumbColor:).

Source

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

Dart Slider(overlayColor:).

Source

pub fn mouse_cursor(self, mouse_cursor: MouseCursorRef) -> Slider

Dart Slider(mouseCursor:).

Source

pub fn semantic_formatter_callback( self, semantic_formatter_callback: SemanticFormatterCallback, ) -> Slider

Dart Slider(semanticFormatterCallback:).

Source

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

Dart Slider(focusNode:).

Source

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

Dart Slider(autofocus:).

Source

pub fn allowed_interaction( self, allowed_interaction: SliderInteraction, ) -> Slider

Dart Slider(allowedInteraction:).

Source

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

Dart Slider(padding:).

Source

pub fn show_value_indicator( self, show_value_indicator: ShowValueIndicator, ) -> Slider

Dart Slider(showValueIndicator:).

Source

pub fn year2023(self, year2023: bool) -> Slider

👎Deprecated:

Set this flag to false to opt into the 2024 slider appearance. Defaults to true. In the future, this flag will default to false. Use SliderThemeData to customize individual properties. This feature was deprecated after v3.27.0-0.1.pre.

Dart Slider(year2023:).

Source

pub fn effective_padding(&self) -> Option<EdgeInsetsGeometry>

Dart’s padding field: what padding was given, and null for a slider made with Slider::adaptive, whose constructor sets it so.

Trait Implementations§

Source§

impl Clone for Slider

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 Slider

Source§

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

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

impl StatefulWidget for Slider

Source§

type State = SliderState

The state this widget creates.
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 debug_check_constructor(&self)

Source§

fn create_state(&self) -> SliderState

Creates the mutable state for this widget at a given location in the tree. Read more
Source§

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

Source§

fn type_name(&self) -> &'static str

See Widget::type_name. A stateful form another widget kind becomes (an animated or status transition widget) names the widget it wraps.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Slider

§

impl !Send for Slider

§

impl !Sync for Slider

§

impl !UnwindSafe for Slider

§

impl Forget for Slider

§

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

§

impl Move for Slider

§

impl Unpin for Slider
where Rc<dyn Key>: Unpin, Rc<dyn Fn(&mut App, f64)>: Unpin, *const u8 is !null: Unpin, Rc<dyn WidgetStateProperty<Option<Color>>>: Unpin, Rc<dyn MouseCursor>: Unpin, Rc<dyn Fn(f64) -> String>: Unpin, NonZero<u32>: Unpin, &'static FocusNodeVTable: Unpin, Rc<Keep>: Unpin,

§

impl UnsafeUnpin for Slider
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, f64)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, f64)>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<Color>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<Color>>>>: UnsafeUnpin, *const RcInner<dyn MouseCursor> is !null: UnsafeUnpin, PhantomData<RcInner<dyn MouseCursor>>: UnsafeUnpin, *const RcInner<dyn Fn(f64) -> String> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(f64) -> String>>: 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<StatefulKind> for W
where W: StatefulWidget,

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>