Class Widget
- Direct Known Subclasses:
Container,GridView,RenderObjectWidget,StatefulWidget,StatelessWidget
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 parametersStatefulWidget- a stateful Widget that can rebuild in response to setStateRenderObjectWidget- 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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks whether this Widget can update the given Element.abstract @NotNull ElementCreates the Element corresponding to this Widget.boolean@Nullable SlotKeygetKey()Gets this Widget's key.inthashCode()booleanisSameWidget(@Nullable Widget other) Checks whether two Widgets represent the same configuration.toString()
-
Field Details
-
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
Creates a new Widget instance with the given key.- Parameters:
key- the key used to stably identify this Widget
-
-
Method Details
-
getKey
Gets this Widget's key.- Returns:
- the key, or null if none was set
-
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.
- StatelessWidget returns a
StatelessElement - StatefulWidget returns a
StatefulElement - RenderObjectWidget returns a
RenderObjectElement
- Returns:
- the corresponding Element instance
- StatelessWidget returns a
-
canUpdate
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
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
-
hashCode
public int hashCode() -
toString
-