Sub topic 12
Under the Hood: Widgets, Elements, and Render Objects
Assume you know basic Dart syntax, widget composition, and the difference between StatelessWidget and StatefulWidget. This chapter focuses on what Flutter creates and keeps behind the widget code you write.
Three layers of Flutter UI
Flutter represents the interface with three related trees:
| Layer | Main purpose | Usually changes? |
|---|---|---|
| Widget | Describes configuration | Often replaced |
| Element | Holds a widget's location and identity | Usually reused |
| Render object | Performs layout, painting, and hit testing | Usually reused |
A widget is an immutable description of part of the interface. For example, Text('Hello') describes text that should appear. A widget can be replaced whenever its parent rebuilds.
An element is a persistent object inserted into the widget tree. It connects a widget description to its place in the tree. It also stores information such as the BuildContext. In fact, a BuildContext is an interface to an element.
A render object does the visual work. It calculates size and position during layout, draws pixels during painting, and can participate in hit testing for pointer events. Only widgets that need rendering create render objects. A Column, for example, creates an element but delegates its layout to a render object created by RenderFlex.
What happens during a rebuild
When setState runs, Flutter marks the associated element as dirty. During the next frame, Flutter calls that element's build method. The method returns new widget objects.
Flutter then compares the new widgets with the old widgets at the same position. If their types and keys match, Flutter updates the existing element instead of creating a new one. The element updates its widget reference and may update its render object. This is why rebuilding does not mean rebuilding every object from scratch.
For a StatefulWidget, the State object belongs to the element. Rebuilding creates a new immutable StatefulWidget configuration, but Flutter keeps the existing State when the element can be matched. That is how fields such as a counter survive rebuilds.
BuildContext and tree location
BuildContext identifies a widget's location in the element tree. It gives code access to inherited information, such as theme data or media size:
@override
Widget build(BuildContext context) {
final color = Theme.of(context).colorScheme.primary;
return Text(
'Ready',
style: TextStyle(color: color),
);
}A context is location-sensitive. Looking up an inherited widget with a context above the provider can produce a different result from looking it up with a context below the provider. Avoid storing a context for later use unless the API specifically supports that pattern.
Rebuilds, layout, and painting
These operations are related but different:
- A rebuild runs widget
buildmethods and updates element configuration. - Layout calculates sizes and positions.
- Painting draws the render objects.
- Hit testing finds which render object receives pointer input.
A rebuild may cause layout or painting, but Flutter can sometimes reuse layout and painting results. Keep frequently changing state in a small widget so that fewer elements need to rebuild.
A practical debugging model
When UI behaves unexpectedly, ask:
- Did the widget rebuild?
- Did Flutter match the new widget with the intended old element?
- Did a key or widget type change?
- Did the render object receive new constraints or data?
- Did layout or painting run after the update?
This model helps distinguish state-preservation problems from layout or rendering problems.
Exercise
Why can a new StatefulWidget object still use the same State object after a rebuild?
Show answer
Flutter matches the new widget with the existing element using its type and key. The element keeps its State object and updates its widget configuration.