java.lang.Object
com.ultikits.ultitools.abstracts.gui.declarative.core.Widget
com.ultikits.ultitools.abstracts.gui.declarative.core.StatefulWidget
Direct Known Subclasses:
Navigator

public abstract class StatefulWidget extends Widget
StatefulWidget is a Widget that carries mutable state.

It separates the Widget's configuration (fixed by its constructor arguments) from its state (managed by a State object). When State.setState(VoidCallback) is called, the framework schedules a rebuild using a new StatefulWidget instance while keeping the same State object.

When to use it:

  • UI that needs user interaction (button clicks, form input)
  • UI that needs to manage internal state (expand/collapse, selection state)
  • UI that needs to subscribe to an external data source (live data updates)

Lifecycle:

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

Usage example:


 public class CounterButton extends StatefulWidget {
     @Override
     public State<CounterButton> createState() {
         return new CounterState();
     }
 }

 class CounterState extends State<CounterButton> {
     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
See Also:
  • Constructor Details

    • StatefulWidget

      protected StatefulWidget()
      Creates a new StatefulWidget.
    • StatefulWidget

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

    • createState

      @NotNull public abstract @NotNull State<? extends StatefulWidget> createState()
      Creates the state object associated with this Widget.

      The framework calls this method the first time the Widget is inserted into the tree. The same State object is reused across Widget rebuilds.

      Returns:
      the new State instance
    • createElement

      @NotNull public @NotNull Element createElement()
      Description copied from class: Widget
      Creates the Element corresponding to this Widget.

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

      Specified by:
      createElement in class Widget
      Returns:
      the corresponding Element instance