الانتقال إلى المحتوى
تطبيقات الموبايلFlutter· 7 دقائق للقراءة

Widgets Are Blueprints, Not Screen Objects: Flutter's Three Trees

Flutter throws your widgets away on every build; the element keeps your State and the render object does the drawing.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Flutter 3.x
محتويات المقال

A teammate builds a quantity stepper for the cart screen. Tapping + does nothing. A print in the handler shows the field going up, 2, 3, 4, while the screen still says 1. Then the cart total elsewhere on the page updates, and the field itself is back to 1. Flutter isn't broken. The code is changing an object that was never on the screen.

The common misconception

The belief: a widget is the thing you see. Text('1') is the label on screen, and when your data changes, Flutter updates that object, the way you'd set the text of a UILabel on iOS or a TextView on Android.

It's a natural assumption. In most UI toolkits, "widget" means exactly that: a long-lived control with properties you mutate. Flutter code reads like it constructs those objects, the DevTools inspector shows a widget tree that mirrors the screen, and the name StatefulWidget sounds like the widget itself holds state. The half-truth is the structure: the widget tree does describe what's on screen. It just doesn't stay there.

What actually happens

Flutter keeps three trees side by side, and each one has a different job.

Widgets: immutable blueprints

The API docs define a widget as something that "describes the configuration for an Element", and its fields must all be final: widgets follow . The Flutter docs call them temporary objects, used to build a presentation of the app in its current state. Every call to build() returns brand-new widget objects, and the previous ones are dropped. That's affordable because a widget is a small bundle of constructor arguments, not a view. The same widget object can even be placed in the tree several times.

Elements: the instance at a place in the tree

An element is "an instantiation of a Widget at a particular location in the tree". The is the part that persists: when a parent rebuilds, Flutter hands the existing element a new widget instead of creating a new element. Elements own the two things worth keeping between frames: the State of stateful widgets and the render objects underneath. You've already met them, too. The BuildContext passed to build() is, in the docs' words, actually an Element object.

Render objects: layout and paint

A is "an object in the render tree", and it does the real work: layout, painting and hit testing. Only render object widgets create one. Padding creates a RenderPadding, and Text builds a RichText whose render object is a RenderParagraph. Your own StatelessWidget and StatefulWidget classes never create one; they only compose other widgets. That's why the render tree is a subset of the element tree and much smaller than the widget code you write.

What a rebuild really does

When setState runs, the framework marks that element dirty. On the next frame:

  1. The element calls build() and gets a fresh set of widgets.
  2. For each child, it compares the old and new widget. If their runtimeType and key match, which is the rule behind Widget.canUpdate, the existing element is updated with the new widget and keeps its State and render object.
  3. A render object widget copies its new configuration onto the existing render object, so a changed color or padding is an update in place, not a new object.
  4. If the type or key differs, the old element and everything below it is removed, and a new one is created from the new widget.
  5. If the new widget is the very same instance as last time, as const widgets often are, the walk stops there.

The docs are explicit that this isn't a general tree diff: Flutter checks each element's child list on its own, in linear time.

Here's the stepper, written both ways:

Dart quantity_stepper.dart
import 'package:flutter/material.dart';class QuantityStepper extends StatelessWidget {  QuantityStepper({super.key});  int quantity = 1; // warning: must_be_immutable  @override  Widget build(BuildContext context) {    return Row(children: [      Text('$quantity'),      IconButton(        icon: const Icon(Icons.add),        onPressed: () => quantity++, // the field changes, the screen doesn't      ),    ]);  }}

In the first version, quantity++ edits a field on a blueprint. Nothing marks the element dirty, so build() never runs and the Text keeps showing 1. When the parent rebuilds for the cart total, it creates a new QuantityStepper whose field starts at 1 again, and the element switches to that one. The analyzer even warns you: must_be_immutable. In the second version, the number lives in State, which the element owns. Run both in a widget test and you can see it: after three taps and a parent rebuild, the first still showed 1 and the second showed 4, with the same State object as before, even though widget now pointed to a new QuantityStepper instance.

WidgetElementRender object
RoleDescribes the configurationThe widget's instance at a place in the treeLayout, painting, hit testing
LifetimeOne buildWhile its spot keeps the same type and keyAs long as its element
Mutable?No, all fields are finalYes, it gets new widgetsYes, updated in place
HoldsConstructor argumentsState, the link to its render objectSize, position, paint logic
Created byYou, in build()The framework, via createElementRender object widgets only

How to apply it in your project

  1. Keep every widget field finalTreat the must_be_immutable warning as a bug, not as noise. If a widget needs a value that changes, it doesn't own that value.
  2. Put changing data in State, and change it through setStateA counter, a selection or a loading flag goes in a State object or in a state-management class above it. Change it inside setState (or through your state-management tool) so the element is marked dirty and rebuilds.
  3. Stop worrying about creating widgets in build()New widget objects on every build are the design, not a leak. Worry about expensive work inside build(), and use const constructors where you can so Flutter can skip unchanged subtrees.
  4. Debug lost state by looking at types and keysWhen a State resets, check what sits at that position across rebuilds: a different widget class, a conditional that swaps widgets, or a key that changes every time.

Key takeaways

  • A widget is an immutable blueprint: every build creates new ones and drops the old.
  • The element at each place in the tree persists, holding your State and the link to its render object.
  • Render objects do layout and painting, and Flutter updates them in place instead of recreating them.
  • Mutating a widget's field changes nothing on screen; change State with setState instead.
  • Same type and key at the same spot keeps the element; anything else replaces it, State included.

📚 المصادر

  1. Inside Flutter — Flutter docs
  2. Widget class — Flutter API
  3. Element class — Flutter API
  4. RenderObject class — Flutter API
  5. Introduction to widgets — Flutter docs

التعليقات (0)