The big idea
Most UI frameworks hand your buttons and text to the operating system's built-in widgets. Flutter doesn't. It draws every pixel itself, the way a game engine does. The OS gives Flutter a blank canvas, and Flutter paints everything on it: buttons, text, shadows, scrolling lists.
That's why a Flutter app looks the same on every platform, and why Flutter needs its own rendering pipeline.
1. The three layers of Flutter
Think of Flutter as a three-storey building. Your code lives on the top floor, and each floor only talks to the one below it.
flowchart TD
A["Your app code (Dart)"] --> B
subgraph B["Framework (Dart)"]
B1["Material / Cupertino"]
B2["Widgets"]
B3["Rendering: layout and paint"]
B4["Animation, Gestures, Painting"]
end
B --> C
subgraph C["Engine (C++)"]
C1["Dart runtime"]
C2["Impeller / Skia graphics"]
C3["Text layout"]
end
C --> D
subgraph D["Embedder (platform code)"]
D1["Window / surface"]
D2["Input and vsync"]
D3["Plugins"]
end
D --> E["OS and GPU"]
| Layer | Written in | What it does |
|---|---|---|
| Framework | Dart | Widgets, Material/Cupertino, animation, gestures, layout and paint logic |
| Engine | C++ | Runs Dart, rasterizes graphics (Impeller or Skia), lays out text, talks to the GPU |
| Embedder | Kotlin/Java, Swift/ObjC, C++ | Creates the window, passes in touches and vsync, hosts plugins |
2. The three trees
This is the most important idea in Flutter rendering. When you write widgets, Flutter actually maintains three parallel trees.
Analogy: building a house
- Widget = the blueprint. A cheap paper description: "a wall, 3 metres, painted blue". You throw it away and draw a new one whenever something changes.
- Element = the site manager. They hold the blueprint, remember what's already built, and compare each new blueprint with the old one to decide what actually needs changing.
- RenderObject = the actual building. The real, expensive structure that has a size, a position and knows how to paint itself.
flowchart LR
subgraph W["Widget tree (blueprints)"]
W1["Container"] --> W2["Padding"] --> W3["Text"]
end
subgraph E["Element tree (managers)"]
E1["Element"] --> E2["Element"] --> E3["Element"]
end
subgraph R["RenderObject tree (buildings)"]
R2["RenderPadding"] --> R3["RenderParagraph"]
end
W1 -.-> E1
W2 -.-> E2
W3 -.-> E3
E2 -.-> R2
E3 -.-> R3
Not every widget has a render object. Container is just a composition of other widgets, so only the widgets that actually lay out or paint (like Padding and Text) create one.
| Tree | Lifetime | Job |
|---|---|---|
| Widget | Very short; rebuilt often | Immutable configuration. Just data. |
| Element | Long-lived | The link. Knows its place in the tree, holds State, decides whether to update or replace. Your BuildContext is the Element. |
| RenderObject | Long-lived | The heavy work: layout, painting, hit-testing. |
Why three trees?
Because widgets are cheap and render objects are expensive. When you call setState, Flutter rebuilds the widgets, then each element compares the new widget with the old one:
flowchart TD
A["setState called"] --> B["Widget rebuilt (new cheap object)"]
B --> C{"Same type and same key as before?"}
C -- yes --> D["Keep the Element and RenderObject. Just update their properties."]
C -- no --> E["Throw away that subtree. Create new Elements and RenderObjects."]
D --> F["Fast: e.g. only the color changes"]
E --> G["Slower: full rebuild of that part"]
Text('Hello', style: TextStyle(color: isRed ? Colors.red : Colors.blue))
When isRed changes, a new Text widget is created, but the same RenderParagraph just gets a new color and repaints. Flutter rebuilds blueprints, not buildings.
Keys: without keys, elements are matched by position. When you reorder a list, the wrong State can attach to the wrong item. A Key says "match me by identity, not by position".
3. The rendering pipeline: one frame
Every frame goes through the same stages, triggered by the screen's vsync signal.
flowchart TD
V["Vsync signal (an event on the event loop)"] --> A
subgraph UI["UI thread (your Dart code)"]
A["1. Animate: tickers update values"] --> B["2. Build: only dirty widgets rebuild"]
B --> C["3. Layout: measure and position"]
C --> D["4. Paint: record drawing commands into layers"]
D --> E["5. Composite: send the layer tree to the engine"]
end
E --> F
subgraph RT["Raster thread (C++)"]
F["6. Rasterize: Impeller / Skia turns layers into GPU pixels"]
end
F --> G["Frame appears on screen"]
-
Step 0:
setState, an animation or a scroll callsscheduleFrame(). Nothing happens immediately; Flutter waits for the next vsync. -
Animate:
AnimationControllers update their values for this frame. - Build: only widgets marked dirty rebuild, not the whole app.
- Layout: sizes and positions are calculated (next section).
- Paint: render objects record commands ("draw a rounded rect here"). No pixels yet.
- Composite: the layer tree goes to the engine, and the UI thread is free again.
- Rasterize: the raster thread turns commands into real pixels on the GPU.
4. Layout: the golden rule
Constraints go down. Sizes go up. Parent sets position.
sequenceDiagram
participant P as Parent
participant C as Child
P->>C: Constraints: width 0 to 400, height 0 to 800
Note over C: Child picks its own size within the limits
C-->>P: My size: 300 x 50
Note over P: Parent decides where to place the child
P->>P: Position child at (50, 100)
Analogy: a manager tells an employee, "Your desk can be at most 2 metres wide." The employee picks a 1.5 m desk. The manager decides where in the office it goes. The employee never chooses their own position.
This explains classic Flutter puzzles:
-
Why
Container(width: 100)sometimes fills the whole screen: its parent gave it tight constraints, so it had no choice. -
Why you get "unbounded height" errors: a
ListViewinside aColumnreceives infinite height constraints and can't pick a size.
Layout is a single pass through the tree, which is why it's fast. Relayout boundaries stop a change from spreading upward when a child's size can't affect its parent.
5. Paint and layers
Painting doesn't create pixels. It records commands into pictures, which are grouped into a layer tree.
flowchart TD
R["Root layer"] --> A["AppBar layer"]
R --> B["Body layer"]
B --> C["List item layers (RepaintBoundary)"]
B --> D["Spinner layer (RepaintBoundary)"]
D -.-> E["Spinner changes every frame: ONLY this layer repaints"]
RepaintBoundary gives a part of the UI its own layer. When something inside it changes, only that layer repaints, not the whole screen. Flutter adds these automatically in some places, such as list items and routes.
6. The threads involved
| Thread | Job |
|---|---|
| Platform | OS main thread: receives touches, runs plugins and platform views |
| UI | Runs your Dart code: event loop, build, layout, paint |
| Raster | Turns the layer tree into GPU pixels |
| IO | Decodes images and loads assets |
Flutter pipelines frames: while the raster thread draws frame 1, the UI thread is already building frame 2.
sequenceDiagram
participant UI as UI thread
participant RT as Raster thread
participant S as Screen
UI->>UI: Build, layout, paint frame 1
UI->>RT: Layer tree for frame 1
par Work in parallel
RT->>RT: Rasterize frame 1
and
UI->>UI: Build, layout, paint frame 2
end
RT->>S: Show frame 1
UI->>RT: Layer tree for frame 2
RT->>RT: Rasterize frame 2
RT->>S: Show frame 2
That gives you two places where jank can come from:
| Jank source | Cause | Fix |
|---|---|---|
| UI thread | Slow Dart code, big rebuilds, heavy layout |
const widgets, smaller widgets, isolates |
| Raster thread | Expensive scene: saveLayer, Opacity, clipping, blurs, shadows |
Simplify effects, use RepaintBoundary wisely |
Flutter DevTools' performance view shows both threads as separate bars, so you can see which one is slow.
7. Impeller vs Skia
The engine needs a graphics library to turn drawing commands into pixels.
| Skia (original) | Impeller (new default on iOS and modern Android) | |
|---|---|---|
| Built for | General 2D graphics (also used by Chrome) | Flutter specifically |
| Shaders | Compiled at runtime, the first time an effect appears | Precompiled at build time |
| Result | Possible stutter on first animations ("shader compilation jank") | Smooth from the first frame |
| GPU APIs | OpenGL, Metal, Vulkan | Metal (iOS), Vulkan (Android) |
As an app developer you rarely touch either directly. You just get smoother first frames with Impeller.
8. A tap, end to end
sequenceDiagram
participant OS as OS / Platform thread
participant UI as UI thread (Dart)
participant RT as Raster thread
participant GPU as GPU / Screen
OS->>UI: Touch event posted to event loop
UI->>UI: Hit-test render tree, find the button
UI->>UI: onTap runs, setState marks element dirty
UI->>OS: scheduleFrame (wait for vsync)
OS->>UI: Vsync event
UI->>UI: Animate, Build (dirty part only), Layout, Paint
UI->>RT: Layer tree
RT->>GPU: Rasterize and display
Note over OS,GPU: All within ~16 ms at 60 Hz
- Your finger touches the screen, and the platform thread receives it.
- The engine posts it to the UI thread as an event.
- Flutter hit-tests the render tree ("which render object is under this point?").
-
onTapruns and callssetState. The element is marked dirty and a frame is scheduled. - Vsync arrives: animate, then build, layout, paint and composite.
- The raster thread draws it on the GPU, and the updated UI appears.
9. What this means for your everyday code
| Practice | Why it helps internally |
|---|---|
const widgets |
The same instance is reused, so Flutter skips rebuilding that subtree |
| Small, split-up widgets |
setState marks a smaller subtree dirty |
ListView.builder |
Only builds and lays out the visible items |
Keys on reorderable lists |
Elements and State match the right items |
RepaintBoundary around frequently changing UI |
Only that layer repaints |
FadeTransition / AnimatedOpacity instead of animating Opacity
|
Avoids expensive saveLayer work on the raster thread |
| Heavy computation in isolates | Keeps the UI thread inside the frame budget |
Quick summary
- Flutter draws every pixel itself with its own engine.
- Three trees: Widget (blueprint), Element (manager), RenderObject (real building).
- Each frame: vsync, animate, build, layout, paint, composite, then rasterize.
- Layout rule: constraints go down, sizes go up, the parent sets the position.
- Paint records commands; the raster thread turns them into pixels.
- Jank comes from either the UI thread (your Dart code) or the raster thread (expensive drawing).
Top comments (0)