Skip to content

SliderInteraction 继承自: Enum

定义用户如何与 Slider 交互以改变其值。

每种模式控制是否接受在滑动条轨道上点击和拖拽的手势,以及是否将交互限制在拖动滑块上。

示例 ​

python
import flet as ft


def showcase_card(interaction: ft.SliderInteraction) -> ft.Container:
    value_text = ft.Text("Value: 50")

    def on_change(e: ft.Event[ft.Slider]):
        value_text.value = f"Value: {round(e.control.value)}"
        value_text.update()

    return ft.Container(
        width=340,
        padding=12,
        border=ft.Border.all(1, ft.Colors.RED),
        border_radius=10,
        bgcolor=ft.Colors.SURFACE_CONTAINER_LOW,
        content=ft.Column(
            spacing=8,
            controls=[
                ft.Text(interaction.name, weight=ft.FontWeight.BOLD),
                value_text,
                ft.Container(
                    width=260,
                    padding=ft.Padding.symmetric(horizontal=8),
                    border=ft.Border.all(1, ft.Colors.OUTLINE),
                    border_radius=8,
                    bgcolor=ft.Colors.SURFACE,
                    content=ft.Slider(
                        min=0,
                        max=100,
                        value=50,
                        divisions=20,
                        interaction=interaction,
                        on_change=on_change,
                    ),
                ),
                ft.Text("Try tapping track and dragging thumb.", size=11),
            ],
        ),
    )


def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    page.appbar = ft.AppBar(title="SliderInteraction Showcase")
    page.add(
        ft.SafeArea(
            content=ft.Column(
                controls=[
                    ft.Text("Compare which gestures are accepted by each slider mode."),
                    ft.Row(
                        wrap=True,
                        spacing=12,
                        expand=True,
                        scroll=ft.ScrollMode.AUTO,
                        alignment=ft.MainAxisAlignment.CENTER,
                        controls=[showcase_card(i) for i in ft.SliderInteraction],
                    ),
                ],
            ),
        )
    )


if __name__ == "__main__":
    ft.run(main)

枚举值 ​

名称类型默认值说明
TAP_AND_SLIDE—'tapAndSlide'Allows the user to interact with a slider by tapping or sliding anywhere on the track. Essentially all possible interactions are allowed. It is different from SLIDE_ONLY, in that when you try to slide anywhere other than the thumb, the thumb will move to the first point of contact.
TAP_ONLY—'tapOnly'Allows only tap interaction on the slider track. Drag/sliding gestures are ignored.
SLIDE_ONLY—'slideOnly'Allows only sliding interaction to change the value. Track taps are ignored.
SLIDE_THUMB—'slideThumb'Allows interaction only by dragging the slider thumb. Taps and drag/slide gestures that begin on the track are ignored.

关注我,学习大模型、大数据、Python等热门技术。