Skip to content

VisualDensity 继承自: Enum

Defines the visual density of user interface components.

示例 ​

python
import flet as ft


def density_card(visual_density: ft.VisualDensity) -> ft.Container:
    return ft.Container(
        width=300,
        padding=12,
        border=ft.Border.all(1, ft.Colors.RED),
        border_radius=10,
        bgcolor=ft.Colors.SURFACE_CONTAINER_LOW,
        theme=ft.Theme(visual_density=visual_density),
        dark_theme=ft.Theme(visual_density=visual_density),
        content=ft.Column(
            spacing=8,
            controls=[
                ft.Text(visual_density.name, weight=ft.FontWeight.BOLD),
                ft.IconButton(icon=ft.Icons.ADD),
                ft.Checkbox(label="Checkbox", value=True),
                ft.Chip(
                    label="Explore topics",
                    leading=ft.Icon(ft.Icons.EXPLORE_OUTLINED),
                ),
                ft.RadioGroup(
                    value="1",
                    content=ft.Row(
                        controls=[
                            ft.Radio(label=f"{i}", value=f"{i}") for i in range(1, 4)
                        ],
                    ),
                ),
            ],
        ),
    )


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

    page.appbar = ft.AppBar(title="VisualDensity Showcase")
    page.add(
        ft.SafeArea(
            content=ft.Column(
                controls=[
                    ft.Text(
                        "Compare component density presets across Material controls."
                    ),
                    ft.Row(
                        wrap=True,
                        spacing=12,
                        expand=True,
                        scroll=ft.ScrollMode.AUTO,
                        alignment=ft.MainAxisAlignment.CENTER,
                        controls=[density_card(vd) for vd in ft.VisualDensity],
                    ),
                ],
            ),
        )
    )


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

枚举值 ​

名称类型默认值说明
STANDARD—'standard'The default/standard profile for visual density. This default value represents a visual density that is less dense than either COMFORTABLE or COMPACT, and corresponds to density values of zero in both axes.
COMPACT—'compact'The profile for a "compact" interpretation of visual density. Individual components will interpret the density value independently, making themselves more visually dense than STANDARD and COMFORTABLE to different degrees 基于 the Material Design specification of the COMFORTABLE setting for their particular use case. It corresponds to a density value of -2 in both axes.
COMFORTABLE—'comfortable'The profile for a "comfortable" interpretation of visual density. Individual components will interpret the density value independently, making themselves more visually dense than STANDARD and less dense than COMPACT to different degrees 基于 the Material Design specification of the comfortable setting for their particular use case. It corresponds to a density value of -1 in both axes.
ADAPTIVE_PLATFORM_DENSITY—'adaptivePlatformDensity'Visual density that is adaptive 基于 the given platform. For desktop platforms, this returns COMPACT, and for other platforms, it returns a default-constructed visual density.

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