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.

flowchart TB subgraph Framework["Framework (Dart)"] direction TB Material["Material and Cupertino"] Widgets["Widgets"] Rendering["Rendering"] Foundation["Animation, Painting, Gestures, Foundation"] Material --> Widgets --> Rendering --> Foundation end subgraph Engine["Engine (C++)"] direction TB Impeller["Impeller renderer"] DartRuntime["Dart runtime"] Text["Text layout and services"] end subgraph Embedder["Embedder (per platform)"] direction TB Surface["Render surface and event loop"] Input["Input, accessibility, plugins"] end Framework --> Engine --> Embedder
  • 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:ui API 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.

flowchart LR subgraph WT["Widget tree (immutable configuration)"] W1["Scaffold"] --> W2["Column"] W2 --> W3["Text"] W2 --> W4["ListView"] end subgraph ET["Element tree (mutable instances)"] E1["Element: Scaffold"] --> E2["Element: Column"] E2 --> E3["Element: Text"] E2 --> E4["Element: ListView"] end subgraph RT["Render tree (layout and paint)"] R1["RenderBox"] --> R2["RenderFlex"] R2 --> R3["RenderParagraph"] R2 --> R4["RenderSliver viewport"] end W1 -. "createElement" .-> E1 E1 -. "createRenderObject" .-> R1
  • 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 Text does 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 State object of a StatefulWidget. When a rebuild produces a new widget, the element compares it with the old one (same runtimeType and key): if they match, the element is updated with the new configuration; otherwise it is replaced.

  • Render objects (RenderObject, usually RenderBox) 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 build, producing new widgets that are diffed against the element tree

setState, a notifier change, an inherited widget change

Layout

Render objects receive constraints from their parent, size their children and position them

A size or constraint change, markNeedsLayout

Paint

Render objects record drawing commands into layers

A visual change, markNeedsPaint

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:

  1. A parent gives each child constraints: a minimum and maximum width and height.

  2. The child picks a size inside those constraints.

  3. 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 ListView inside a Column fails with "unbounded height" (a "Vertical viewport was given unbounded height" error) because the Column lets its children be as tall as they like and the list tries to fill that infinite height; wrap the list in Expanded so it takes the remaining space, or in a SizedBox with a fixed height.

  • A Row of wide children overflows (the yellow and black stripes) because a Row passes unbounded width constraints down to non-flexible children. Use Expanded, Flexible or wrap text with a bounded width.

  • A widget seems to ignore its size because its parent forced tight constraints; Align, Center or UnconstrainedBox loosen 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

sequenceDiagram participant Dev as main() participant Fw as Framework participant Sched as Scheduler participant Eng as Engine participant GPU as GPU Dev->>Fw: runApp(TrailheadApp) Fw->>Fw: Inflate the element tree and create render objects Fw->>Sched: scheduleFrame Eng->>Sched: Vsync signal Sched->>Fw: Build, layout, paint Fw->>Eng: Submit scene (layer tree) Eng->>GPU: Impeller rasterizes the frame GPU-->>Eng: Pixels presented

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 build methods 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 const constructors, so rebuilds touch less of the tree.

  • Use ListView.builder for 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.