Class StatefulWidget
java.lang.Object
com.ultikits.ultitools.abstracts.gui.declarative.core.Widget
com.ultikits.ultitools.abstracts.gui.declarative.core.StatefulWidget
- Direct Known Subclasses:
Navigator
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:
State.initState()- initializes state, called exactly onceState.build(BuildContext)- builds the UI, may be called multiple timesState.didUpdateWidget(StatefulWidget)- called when the configuration changesState.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 Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new StatefulWidget.protectedStatefulWidget(SlotKey key) Creates a new StatefulWidget with the given key. -
Method Summary
Modifier and TypeMethodDescription@NotNull ElementCreates the Element corresponding to this Widget.abstract @NotNull State<? extends StatefulWidget> Creates the state object associated with this Widget.
-
Constructor Details
-
StatefulWidget
protected StatefulWidget()Creates a new StatefulWidget. -
StatefulWidget
Creates a new StatefulWidget with the given key.- Parameters:
key- the key used to stably identify this Widget
-
-
Method Details
-
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
Description copied from class:WidgetCreates the Element corresponding to this Widget.This is a framework-internal method; subclasses should return the Element implementation appropriate to the Widget type.
- StatelessWidget returns a
StatelessElement - StatefulWidget returns a
StatefulElement - RenderObjectWidget returns a
RenderObjectElement
- Specified by:
createElementin classWidget- Returns:
- the corresponding Element instance
- StatelessWidget returns a
-