Flutter · One codebase, native feel
Explain the widget tree and how it updates.
Short answer
The widget tree is an immutable description of the UI. Flutter diffs new widgets against the element tree, updates reused elements, and then rebuilds only the render objects that need layout or paint.
Every widget is a lightweight configuration object. Widgets are rebuilt often and should stay cheap. The element tree is the long-lived structure that connects widgets to render objects and preserves state. The render tree performs layout, painting and hit testing.
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: const [
Text('Account'),
SizedBox(height: 8),
Card(child: ListTile(title: Text('Billing'))),
],
),
);
}What happens on setState
- The framework marks the element dirty and schedules a frame
- build runs again and returns a new widget subtree
- Elements compare old and new widget runtime type and key
- Matching elements update their widget reference and rebuild children as needed
- Render objects update only when their configuration or constraints changed
A rebuild is not the same as a repaint. Rebuilding can be cheap if the widgets are const and the subtree is small. Repainting is expensive when it touches large areas or triggers layer allocation. Splitting widgets limits how far a rebuild propagates.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between a widget and an element?
- 2Why are widgets immutable?
- 3Does every rebuild cause a repaint?
- 4How does const influence widget rebuilds?
Say it in the interview
The widget tree is an immutable description of the UI. When state changes, Flutter rebuilds widgets, compares them with the existing element tree, and reuses elements whose type and key match. The element tree keeps state and links to render objects, which handle layout and paint. A rebuild does not automatically mean a repaint, and const subtrees let Flutter skip work.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Flutter mock interview and get scored feedback on depth, clarity and confidence.