π
If you're just getting started with Flutter, you'll write your first widget within minutes. But somewhere around day three, you'll hit a wall β why does context crash here? Why did my whole screen rebuild when I changed one variable? Why does Flutter feel so different from React Native or SwiftUI?
The answer is architecture. Once you understand how Flutter actually works under the hood, everything else clicks into place. This is Part 1 of the Flutter From Zero series, and we're covering the foundations.
What Is Flutter β and Why Does It Exist?
Flutter is an open-source UI toolkit from Google that lets you build natively compiled apps for six platforms from a single Dart codebase: Android, iOS, Web, Windows, macOS, and Linux.
But here's what makes it different from every other cross-platform tool:
Flutter doesn't use native UI components. It draws its own UI using a custom rendering engine.
That's a big deal. Tools like React Native translate your code into native components β so a <Button> becomes an Android Button or an iOS UIButton. Flutter skips that entirely and paints pixels directly to the screen using its own engine (Skia or the newer Impeller). The result: pixel-perfect UI that looks identical on every platform, and no JavaScript bridge slowing things down.
| Problem | Flutter's Solution |
|---|---|
| Separate iOS/Android codebases | One Dart codebase for all platforms |
| Bridge overhead (React Native) | No bridge β renders directly |
| Inconsistent UI per platform | Flutter draws its own widgets |
| Slow cross-platform performance | Compiles to native ARM machine code |
Flutter 1.0 shipped in December 2018. By Flutter 3.x, it targets six platforms stably.
The 3 Layers of Flutter
Flutter's architecture is a three-layer sandwich. Each layer depends on the one below it. You work in the top layer and rarely touch the others β but knowing what they do changes how you debug and optimize.
ββββββββββββββββββββββββββββββββββββββββββββ
β FRAMEWORK LAYER (Dart) β
β Material / Cupertino / Widgets / ... β
ββββββββββββββββββββββββββββββββββββββββββββ€
β ENGINE LAYER (C++) β
β Skia / Impeller / Dart Runtime β
ββββββββββββββββββββββββββββββββββββββββββββ€
β EMBEDDER LAYER (Platform) β
β Android / iOS / Windows / macOS ... β
ββββββββββββββββββββββββββββββββββββββββββββ
1. Framework Layer (Dart)
This is where you spend 95% of your time. It's written entirely in Dart and contains everything you interact with: widgets, routing, theming, gestures, and animations.
The sub-layers stack like this, bottom to top:
Material / Cupertino β Design-system widgets (buttons, dialogs, etc.)
Widgets β Core primitives (StatelessWidget, StatefulWidget)
Rendering β RenderObjects, layout, paint
Animation / Painting β Curves, tweens, Canvas API
Foundation β Utilities, diagnostics, change notifiers
2. Engine Layer (C++)
The engine is Flutter's heart. You never write C++, but this layer is doing the heavy lifting:
- Skia / Impeller β renders pixels to the screen
- Dart Runtime β runs your Dart code (AOT in release builds, JIT in debug)
- Platform Channels β lets Dart talk to native code
- Text Layout β handles fonts and text shaping
Quick note on Impeller: it's Flutter's newer renderer, designed to eliminate shader-compilation jank (that stutter you feel the first time a new animation runs). It's now the default on iOS and rolling out on Android.
3. Embedder Layer (Platform-specific)
The embedder is what makes Flutter runnable on a given OS. It's written in the host platform's language (Kotlin for Android, Swift for iOS, C++ for Desktop).
Its job: create a Flutter Engine instance, give it a surface to draw on, and feed it input events (touch, keyboard, mouse). Because this layer is separated out, Flutter can run on custom hardware β anyone can write a custom embedder.
The 3 Trees and the Rendering Pipeline
This is the part that trips up most beginners. Flutter manages three parallel trees to render your UI efficiently.
Your Code
β
βΌ
βββββββββββββββ
β Widget Tree β β Immutable blueprints (cheap to create)
ββββββββ¬βββββββ
β inflate
βΌ
βββββββββββββββ
β Element Treeβ β Mutable, long-lived (lifecycle manager)
ββββββββ¬βββββββ
β creates
βΌ
βββββββββββββββ
β Render Tree β β Does the actual layout and painting (expensive)
βββββββββββββββ
Widget Tree β Widgets are immutable Dart objects. Think of them as configuration blueprints. Every time build() runs, new widgets are created. This sounds wasteful, but widgets are intentionally cheap and lightweight.
Element Tree β Elements are long-lived and mutable. When Flutter first renders a widget, it creates an Element for it. That Element persists across rebuilds β it holds the connection between the widget (config) and the render object (output).
When you call
setState(), Flutter marks the Element dirty and schedules a rebuild. It does NOT recreate the Element or RenderObject from scratch unless the widget type changes.
Render Tree β RenderObjects do the real work: computing sizes, calculating positions, and painting pixels. This tree is expensive to create, so Flutter reuses RenderObjects as aggressively as possible.
The Render Pipeline
Each frame goes through five stages:
Build β Layout β Paint β Composite β Rasterize
-
Build β
build()is called on dirty widgets; new widget nodes are produced - Layout β RenderObjects compute sizes and positions using constraints
- Paint β RenderObjects draw onto a Canvas
- Composite β Layers are assembled into a scene
- Rasterize β The GPU converts the scene to pixels on screen
Flutter targets 60fps (16.6ms per frame) or 120fps on capable hardware. The three-tree model is what makes that possible β only dirty parts of the tree get rebuilt, not the entire UI.
Project Structure
Running flutter create my_app produces this layout:
my_app/
βββ lib/ β Your Dart code lives here
β βββ main.dart β Entry point
βββ android/ β Android embedder + native code
βββ ios/ β iOS embedder + native code
βββ web/ β Web embedder
βββ windows/
βββ macos/
βββ linux/
βββ assets/ β Images, fonts, JSON, etc.
βββ test/ β Unit and widget tests
βββ pubspec.yaml β Project manifest
A recommended structure for real apps inside lib/:
lib/
βββ main.dart
βββ app.dart β Root widget + routing
βββ features/ β Feature-based folders
β βββ home/
β βββ auth/
βββ shared/ β Reusable widgets, utils
βββ data/ β Models, APIs, repositories
pubspec.yaml
This is your project's manifest β it declares dependencies, assets, and fonts:
name: my_app
version: 1.0.0+1
environment:
sdk: ">=3.0.0<4.0.0"
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
flutter:
assets:
- assets/images/
fonts:
- family: Poppins
fonts:
- asset: assets/fonts/Poppins-Regular.ttf
- asset: assets/fonts/Poppins-Bold.ttf
weight: 700
After any edit to pubspec.yaml, run:
flutter pub get
App Lifecycle
Flutter exposes lifecycle events via WidgetsBindingObserver. Understanding this is critical when you work with cameras, audio, network connections, or local storage.
The States
detached β inactive β resumed β hidden / paused β detached
- resumed β app is fully active in the foreground β
- inactive β visible but not receiving input (e.g., incoming call overlay)
- hidden β not visible but not suspended (added in Flutter 3.13)
- paused β fully backgrounded / suspended
- detached β engine alive, no view attached (startup or shutdown)
Listening to Lifecycle Events
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this); // ALWAYS unregister
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
// Resume camera, refresh data
break;
case AppLifecycleState.paused:
// Save state, release resources
break;
default:
break;
}
}
}
BuildContext β The Part That Confuses Everyone
BuildContext is a handle to your widget's location in the Widget Tree. Under the hood, it's the Element itself.
@override
Widget build(BuildContext context) {
final theme = Theme.of(context); // walks UP the tree to find ThemeData
final size = MediaQuery.of(context).size;
return Container(color: theme.colorScheme.primary);
}
When you call Theme.of(context), Flutter walks up the Element Tree to find the nearest Theme ancestor. Your widget then subscribes to that theme β it rebuilds automatically when the theme changes.
The 4 Mistakes to Avoid
β Using context across an async gap
// WRONG
onPressed: () async {
await Future.delayed(Duration(seconds: 2));
Navigator.of(context).pop(); // context may be stale!
}
// CORRECT
onPressed: () async {
await Future.delayed(Duration(seconds: 2));
if (!mounted) return; // guard check first
Navigator.of(context).pop();
}
β Using context in initState()
// WRONG β widget isn't in the tree yet
@override
void initState() {
super.initState();
final theme = Theme.of(context); // CRASH
}
// CORRECT β use didChangeDependencies() instead
@override
void didChangeDependencies() {
super.didChangeDependencies();
final theme = Theme.of(context); // β
}
β Wrong context level for Scaffold
// CORRECT β use Builder to get a context below the Scaffold
return Scaffold(
body: Builder(
builder: (innerContext) {
return ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(innerContext).showSnackBar(...); // β
},
);
},
),
);
β Storing context as a field
// WRONG β stored context goes stale after rebuilds or navigation
BuildContext? savedContext;
Quick Cheat Sheet
Flutter Architecture
β
βββ 3 Layers
β βββ Framework (Dart) β what you write
β βββ Engine (C++) β renders, runs Dart
β βββ Embedder (Native) β platform window/surface
β
βββ 3 Trees
β βββ Widget Tree β immutable blueprints
β βββ Element Tree β mutable, manages lifecycle
β βββ Render Tree β layout + paint (expensive)
β
βββ Render Pipeline
β Build β Layout β Paint β Composite β Rasterize
β
βββ Lifecycle
β detached β inactive β resumed β paused/hidden
β
βββ BuildContext Rules
β
Use in build(), didChangeDependencies()
β
Check mounted after async gaps
β Never in initState() directly
β Never store long-term
What's Next?
In Part 2, we'll dive into StatelessWidget vs StatefulWidget, when to use each, and how state actually flows through a Flutter app.
If this helped you, drop a β€οΈ or leave a comment with questions β I read every one. Follow the Flutter From Zero series so you don't miss the next post.
Written for Flutter 3.x | Dart SDK 3.0+
For further actions, you may consider blocking this person and/or reporting abuse
