Sub topic 4
Elements: the Living Tree Behind Your Widgets
The missing layer
This chapter assumes basic Dart, simple Flutter apps, and common layout widgets such as Row, Column, Padding, and Text.
A widget is an immutable description of part of the user interface. An element is the long-lived object that places that widget in the app's tree. The element connects configuration to runtime behavior.
When Flutter receives a widget, it asks the widget to create an element. The element then manages that widget's position, parent-child relationship, and updates. A widget can be replaced during a rebuild, while its element is often reused.
flowchart TD
W[Widget: immutable configuration] -->|createElement| E[Element: long-lived instance]
E -->|build or update| C[Child elements]
E -->|for render widgets| R[Render object]The element tree has roughly the same shape as the widget tree, but the two trees have different jobs:
- The widget tree describes what the UI should be.
- The element tree keeps the live instances that manage those descriptions.
- The render object tree performs layout, painting, and hit testing.
Widgets such as Padding create elements and also create render objects indirectly. Widgets such as Directionality create elements but usually compose other widgets without creating a render object of their own.
Element lifecycle
An element moves through a lifecycle:
- Mount: Flutter inserts the element into the tree and gives it a parent and position.
- Update: Flutter supplies a new widget configuration to the existing element.
- Deactivate: Flutter temporarily removes the element from its current location. It may be reinserted, especially during a keyed move.
- Unmount: Flutter permanently removes the element. It will not participate in future frames.
A StatefulElement also owns the State object created by its StatefulWidget. The element and state usually survive ordinary rebuilds, even though the widget configuration changes.
import 'package:flutter/widgets.dart';
void main() {
runApp(const Directionality(
textDirection: TextDirection.ltr,
child: Counter(),
));
}
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int count = 0;
@override
void initState() {
super.initState();
debugPrint('State initialized');
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => setState(() => count++),
child: Text('Taps: $count'),
);
}
@override
void dispose() {
debugPrint('State disposed');
super.dispose();
}
}build receives a BuildContext. In practice, that context is the element associated with the widget currently being built. It lets Flutter look up information from the element's ancestors, such as inherited values, theme data, or media information.
Exercise
Exercise
Which object usually survives when a StatefulWidget rebuilds with a new configuration: the widget, its element, or neither?
Show answer
Its element usually survives, and the same State object remains attached to that element.