主题
ControlState 继承自: Enum
一些控件在接收用户输入时可能进入的交互状态。
这些状态由 Material 3 设计规范 specification 定义,但并不限于此。
并非所有状态都始终适用于所有控件。
枚举值
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
HOVERED | — | 'hovered' | The state when the user drags their mouse cursor over the given control. See material docs. |
FOCUSED | — | 'focused' | The state when the user navigates with the keyboard to a given control. This can also sometimes be triggered when a control is tapped. For example, when a [TextField] is tapped, it becomes FOCUSED. See material docs. |
PRESSED | — | 'pressed' | The state when the user is actively pressing down on the given control. See material docs. |
DRAGGED | — | 'dragged' | The state when this control is being dragged from one place to another by the user. See material docs. |
SELECTED | — | 'selected' | The state when this item has been selected. This applies to things that can be toggled (such as chips and checkboxes) and things that are selected from a set of options (such as tabs and radio buttons). See material docs. |
SCROLLED_UNDER | — | 'scrolledUnder' | The state when this control overlaps the content of a scrollable below. Used by AppBar to indicate that the primary scrollable's content has scrolled up and behind the app bar. |
DISABLED | — | 'disabled' | The state when this control is disabled and cannot be interacted with. Disabled controls should not respond to hover, focus, press, or drag interactions. See material docs. |
ERROR | — | 'error' | The state when the control has entered some form of invalid state. See material docs. |
DEFAULT | — | 'default' | The default state. Will be used for undeclared states. |
使用示例
配置 fill_color 有两种方式:
1. 所有状态使用同一取值
如果你想在所有 Material 状态下都使用同一种颜色,直接把 fill_color 赋值为单个颜色即可:
python
ft.Radio(fill_color=ft.Colors.GREEN)2. 为每个状态指定取值
若要更精细地控制,你可以传入一个字典,其中键为状态名,值为对应的颜色。
顺序很重要
- 状态在字典中出现的顺序决定它们的优先级,从而带来灵活性与可定制性。
DEFAULT的位置不影响行为——它始终拥有最低优先级,可以放在字典的任意位置。
示例
要为 Radio 按钮配置 ControlState.HOVERED 与 ControlState.FOCUSED 的不同填充色,并为所有其他状态指定一个回退颜色:
python
ft.Radio(
fill_color={
ft.ControlState.HOVERED: ft.Colors.GREEN,
ft.ControlState.FOCUSED: ft.Colors.RED,
ft.ControlState.DEFAULT: ft.Colors.BLACK,
}
)这样配置后,HOVERED 和 FOCUSED 状态会优先,而所有未指定的状态默认回退为 BLACK。