getHiddenElements static method
获取指定插槽的需隐藏元素集合
从上下文向上自动查找 AliPlayerWidget,返回相应插槽的隐藏配置。 此实现使各个 UI 组件能够内聚地获取配置,而无需外部逐层传入。
⚠️ 性能警告 / Performance Warning:
此方法内部调用 context.findAncestorWidgetOfExactType(),会遍历 widget tree。
若需要检查多个元素可见性,建议调用一次此方法获取 hiddenElements,
然后使用 HiddenElementsExt.isElementVisible 扩展方法进行多次检查,避免重复遍历。
⚠️ This method calls context.findAncestorWidgetOfExactType() internally,
which traverses the widget tree. When checking visibility of multiple elements,
prefer calling this method once and using HiddenElementsExt.isElementVisible for subsequent checks.
Implementation
static Set<String> getHiddenElements(
BuildContext context,
SlotType slotType,
) {
// Note keria:
// 使用 findAncestorWidgetOfExactType 而非 InheritedWidget,主要基于性能与使用场景的考量:
//
// 1. hiddenSlotElements 属于配置型数据,初始化后基本不会发生变化;
// 2. AliPlayerWidget 在播放过程中会因进度、状态变化频繁触发 rebuild;
//
// 如果使用 InheritedWidget:
// - 虽然数据读取本身是 O(1),但其依赖机制会在依赖发生变化或 rebuild 时,
// 触发所有依赖该 InheritedWidget 的子组件进行级联重建;
// - 在播放器高频 rebuild 的场景下,这种机制会带来额外的性能开销。
//
// 相比 findAncestorWidgetOfExactType:
// - 其查找复杂度是 O(n),其中 n 为向上遍历的祖先层级数。
// - 虽然需要向上查找 widget tree(通常仅 5~10 层),
// - 但不会建立依赖关系,也不会引发额外的子树重建,整体开销更可控。
//
// 因此在当前场景下,该方案更有利于避免不必要的 rebuild,提高性能稳定性。
// 后续仍需持续关注 UI 性能表现,避免潜在性能劣化。
final widget = context.findAncestorWidgetOfExactType<AliPlayerWidget>();
return widget?.hiddenSlotElements[slotType] ?? const <String>{};
}