GlobalKeyManager class

全局 GlobalKey 管理器

GlobalKeyManager 是一个单例类,用于管理所有菜单项的 GlobalKey 它的主要目的是解决二级菜单位置计算问题,确保二级菜单能够正确定位在对应的一级菜单下方

为什么需要 GlobalKey? 在 Flutter 中,要获取一个 Widget 的位置和尺寸,需要通过 GlobalKey 获取其 RenderBox 然后通过 RenderBox.localToGlobal 方法计算位置

为什么使用单例?

  1. 确保全局只有一个 GlobalKey 管理器实例,避免多个实例造成混乱
  2. 提供全局访问点,任何组件都可以注册或获取 GlobalKey
  3. 简化生命周期管理,特别是在切换应用时需要重置所有 GlobalKey

使用方式:

  1. 菜单项创建时:GlobalKeyManager().registerMenuKey(index, key)
  2. 获取菜单位置时:GlobalKeyManager().getMenuKey(index)
  3. 切换应用时:GlobalKeyManager().clearAllKeys()

Constructors

GlobalKeyManager()
工厂构造函数,返回单例实例
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

cleanupInvalidKeys() → void
清理所有无效的 GlobalKey
clearAllKeys() → void
清除所有 GlobalKey - 用于切换应用时重置
getAllMenuKeys() Map<int, GlobalKey<State<StatefulWidget>>>
获取所有菜单项的 GlobalKey
getMenuKey(String index) GlobalKey<State<StatefulWidget>>?
获取菜单项的 GlobalKey
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerMenuKey(String index, GlobalKey<State<StatefulWidget>> key) GlobalKey<State<StatefulWidget>>
注册菜单项的 GlobalKey
toString() String
A string representation of this object.
inherited
unregisterMenuKey(String index) → void
取消注册菜单项的 GlobalKey

Operators

operator ==(Object other) bool
The equality operator.
inherited