Skip to main content

Widget

Trait Widget 

Source
pub trait Widget:
    Any
    + Debug
    + 'static {
Show 22 methods // Required methods fn as_any(&self) -> &dyn Any; fn create_render_object(&self) -> Box<dyn RenderObject>; // Provided methods fn type_name(&self) -> &'static str { ... } fn is_stateful(&self) -> bool { ... } fn is_focusable(&self) -> bool { ... } fn wants_ime(&self) -> bool { ... } fn captures_tab(&self) -> bool { ... } fn create_state(&self) -> Option<Box<dyn State>> { ... } fn children(&self) -> Vec<WidgetNode> { ... } fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> { ... } fn handle_event(&self, event: &WidgetEvent, local_pos: Point) { ... } fn accepts_scroll(&self) -> bool { ... } fn key(&self) -> Option<u64> { ... } fn gesture_state(&self) -> Option<GestureState> { ... } fn dynamic_signal(&self) -> Option<Signal<Point>> { ... } fn semantics(&self) -> Option<WidgetSemantics> { ... } fn as_inherited(&self) -> Option<&dyn InheritedWidget> { ... } fn can_update(&self, old: &dyn Widget) -> bool { ... } fn is_equivalent(&self, _old: &dyn Widget) -> bool { ... } fn is_repaint_boundary(&self) -> bool { ... } fn visible_children(&self) -> Option<Vec<bool>> { ... } fn parent_data(&self) -> Option<ParentData> { ... }
}
Expand description

不可变 widget 配置:描述“要显示什么”,由 Element 树实例化并 diff。

Required Methods§

Source

fn as_any(&self) -> &dyn Any

类型擦除入口(用于 downcast 到具体 widget)。

Source

fn create_render_object(&self) -> Box<dyn RenderObject>

创建 RenderObject(桥接到渲染子系统)

Provided Methods§

Source

fn type_name(&self) -> &'static str

类型名称(调试用,Debug impl 用)

Source

fn is_stateful(&self) -> bool

是否 stateful(true 则 Element 自动创建 State 并持久化)

Source

fn is_focusable(&self) -> bool

是否可聚焦(Tab 焦点链 + 键盘激活)

Source

fn wants_ime(&self) -> bool

是否需要 IME(软键盘 —— TextField/TextBox 等输入组件)

Source

fn captures_tab(&self) -> bool

是否捕获 Tab 键(编辑器等需要 Tab 缩进的输入组件)。 true 时框架焦点遍历跳过该组件,Tab/BackTab 直接投递给焦点 element。

Source

fn create_state(&self) -> Option<Box<dyn State>>

创建 State 实例(仅 is_stateful=true 时 Element 会调)

Source

fn children(&self) -> Vec<WidgetNode>

子 widget 列表(Element 树递归用)

Source

fn children_with_positions(&self) -> Vec<(Point, WidgetNode)>

子 widget + 各自在父内的 offset(Stack/Positioned 等需要)

Source

fn handle_event(&self, event: &WidgetEvent, local_pos: Point)

处理事件(不可变 — Widget 是 Rc 共享的;stateful widget 通常把逻辑放在 State 里)

Source

fn accepts_scroll(&self) -> bool

是否接受滚动事件(ScrollView/ListView 返回 true)

Source

fn key(&self) -> Option<u64>

唯一标识(用于 ListView 复用 Element)

Source

fn gesture_state(&self) -> Option<GestureState>

手势竞技场仲裁(返回 Some 表示参与指针竞争)

Source

fn dynamic_signal(&self) -> Option<Signal<Point>>

返回需要订阅的 Signal(PositionedWidget 用)

Source

fn semantics(&self) -> Option<WidgetSemantics>

返回无障碍语义(阶段 A11y)

Source

fn as_inherited(&self) -> Option<&dyn InheritedWidget>

如果是 InheritedWidget,返回引用(主题/布局约束等传播)

Source

fn can_update(&self, old: &dyn Widget) -> bool

Element diff 时判断新旧 widget 是否“兼容“(类型相同 + key 相同) 默认:type_id 比较

Source

fn is_equivalent(&self, _old: &dyn Widget) -> bool

P0-1:新旧 widget 的全部输入是否等价(不含 State)。

默认 false = 保守重建。返回 true 必须保证 create_render_object、 layout/paint、children 的输入完全一致。纯 stateless 子树可据此整体跳过; stateful widget 仍会执行 build,只用它判断自己的 RO 是否可保留。

Source

fn is_repaint_boundary(&self) -> bool

是否 RepaintBoundary(dirty 重绘边界)

Source

fn visible_children(&self) -> Option<Vec<bool>>

逐子节点可见性(按 children() 顺序)。None = 全部可见(默认,零行为变化)。 Some(vec) 与 children() 等长;false 表示该子节点的整棵子树 (含其内部 RepaintBoundary layer)不参与合成。

框架在 paint_layer_recursive 中沿 element 祖先链上溯此列表, 被隐藏分支的 layer 不 push 进本帧 sub_layers(即不被 blit)。

Source

fn parent_data(&self) -> Option<ParentData>

父容器在布局阶段读取的子节点元数据(如 Flex 的 grow/basis)。 默认 None(无元数据)。Expanded/Flexible 等 override 返回 ParentData::Flex。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§