Beginner · Flutter setup and your first app · topic 15 of 50 · 35 min
Everything is a widget
Explanation
What is a widget?
A widget is a description of part of the user interface. Text('Hello') describes some text; Padding describes space around its child; Column describes children arranged vertically; MaterialApp describes the whole app. Widgets are cheap, immutable objects (their fields are final). When something changes, Flutter builds new widget objects and efficiently updates only what changed on screen.
The widget tree
MaterialApp
└── Scaffold
├── AppBar
│ └── Text('Chai Shop')
└── Center
└── Column
├── Text('Masala chai')
├── Text('Rs 15')
└── ElevatedButton
└── Text('Order')import 'package:flutter/material.dart';
void main() => runApp(const ChaiApp());
class ChaiApp extends StatelessWidget {
const ChaiApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Chai Shop',
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.brown)),
home: Scaffold(
appBar: AppBar(title: const Text('Chai Shop')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Masala chai'),
const Text('Rs 15', style: TextStyle(fontSize: 22)),
ElevatedButton(onPressed: () {}, child: const Text('Order')),
],
),
),
),
);
}
}The key widgets so far
| Widget | Role |
|---|---|
| MaterialApp | Root of a Material Design app: theme, title, navigation, localisation |
| Scaffold | Standard screen layout: appBar, body, floatingActionButton, bottomNavigationBar, drawer |
| AppBar | The bar at the top with a title and actions |
| Text | Shows text with a style |
| Center | Centres its child |
| Column / Row | Arrange children vertically / horizontally (module 5) |
| ElevatedButton | A Material button; onPressed: null disables it |
| Icon | A Material icon, such as Icon(Icons.local_cafe) |
The build method
Every widget class has a build method that returns the widgets it is made of. Flutter calls build when the widget first appears and again whenever it needs to rebuild, possibly many times per second during animations. So build must be fast and should only describe the UI: no network calls, no heavy calculations, no changing of other variables.
BuildContext
BuildContext is a handle to a widget's location in the tree. It lets a widget look up things provided above it: Theme.of(context) finds the app theme, MediaQuery.sizeOf(context) the screen size, Navigator.of(context) the navigator. You receive it as the argument to build. A context only sees what is above it: inside ChaiApp.build above, context sits above MaterialApp, so Theme.of(context) there would not find the app theme. Use it inside widgets that are placed below MaterialApp, such as your screens.
Composition: small widgets
Instead of one giant build method, split the UI into small widget classes: MenuItemCard, PriceTag, OrderButton. Small widgets are easier to read, reuse and test, and Flutter can rebuild them independently. Mark them const when possible.
Common mistakes
- Writing everything in one huge build method. Extract widgets.
- Doing slow work in build: it can run many times; janky UI.
- Forgetting const on widgets that never change, losing a free optimisation (the analyser suggests it).
- Using a BuildContext from a widget that is no longer on screen, for example after an await, without checking mounted (next topic).
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Everything is a widget · 1/6
In Flutter, everything on screen is a widget: text, padding, layouts, even the whole app.
Prefer a video by a teacher? Find free videos on “flutter widgets explained widget tree build method buildcontext” on YouTube ↗
In real life: A reusable menu item card
A café app shows dozens of menu items. Instead of repeating Text and Row widgets everywhere, the developer creates a MenuItemCard widget with name, price and an onAdd callback. The home screen, the search results and the "favourites" screen all use the same card, and a design change is made in one place.
class MenuItemCard extends StatelessWidget {
const MenuItemCard({super.key, required this.name, required this.price, required this.onAdd});
final String name;
final int price;
final VoidCallback onAdd;
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: const Icon(Icons.local_cafe),
title: Text(name),
subtitle: Text('Rs $price'),
trailing: IconButton(icon: const Icon(Icons.add), onPressed: onAdd),
),
);
}
}Practice (0/3 done)
Which widget gives a screen a standard layout with an app bar and body?
Why must build methods be fast and free of network calls?
Build a "business card" app: a Scaffold with an AppBar showing your name, and a centred Column with an Icon, your name in a large text style from Theme.of(context), your role, your city and an ElevatedButton "Call me" that prints to the console. Extract the contact lines into a small ContactLine widget.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Stateless and stateful widgetsFlutter setup and your first app · same module
- Installing FlutterFlutter setup and your first app · same module
- Interview questions: FlutterFlutter careers and interviews
- Navigator basicsLists, navigation and forms
- Provider and ChangeNotifierState management
- Widget testsResponsive design and testing
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is a widget?
Answer: An immutable description of part of the UI
Flutter rebuilds the screen from widgets.
What does BuildContext represent?
Answer: A widget's location in the widget tree
It is used for lookups like Theme.of(context).
Which property holds a list of widgets in a Column?
Answer: children
Column has children.
How do you disable an ElevatedButton?
Answer: onPressed: null
A null onPressed disables the button.
Which object holds a StatefulWidget's State between rebuilds?Related
Which widget controls whether the back button can leave a screen in current Flutter?Related
Which API rebuilds a widget only when one value of the model changes?Related
Which function defines a widget test?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Flutter setup and your first app” before its exam: Stateless and stateful widgets.
- Module 4 exam: Flutter setup and your first app · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Layouts. 50 topics left in the course before the final exam.