Class State<T extends StatefulWidget>
- Type Parameters:
T- the corresponding StatefulWidget type
- Direct Known Subclasses:
NavigatorState
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:
initState()- initializes state, called exactly oncebuild(BuildContext)- builds the UI, may be called multiple timesdidUpdateWidget(StatefulWidget)- called when the configuration changesdispose()- 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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract @NotNull Widgetbuild(@NotNull BuildContext context) Builds this State's UI.(package private) voidprotected voiddidUpdateWidget(T oldWidget) Called when the Widget configuration changes.(package private) voiddidUpdateWidgetInternal(@NotNull StatefulWidget oldWidget) Internal method: invokes didUpdateWidget using the raw type to avoid generics issues.protected voiddispose()Releases resources.Gets the Widget associated with this State.protected voidInitializes the state.(package private) booleanisDirty()booleanChecks whether the State is mounted in the tree.(package private) voidsetElement(@Nullable StatefulElement element) (package private) voidsetMounted(boolean mounted) voidsetState(@NotNull VoidCallback fn) Marks the State as dirty and schedules a rebuild.(package private) voidsetWidget(@NotNull StatefulWidget widget)
-
Field Details
-
_widget
-
_element
-
_dirty
private boolean _dirty -
_mounted
private boolean _mounted
-
-
Constructor Details
-
State
public State()Creates a new State.
-
-
Method Details
-
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
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
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
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
-
setWidget
-
setMounted
void setMounted(boolean mounted) -
isDirty
boolean isDirty() -
clearDirty
void clearDirty() -
didUpdateWidgetInternal
Internal method: invokes didUpdateWidget using the raw type to avoid generics issues.
-