java.lang.Object
com.ultikits.ultitools.abstracts.gui.declarative.core.SlotKey

public final class SlotKey extends Object
SlotKey 用于在 Widget 树中唯一标识一个 Widget。

在列表或动态内容中,SlotKey 对于 diff 算法至关重要。 它允许框架识别哪些 Widget 是"同一个"(只是数据变化), 哪些是新增或删除的,从而避免不必要的重建。

使用场景:

  • 列表项需要稳定的 key 来保持状态(如滚动位置、选中状态)
  • 动态内容需要 key 来触发正确的动画或过渡
  • 复用 Element 以提高性能

最佳实践:


 // 好的做法:使用业务唯一标识作为 key
 ListView.builder()
     .children(players.stream()
         .map(p -> PlayerWidget.builder()
             .key(SlotKey.of(p.getUniqueId().toString()))  // 稳定的 UUID
             .player(p)
             .build())
         .toList())
     .build();

 // 避免:使用列表索引作为 key(除非列表是静态的)
 // 这样会导致数据更新时,key 与实际内容不匹配
 
Since:
6.2.0
Version:
1.0.0
Author:
UltiTools Team
See Also:
  • Field Details

    • value

      @NotNull private final @NotNull String value
  • Constructor Details

    • SlotKey

      private SlotKey(@NotNull @NotNull String value)
  • Method Details

    • of

      @NotNull public static @NotNull SlotKey of(@NotNull @NotNull String value)
      创建一个 SlotKey。
      Parameters:
      value - key 值,不能为空
      Returns:
      新的 SlotKey 实例
      Throws:
      IllegalArgumentException - 如果 value 为空
    • of

      @NotNull public static @NotNull SlotKey of(@NotNull @NotNull String prefix, @NotNull @NotNull String value)
      创建一个带有前缀的 SlotKey。
      Parameters:
      prefix - 前缀
      value - key 值
      Returns:
      新的 SlotKey 实例
    • getValue

      @NotNull public @NotNull String getValue()
      获取 key 的字符串值。
      Returns:
      key 值
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object