Flutter Widgets
In Flutter, everything you see is a widget: a piece of text, an icon, a button, some padding, a row of items, even the whole app. A widget is an immutable description of part of the user interface. You build screens by nesting small widgets inside bigger ones.
The widget tree
Widgets form a tree. Each widget has at most one parent and can have one child or a list of children.
MaterialApp
└── Scaffold
├── AppBar
│ └── Text("Shop")
└── Center
└── Column
├── Icon(shopping_cart)
└── Text("Your cart is empty")Here is the same tree as code:
import 'package:flutter/material.dart';
void main() => runApp(const ShopApp());
class ShopApp extends StatelessWidget {
const ShopApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Shop')),
body: const Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.shopping_cart, size: 64, color: Colors.grey),
Text('Your cart is empty'),
],
),
),
),
);
}
}Notice how the code's indentation mirrors the tree. Reading Flutter code becomes much easier once you see it as a tree.
Kinds of widgets
Widgets fall into a few broad families:
| Family | Examples | Job |
|---|---|---|
| Structural | MaterialApp, Scaffold, AppBar |
App and screen skeleton |
| Visible | Text, Icon, Image, ElevatedButton |
Draw something |
| Layout | Row, Column, Stack, Center, Padding |
Size and position children |
| Interactive | GestureDetector, TextField, Checkbox |
React to input |
| Stateful wrappers | StatefulWidget subclasses |
Hold data that changes over time |
Layout widgets are invisible on their own — Padding only adds space, Center only positions its child. Flutter prefers many small single-purpose widgets over a few big widgets with dozens of options.
Stateless vs stateful
Every custom widget you write extends one of two classes:
StatelessWidget— depends only on its constructor arguments. It never changes by itself. Examples: a label, a profile header.StatefulWidget— owns aStateobject that can change, e.g. a counter, a checkbox or a form. CallingsetState()tells Flutter to rebuild it.
Both have a build(BuildContext context) method that returns the widget's subtree. The next two lessons cover each kind in detail.
Composition: building your own widgets
Instead of writing one huge build method, extract repeated pieces into your own widgets. Here a ColorChip widget is reused three times with different arguments:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: PalettePage()));
class PalettePage extends StatelessWidget {
const PalettePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Palette')),
body: const Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ColorChip(label: 'Red', color: Colors.red),
ColorChip(label: 'Green', color: Colors.green),
ColorChip(label: 'Blue', color: Colors.blue),
],
),
),
);
}
}
class ColorChip extends StatelessWidget {
const ColorChip({super.key, required this.label, required this.color});
final String label;
final Color color;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(bottom: 8),
padding: const EdgeInsets.all(12),
color: color,
child: Text(label, style: const TextStyle(color: Colors.white)),
);
}
}Why const?
Many widgets above are created with const. A const widget is built once at compile time and reused, so Flutter can skip rebuilding it. The rule of thumb: if every argument is a compile-time constant, add const. The flutter_lints package even warns you when you forget.
Tip: Writing
conston an outer widget (likeconst Center(child: Column(...))) makes everything inside it constant too, so you don't have to repeat the keyword.
How Flutter renders
Behind the scenes Flutter keeps three trees: your widgets (cheap, immutable configuration), elements (the live instances mounted in the tree) and render objects (which do layout and painting). When you rebuild, Flutter compares the new widgets with the old ones and only updates what changed. That's why creating new widget objects on every build is perfectly fine.
Test yourself
Which statement about Flutter widgets is true?
- Only visible elements are widgets
- Widgets are mutable objects you change in place
- Layouts, padding and whole screens are all widgets
- Each screen can contain only one widget
Which widget type should you use for a UI piece whose data changes when the user taps it?
- StatelessWidget
- StatefulWidget
- InheritedWidget
- const widget
Why add const to widget constructors when possible?
- It makes the widget invisible
- It lets Flutter reuse the instance and skip rebuilding it
- It is required by the compiler
- It converts the widget to a StatefulWidget