Skip to content

ButtonStyle

用于控制按钮的所有视觉方面,例如形状、前景色、背景色、阴影颜色、内容内边距、边框宽度和圆角。

这些样式属性大多可以针对按钮的全部或特定 ControlState 进行配置, 例如 HOVERED、FOCUSED、DISABLED 等。

属性 ​

名称类型默认值说明
colorOptional[ControlStateValue[ColorValue]]NoneThe color for the button's Text and Icon control descendants.
bgcolorOptional[ControlStateValue[ColorValue]]NoneThe button's background fill color.
overlay_colorOptional[ControlStateValue[ColorValue]]NoneThe highlight color that's typically used to indicate that the button is focused, hovered, or pressed.
shadow_colorOptional[ControlStateValue[ColorValue]]NoneThe shadow color of the button's Material.
elevationOptional[ControlStateValue[Optional[Number]]]NoneThe elevation of the button's Material.
animation_durationOptional[DurationValue]NoneDefines the duration in milliseconds of animated changes for shape and elevation.
paddingOptional[ControlStateValue[PaddingValue]]NoneThe padding between the button's boundary and its content.
sideOptional[ControlStateValue[BorderSide]]NoneDefines the button's border outline.
shapeOptional[ControlStateValue[OutlinedBorder]]NoneThe shape of the button's underlying Material.
alignmentOptional[Alignment]NoneThe alignment of the button's content.
enable_feedbackOptional[bool]NoneWhether detected gestures should provide acoustic and/or haptic feedback.
text_styleOptional[ControlStateValue[TextStyle]]NoneThe text style of the button's Text control descendants.
icon_sizeOptional[ControlStateValue[Optional[Number]]]NoneThe icon's size inside of the button.
icon_colorOptional[ControlStateValue[ColorValue]]NoneThe icon's color inside the button. If not set or None, then the color will be used.
visual_densityOptional[VisualDensity]NoneDefines how compact the button's layout will be.
mouse_cursorOptional[ControlStateValue[MouseCursor]]NoneThe cursor to be displayed when the mouse pointer enters or is hovering over the button.

方法 ​

名称类型默认值说明
copy'ButtonStyle'—返回此对象的副本,可覆盖指定属性。

使用示例 ​

你可以为悬停状态配置不同的形状、背景色,并为所有其他状态配置回退值。

要为所有 Material 状态配置某个样式属性,将其值设为一个字面值(或类实例)即可。例如,如果把 color 属性设为字面值,该值会应用到按钮的所有状态:

python
ButtonStyle(
    color=ft.Colors.WHITE
)

要为特定的 Material 状态配置某个样式属性,将其值设为一个字典,其中键为状态名。例如,要为 HOVERED 和 FOCUSED 状态配置不同的背景色,同时为所有其他状态配置另外的颜色:

python
ButtonStyle(
    color={
        ft.ControlState.HOVERED: ft.Colors.WHITE,
        ft.ControlState.FOCUSED: ft.Colors.BLUE,
        ft.ControlState.DEFAULT: ft.Colors.BLACK,
    }
)

各种按钮形状示例 ​

python
import flet as ft

def main(page: ft.Page):
    page.padding = 30
    page.spacing = 30
    page.add(
        ft.FilledButton(
            "Stadium",
            style=ft.ButtonStyle(
                shape=ft.StadiumBorder(),
            ),
        ),
        ft.FilledButton(
            "Rounded rectangle",
            style=ft.ButtonStyle(
                shape=ft.RoundedRectangleBorder(radius=10),
            ),
        ),
        ft.FilledButton(
            "Continuous rectangle",
            style=ft.ButtonStyle(
                shape=ft.ContinuousRectangleBorder(radius=30),
            ),
        ),
        ft.FilledButton(
            "Beveled rectangle",
            style=ft.ButtonStyle(
                shape=ft.BeveledRectangleBorder(radius=10),
            ),
        ),
        ft.FilledButton(
            "Circle",
            style=ft.ButtonStyle(shape=ft.CircleBorder(), padding=30),
        ),
    )

ft.run(main)

样式化按钮示例 ​

请查看以下示例:

python
import flet as ft

def main(page: ft.Page):

    page.add(
        ft.Button(
            "Styled button 1",
            style=ft.ButtonStyle(
                color={
                    ft.ControlState.HOVERED: ft.Colors.WHITE,
                    ft.ControlState.FOCUSED: ft.Colors.BLUE,
                    ft.ControlState.DEFAULT: ft.Colors.BLACK,
                },
                bgcolor={ft.ControlState.FOCUSED: ft.Colors.PINK_200, "": ft.Colors.YELLOW},
                padding={ft.ControlState.HOVERED: 20},
                overlay_color=ft.Colors.TRANSPARENT,
                elevation={"pressed": 0, "": 1},
                animation_duration=500,
                side={
                    ft.ControlState.DEFAULT: ft.BorderSide(1, ft.Colors.BLUE),
                    ft.ControlState.HOVERED: ft.BorderSide(2, ft.Colors.BLUE),
                },
                shape={
                    ft.ControlState.HOVERED: ft.RoundedRectangleBorder(radius=20),
                    ft.ControlState.DEFAULT: ft.RoundedRectangleBorder(radius=2),
                },
            ),
        )
    )

ft.run(main)

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