Skip to content
Back to skills

Flutter Internals

ASecurity

Use when the user asks about Flutter internals: widget/element/render object trees, BuildContext, constraints go down sizes go up, RenderBox, Impeller vs Skia, raster thread, platform views or the embedder. Explains the build-layout-paint pipeline to diagnose layout errors and jank. Not for app architecture or state management (use flutter-expertise).

  • 46 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 27, 2026
ai-agentsgoc++expressawstestingdebuggingbackendperformancedocumentation

Works with

  • cli

Security analysis

A100/100

Scanned November 27, 2026

npx -y skills add j4flmao/agent-skills --skill flutter-internals --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Flutter Internals?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Flutter Internals
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/j4flmao-flutter-internals/badge)](https://www.skillsdirectory.com/skills/j4flmao-flutter-internals)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: flutter-internals
description: >
  Use when the user asks about Flutter internals: widget/element/render object
  trees, BuildContext, constraints go down sizes go up, RenderBox, Impeller vs
  Skia, raster thread, platform views or the embedder. Explains the
  build-layout-paint pipeline to diagnose layout errors and jank. Not for app
  architecture or state management (use flutter-expertise).
version: "2.0.0"
author: "j4flmao"
license: "MIT"
last_verified: "2026-11-17"
tags: [mobile, flutter, rendering, impeller, internals]
---

# Flutter Internals

## Purpose
Flutter draws every pixel itself instead of wrapping OEM widgets. Understanding the three trees, the constraint-based layout protocol and the threads that produce a frame explains most "RenderFlex overflowed", "unbounded height" and jank problems, and tells you when a custom `RenderObject` is the right tool.

## Agent Protocol

### Trigger
"Flutter internals", "widget element render object", "BuildContext", "RenderObject", "layout constraints", "Impeller", "Skia", "raster thread", "layer tree", "Flutter engine", "platform views", "custom RenderBox".

### Input Context
- Flutter version and platform; renderer in use (Impeller/Skia)
- Error message or DevTools timeline (UI vs raster thread times)
- Widget subtree involved

### Output Artifact
Explanation tied to the pipeline stage, plus the code fix or a custom render object. No file unless requested.

### Response Format
```
Stage: {build | layout | paint | composite/raster | platform}
Cause: {mechanism}
Evidence: {error text / timeline numbers}
Fix: {code}
```

## 1. The Three Trees
| Tree | Object | Lifetime | Role |
|------|--------|----------|------|
| Widget | immutable configuration (`StatelessWidget`, `StatefulWidget`, `RenderObjectWidget`) | rebuilt often, cheap | describes UI |
| Element | `ComponentElement`, `RenderObjectElement` | long-lived | the instantiated tree; holds `State`, links widgets to render objects; `BuildContext` *is* the element |
| RenderObject | `RenderBox`, `RenderSliver` | long-lived | layout, painting, hit testing |

- On rebuild, an element compares the new widget with the old: same `runtimeType` and `key` → update in place (`canUpdate`); otherwise the subtree is unmounted and recreated (state lost).
- Keys preserve identity when children reorder (`ValueKey`), or move subtrees across the tree (`GlobalKey` — expensive, use sparingly).
- `InheritedWidget` (and everything built on it: `Theme.of`, `MediaQuery.of`, Provider, Riverpod's scope) registers dependents so only they rebuild. Use specific accessors like `MediaQuery.sizeOf(context)` to depend on one aspect.

## 2. The Frame Pipeline
```
vsync → animate (tickers) → build (dirty elements) → layout → compositing bits → paint → semantics
     → layer tree submitted → raster thread rasterises with Impeller/Skia → GPU → display
```
- UI thread (Dart root isolate) runs build/layout/paint and produces a layer tree; the raster thread turns it into GPU commands. At 60 Hz each has ~16 ms (8 ms at 120 Hz).
- DevTools: if the UI bar is high → expensive build/layout/Dart code; if raster is high → expensive layers (saveLayer, clips, shaders, big images).

## 3. Layout Protocol
"Constraints go down, sizes go up, parent sets position":
- Parent passes `BoxConstraints(minW, maxW, minH, maxH)`; child picks a size within them; parent positions the child (child does not know its position).
- Common errors:
  - *Unbounded height* — a `ListView` inside a `Column`: the column gives infinite max height. Fix: `Expanded`/`Flexible` around the list, or a fixed-height `SizedBox`.
  - *RenderFlex overflowed* — children's intrinsic sizes exceed the main axis; use `Expanded`, `Flexible`, `Wrap`, or scrolling.
  - Intrinsic sizing (`IntrinsicHeight`, `IntrinsicWidth`) runs extra layout passes — can be O(n²) in deep trees; avoid in lists.
- Relayout boundaries (a render object whose size depends only on its constraints) stop layout invalidation from propagating upward.

## 4. Painting and Compositing
- Render objects paint into layers; `RepaintBoundary` creates a separate layer so a frequently changing subtree repaints alone (and the rest is cached). Too many boundaries cost memory and compositing time.
- `saveLayer`-triggering operations (opacity on groups, some `ShaderMask`/`ColorFilter`/clip combinations) allocate offscreen buffers; prefer per-primitive alpha and `FadeTransition`.
- Text layout and image decoding are expensive; image decode happens off the UI thread but large images still cost raster/memory — decode at display size (`cacheWidth`).

## 5. Engine and Renderer
- The engine (C++) hosts the Dart VM, text layout, the renderer and platform embedders (Android, iOS, web, Windows, macOS, Linux).
- **Impeller** precompiles a small fixed set of shaders at build time, removing the runtime shader-compilation jank that Skia had. It is the default on iOS and on modern Android devices (Vulkan, with an OpenGL ES backend where needed); Flutter has been removing the Skia path on Android over recent releases — check the Impeller documentation for your version's fallback rules. Web uses CanvasKit/Skwasm (WebAssembly) builds.
- Threads: platform (main) thread, UI thread, raster thread, I/O thread (per engine instance). Since Flutter 3.29 the Dart UI work runs on the platform main thread on iOS and Android (merged threads), which is what allows synchronous native calls through FFI; heavy Dart work therefore blocks platform events too.

## 6. Platform Integration
- Platform channels: asynchronous binary messages between Dart and the host (`MethodChannel`, `BasicMessageChannel`, `EventChannel`), encoded by `StandardMessageCodec`; handlers run on the platform thread by default — offload slow native work to background threads/task queues.
- `dart:ffi` calls C synchronously on the calling Dart thread — fastest path for native libraries.
- Platform views embed native views (maps, web views, ads). They are expensive (extra composition; Android modes Hybrid Composition / Texture Layer have different trade-offs) — limit their number and avoid them inside scrolling lists where possible.

## 7. Custom Render Objects
Write a `RenderBox` when composition of existing widgets cannot express the layout efficiently (e.g. a custom chart or flow layout):
```dart
class RenderSquare extends RenderBox {
  RenderSquare(this._color);
  Color _color;
  set color(Color c) { if (c == _color) return; _color = c; markNeedsPaint(); }

  @override
  void performLayout() {
    final side = constraints.constrainDimensions(double.infinity, double.infinity).shortestSide;
    size = constraints.constrain(Size.square(side.isFinite ? side : 100));
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    context.canvas.drawRect(offset & size, Paint()..color = _color);
  }
}

class Square extends LeafRenderObjectWidget {
  const Square({super.key, required this.color});
  final Color color;
  @override
  RenderSquare createRenderObject(BuildContext context) => RenderSquare(color);
  @override
  void updateRenderObject(BuildContext context, RenderSquare r) => r.color = color;
}
```
Call `markNeedsLayout()` when layout inputs change and `markNeedsPaint()` when only appearance changes.

## 8. Anti-Patterns
- Creating widgets with new `GlobalKey`s every build (state reset, expensive reparenting).
- Using `MediaQuery.of(context)` deep in lists (rebuild on every inset/keyboard change) instead of `sizeOf`/`paddingOf`.
- Intrinsic widgets inside long lists.
- Debugging performance in debug mode (JIT, assertions) instead of profile mode.

## 9. Checklist
- [ ] Identify the pipeline stage (UI vs raster) before optimising
- [ ] Layout errors traced to the constraint that was unbounded or too tight
- [ ] Keys used deliberately for reorderable/stateful children
- [ ] `saveLayer` and platform views minimised in animated/scrolling content
- [ ] Custom render objects only where composition is insufficient, with correct invalidation

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.