java.lang.Object
com.ultikits.ultitools.abstracts.gui.declarative.core.State<T>
Type Parameters:
T - the corresponding StatefulWidget type
Direct Known Subclasses:
NavigatorState

public abstract class State<T extends StatefulWidget> extends Object
State is the mutable state of a StatefulWidget.

State objects are managed by the framework and have the following properties:

  • Created when the StatefulWidget is inserted into the tree
  • Destroyed when the StatefulWidget is removed from the tree
  • Reused across StatefulWidget rebuilds

Lifecycle:

  1. initState() - initializes state, called exactly once
  2. build(BuildContext) - builds the UI, may be called multiple times
  3. didUpdateWidget(StatefulWidget) - called when the configuration changes
  4. dispose() - releases resources, called exactly once

Usage example:


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

     @Override
     void initState() {
         super.initState();
         // initialization work
     }

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

    • _widget

      @Nullable private T extends StatefulWidget _widget
    • _element

      @Nullable private @Nullable StatefulElement _element
    • _dirty

      private boolean _dirty
    • _mounted

      private boolean _mounted
  • Constructor Details

    • State

      public State()
      Creates a new State.
  • Method Details

    • getWidget

      @NotNull public T getWidget()
      Gets the Widget associated with this State.
      Returns:
      the Widget instance
      Throws:
      IllegalStateException - if the State is not mounted yet
    • isMounted

      public boolean isMounted()
      Checks whether the State is mounted in the tree.
      Returns:
      true if mounted
    • setState

      public void setState(@NotNull @NotNull VoidCallback fn)
      Marks the State as dirty and schedules a rebuild.

      This method notifies the framework that the State object has changed and the UI needs to be rebuilt. The framework calls the build method on the next frame.

      Important: the callback should only modify state and must not have side effects.

      Usage example:

      
       void handleClick() {
           setState(() -> {
               _counter++;  // only modify state
           });
       }
       
      Parameters:
      fn - the callback that modifies state
    • initState

      protected void initState()
      Initializes the state.

      Called once, after the State object is created and before it is inserted into the tree. Subscribe to streams, start animations, or run other one-time initialization here.

      Must call super.initState().

    • dispose

      protected void dispose()
      Releases resources.

      Called once, when the State object is permanently removed from the tree. Unsubscribe, stop animations, or run other cleanup work here.

      Must call super.dispose().

    • didUpdateWidget

      protected void didUpdateWidget(@NotNull T oldWidget)
      Called when the Widget configuration changes.

      Called when the parent Widget rebuilds and creates a new StatefulWidget instance. Compare the old and new Widget properties here and adjust state accordingly.

      Parameters:
      oldWidget - the previous Widget instance
    • build

      @NotNull public abstract @NotNull Widget build(@NotNull @NotNull BuildContext context)
      Builds this State's UI.

      This method is called:

      • after initState
      • after didUpdateWidget
      • after setState
      • when a depended-upon InheritedWidget changes
      Parameters:
      context - the build context
      Returns:
      the Widget tree
    • setElement

      void setElement(@Nullable @Nullable StatefulElement element)
    • setWidget

      void setWidget(@NotNull @NotNull StatefulWidget widget)
    • setMounted

      void setMounted(boolean mounted)
    • isDirty

      boolean isDirty()
    • clearDirty

      void clearDirty()
    • didUpdateWidgetInternal

      void didUpdateWidgetInternal(@NotNull @NotNull StatefulWidget oldWidget)
      Internal method: invokes didUpdateWidget using the raw type to avoid generics issues.