java.lang.Object
com.ultikits.ultitools.abstracts.gui.declarative.core.Widget
Direct Known Subclasses:
Container, GridView, RenderObjectWidget, StatefulWidget, StatelessWidget

public abstract class Widget extends Object
Widget is the core abstract class of the declarative UI framework.

A Widget is an immutable, lightweight configuration object that describes part of the UI. Every state change creates a new Widget instance, but the framework reuses Elements efficiently through the diff algorithm.

Core idea: UI = f(state)

A Widget itself holds no mutable state and never touches an Inventory directly. It only describes "what should be shown", never "how" or "where" it is shown.

Subclass types:

  • StatelessWidget - a stateless, purely functional Widget that depends only on its input parameters
  • StatefulWidget - a stateful Widget that can rebuild in response to setState
  • RenderObjectWidget - a render Widget, corresponding to an actual RenderNode

Usage example:


 // Create a counter Widget
 public class CounterWidget extends StatefulWidget {
     @Override
     public State createState() {
         return new CounterState();
     }
 }

 class CounterState extends State<CounterWidget> {
     private int count = 0;

     @Override
     public Widget build(BuildContext context) {
         return TextButton.builder()
             .text("Count: " + count)
             .onClick(() -> setState(() -> count++))
             .build();
     }
 }
 
Since:
6.2.0
Version:
1.0.0
Author:
UltiTools Team
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final @Nullable SlotKey
    The optional key used to stably identify this Widget within a list.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new Widget instance.
    protected
    Widget(@Nullable SlotKey key)
    Creates a new Widget instance with the given key.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canUpdate(@NotNull Element element)
    Checks whether this Widget can update the given Element.
    abstract @NotNull Element
    Creates the Element corresponding to this Widget.
    boolean
     
    @Nullable SlotKey
    Gets this Widget's key.
    int
     
    boolean
    isSameWidget(@Nullable Widget other)
    Checks whether two Widgets represent the same configuration.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • key

      @Nullable private final @Nullable SlotKey key
      The optional key used to stably identify this Widget within a list.

      When a Widget sits inside a list, it should supply a stable key so the framework can recognize which Widgets are the same one, which optimizes diff performance.

  • Constructor Details

    • Widget

      protected Widget()
      Creates a new Widget instance.
    • Widget

      protected Widget(@Nullable @Nullable SlotKey key)
      Creates a new Widget instance with the given key.
      Parameters:
      key - the key used to stably identify this Widget
  • Method Details

    • getKey

      @Nullable public @Nullable SlotKey getKey()
      Gets this Widget's key.
      Returns:
      the key, or null if none was set
    • createElement

      @NotNull public abstract @NotNull Element createElement()
      Creates the Element corresponding to this Widget.

      This is a framework-internal method; subclasses should return the Element implementation appropriate to the Widget type.

      Returns:
      the corresponding Element instance
    • canUpdate

      public boolean canUpdate(@NotNull @NotNull Element element)
      Checks whether this Widget can update the given Element.

      The default implementation checks whether the runtime types match. Subclasses may override this to provide custom update logic.

      Parameters:
      element - the Element to check
      Returns:
      true if the Element can be updated
    • isSameWidget

      public boolean isSameWidget(@Nullable @Nullable Widget other)
      Checks whether two Widgets represent the same configuration.

      Two Widgets are considered the same Widget when their keys match and their runtime types match. This is used by the diff algorithm to decide whether an Element can be reused.

      Parameters:
      other - the other Widget
      Returns:
      true if they are the same
    • 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