Architecture and Rendering
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Understanding what happens between runApp and pixels on the screen explains most Flutter behaviour that otherwise looks
surprising: why widgets are cheap to rebuild, why layout errors mention constraints, why a long computation freezes an
animation, and why the same app looks identical on every platform. This page describes the architecture of Flutter 3.47 and
connects it to the Trailhead app built in the rest of this section. It follows
Getting Started with Flutter and prepares
Widgets and Layout.
The Three Layers
Flutter is a layered system in which each layer depends only on the one below it.
-
The framework is written in Dart. It contains the Material and Cupertino design libraries, the widgets layer (
StatelessWidget,StatefulWidget,InheritedWidget), the rendering layer (layout and painting) and low-level libraries for animation, gestures and painting. Almost everything an app developer touches lives here, and all of it is open source and readable. -
The engine is written mainly in C++. It provides the low-level rendering through Impeller, the Dart runtime, text layout, and the
dart:uiAPI that the framework calls to submit a frame. -
The embedder is platform-specific code (Kotlin and C on Android, Objective-C and Swift on iOS, C on desktop, JavaScript on the web) that hosts the engine. It creates the window or surface, delivers pointer and keyboard events, connects accessibility services and runs the platform event loop.
Because the framework is separate from the embedder, adding a platform means writing an embedder, not rewriting the widgets. This is also why Flutter can run on embedded and automotive hardware.
Why Flutter Draws Its Own Pixels
Native toolkits and React Native ask the operating system to create platform widgets: a button is a UIButton or an Android
Button. Flutter instead asks the platform only for a blank drawing surface, and paints every button, label and shadow
itself. The trade-offs are:
| Aspect | Own renderer (Flutter) | Native widgets (React Native, native) |
|---|---|---|
Visual consistency |
Identical on every platform and OS version |
Follows the OS look and updates with it |
New OS controls |
Appear only when Flutter or a package re-implements them |
Available immediately |
Custom designs and animation |
Unconstrained; the canvas belongs to the app |
Limited by the platform widget |
Bridge overhead |
None between UI code and the renderer |
A native layer between code and widgets |
Accessibility |
Reproduced through the semantics tree |
Provided by the platform widget |
App size |
Includes the engine |
Smaller runtime |
The same trade-off exists for the other approaches described in React Native and Hybrid Apps. Flutter’s bet is that owning the pixels gives predictable results and removes a class of per-platform bugs.
The Three Trees
Flutter maintains three parallel trees. Understanding their roles is the key to understanding performance.
-
Widgets are lightweight, immutable descriptions of part of the UI. They are cheap to create and are thrown away and recreated on every rebuild. Creating a
Textdoes not draw anything. -
Elements are the long-lived instances that sit between a widget and its render object. An element holds the widget it was last built from, remembers the position in the tree, and owns the
Stateobject of aStatefulWidget. When a rebuild produces a new widget, the element compares it with the old one (sameruntimeTypeandkey): if they match, the element is updated with the new configuration; otherwise it is replaced. -
Render objects (
RenderObject, usuallyRenderBox) do the real work: they compute layout, paint and hit-test. They are expensive, so Flutter keeps them and only updates their properties when an element updates.
This is why rebuilding a big build method is cheap: only small widget objects are allocated, and render objects are
reused. It is also why const widgets help, because an identical const instance lets Flutter skip the subtree
comparison altogether. A fourth, parallel tree, the semantics tree, is built for accessibility (see
Widgets and Layout).
Build, Layout and Paint
Each frame passes through phases that run in order on the UI thread.
| Phase | What happens | Triggered by |
|---|---|---|
Build |
Dirty elements call |
|
Layout |
Render objects receive constraints from their parent, size their children and position them |
A size or constraint change, |
Paint |
Render objects record drawing commands into layers |
A visual change, |
Composite |
Layers are combined into a scene and sent to the engine |
Every frame that painted |
Raster |
The engine rasterizes the scene on the GPU |
Receiving the scene |
Calling setState only marks an element dirty and schedules a frame; Flutter then rebuilds just the dirty elements, lays out
only render objects whose constraints changed, and repaints only the regions that need it through repaint boundaries. Wrap an
expensive, frequently animating subtree in a RepaintBoundary to isolate its painting from the rest.
Constraints Go Down, Sizes Go Up
Layout follows one rule, from the Flutter documentation, that explains almost every layout error:
-
A parent gives each child constraints: a minimum and maximum width and height.
-
The child picks a size inside those constraints.
-
The parent positions the child. A child never knows its own position on the screen.
// The ConstrainedBox allows at most 200 logical pixels of width, so the Text wraps.
Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 200),
child: const Text('A long trail name that wraps at two hundred logical pixels'),
),
)
Typical consequences:
-
A
ListViewinside aColumnfails with "unbounded height" (a "Vertical viewport was given unbounded height" error) because theColumnlets its children be as tall as they like and the list tries to fill that infinite height; wrap the list inExpandedso it takes the remaining space, or in aSizedBoxwith a fixed height. -
A
Rowof wide children overflows (the yellow and black stripes) because aRowpasses unbounded width constraints down to non-flexible children. UseExpanded,Flexibleor wrap text with a bounded width. -
A widget seems to ignore its size because its parent forced tight constraints;
Align,CenterorUnconstrainedBoxloosen them.
The relevant widgets are covered in Widgets and Layout. Layout is linear in the number of widgets because each render object is laid out once per frame in a single pass, with children measured by their parent.
From runApp to a Frame
runApp makes the given widget the root, inflates the element tree and schedules the first frame. After that, every frame is
driven by the platform vsync signal. If nothing is dirty, no frame is produced and the app uses no CPU.
Impeller and Skia
Flutter originally used Skia as its renderer. Skia compiled shaders on demand at run time, which caused one-time stutter (shader jank) the first time an animation ran. Impeller replaced it: it precompiles a small, fixed set of shaders at build time and uses modern graphics APIs (Metal on iOS and macOS, Vulkan on Android with an OpenGL fallback). Impeller is the default renderer on iOS and on modern Android, so most apps need no configuration. You can inspect or temporarily switch the choice with command-line flags, but they are diagnostic tools, not part of normal development; consult the Flutter documentation page on Impeller for the current flags and support matrix.
Threads and Isolates
A running Flutter app uses several threads:
| Thread | Role |
|---|---|
UI thread |
Runs your Dart code in the main isolate: build, layout, paint, event handlers. Produces the layer tree. |
Raster thread |
Takes the layer tree and rasterizes it with Impeller on the GPU. |
Platform thread |
The platform’s main thread; hosts the embedder and plugin code and delivers platform messages. |
IO thread |
Decodes images and performs other engine-level background work. |
Your Dart code normally runs on the UI thread in the main isolate. A frame has to finish within about 16 ms at 60 Hz (8 ms
at 120 Hz); a synchronous computation longer than that causes dropped frames, no matter how fast the GPU is. Asynchronous
I/O (await http.get(uri)) does not block, but CPU-heavy work does. Offload it to another isolate:
import 'dart:convert';
import 'dart:isolate';
Future<List<Trail>> decodeTrails(String body) {
// Parse a very large response without blocking the UI thread.
return Isolate.run(() {
final list = jsonDecode(body) as List<dynamic>;
return [for (final e in list) Trail.fromJson(e as Map<String, dynamic>)];
});
}
Isolates share no memory, so data is copied or transferred between them. Plugin calls are asynchronous messages to the platform
thread (see Platform Integration). The DevTools Performance view shows
the UI and raster thread timelines, and tells you which side is the bottleneck: slow UI means build or layout cost, slow raster
means expensive painting such as large blurs, clips or saveLayer calls.
Practical Consequences for Trailhead
-
Keep
buildmethods free of work: no network calls, no parsing, no sorting of large lists. Compute in a repository, a notifier (see State Management) or an isolate. -
Split large screens into small widget classes, and prefer
constconstructors, so rebuilds touch less of the tree. -
Use
ListView.builderfor the trail list so only visible rows are built, laid out and painted. -
Give list items stable keys when they can be reordered, so elements and their state follow the right data.
-
Measure in profile mode on a real device before optimizing, and look at both threads in DevTools.