Slider
A slider provides a visual indication of adjustable content, as well as the current setting in the total range of content.
Use a slider when you want people to set defined values (such as volume or brightness), or when people would benefit from instant feedback on the effect of setting changes.
Inherits: LayoutControl
, AdaptiveControl
Properties
-
active_color
(ColorValue | None
) –The color to use for the portion of
-
autofocus
(bool
) –True if the control will be selected as the initial focus. If there is more
-
divisions
(int | None
) –The number of discrete divisions.
-
inactive_color
(ColorValue | None
) –The color for the inactive portion of
-
interaction
(SliderInteraction | None
) –The allowed way for the user to interact with this slider.
-
label
(str | None
) –A label to show above the slider when the slider is active. The value of
-
max
(Number
) –The maximum value the user can select.
-
min
(Number
) –The minimum value the user can select.
-
mouse_cursor
(MouseCursor | None
) –The cursor to be displayed when a mouse pointer enters or is hovering over this
-
overlay_color
(ControlStateValue[ColorValue] | None
) –The highlight color that's typically
-
padding
(PaddingValue | None
) –Determines the padding around this slider.
-
round
(int
) –The number of decimals displayed on the
label
-
secondary_active_color
(ColorValue | None
) –The color to use for the portion of
-
secondary_track_value
(Number | None
) –The secondary track value for this slider.
-
thumb_color
(ColorValue | None
) –The color of the thumb.
-
value
(Number | None
) –The currently selected value for this slider.
-
year_2023
(bool | None
) –If this is set to
False
, this slider will use the latest
Events
-
on_blur
(ControlEventHandler[Slider] | None
) –Called when this slider has lost focus.
-
on_change
(ControlEventHandler[Slider] | None
) –Called when the state of this slider is changed.
-
on_change_end
(ControlEventHandler[Slider] | None
) –Called when the user is done selecting a new value for this slider.
-
on_change_start
(ControlEventHandler[Slider] | None
) –Called when the user starts selecting a new value for this slider.
-
on_focus
(ControlEventHandler[Slider] | None
) –Called when this slider has received focus.
Examples#
Basic Example#
import flet as ft
def main(page: ft.Page):
page.add(
ft.Text("Default slider:"),
ft.Slider(),
ft.Text("Default disabled slider:"),
ft.Slider(disabled=True),
)
ft.run(main)
Setting a custom label#
Handling events#
import flet as ft
def main(page: ft.Page):
def slider_changed(e: ft.Event[ft.Slider]):
message.value = f"Slider changed to {e.control.value}"
message.update()
page.add(
ft.Text("Slider with 'on_change' event:"),
ft.Slider(
min=0,
max=100,
divisions=10,
label="{value}%",
on_change=slider_changed,
),
message := ft.Text(),
)
ft.run(main)
Random values#
import asyncio
import random
import flet as ft
async def main(page: ft.Page):
def handle_slider_change(e: ft.Event[ft.Slider]):
message.value = f"Slider.value changed to {e.control.value}"
message.update()
page.add(
ft.Text("Slider with 'on_change' event:"),
slider := ft.Slider(label="{value}", on_change=handle_slider_change),
message := ft.Text(),
)
while True:
await asyncio.sleep(1)
slider.value = random.random()
event = ft.Event("_", slider, data=slider.value)
handle_slider_change(event)
slider.update()
ft.run(main)
Properties#
active_color
#
active_color: ColorValue | None = None
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.
autofocus
#
autofocus: bool = False
True if the control will be selected as the initial focus. If there is more than one control on a page with autofocus set, then the first one added to the page will get focus.
divisions
#
divisions: int | None = None
The number of discrete divisions.
Typically used with label
to show the current discrete value.
If None
, this slider is continuous.
inactive_color
#
inactive_color: ColorValue | None = None
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.
interaction
#
interaction: SliderInteraction | None = None
The allowed way for the user to interact with this slider.
If None
, SliderTheme.interaction
is used.
If that's is also None
, defaults to
SliderInteraction.TAP_AND_SLIDE
.
label
#
label: str | None = None
A label to show above the slider when the slider is active. The value of
label
may contain {value}
which will be dynamicly replaced with a current slider
value. For example, "Volume: {value}"
.
It is used to display the value of a discrete slider, and it is displayed as part of the value indicator shape.
If not set, then the value indicator will not be displayed.
mouse_cursor
#
mouse_cursor: MouseCursor | None = None
The cursor to be displayed when a mouse pointer enters or is hovering over this control.
overlay_color
#
overlay_color: ControlStateValue[ColorValue] | None = None
The highlight color that's typically
used to indicate that the range slider thumb is in
ControlState.HOVERED
or
ControlState.DRAGGED
states.
secondary_active_color
#
secondary_active_color: ColorValue | None = None
The color to use for the portion of
the slider track between the thumb and
the secondary_track_value
.
secondary_track_value
#
secondary_track_value: Number | None = None
The secondary track value for this slider.
If not null, a secondary track using secondary_active_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.
value
#
value: Number | None = None
The currently selected value for this slider.
The slider's thumb is drawn at a position that corresponds to this value.
Defaults to value of min
.
Raises:
year_2023
#
year_2023: bool | None = None
If this is set to False
, this slider will use the latest
Material Design 3 appearance, which was introduced in December 2023.
When True
, the Slider will use the 2023 Material Design 3 appearance.
If not set, then the
SliderTheme.year_2023
will be
used, which is False
by default.
If Theme.use_material3
is False
,
then this property is ignored.
Events#
on_blur
#
on_blur: ControlEventHandler[Slider] | None = None
Called when this slider has lost focus.
on_change
#
on_change: ControlEventHandler[Slider] | None = None
Called when the state of this slider is changed.
on_change_end
#
on_change_end: ControlEventHandler[Slider] | None = None
Called when the user is done selecting a new value for this slider.
on_change_start
#
on_change_start: ControlEventHandler[Slider] | None = None
Called when the user starts selecting a new value for this slider.
on_focus
#
on_focus: ControlEventHandler[Slider] | None = None
Called when this slider has received focus.