Skip to content

Beginner · Flutter setup and your first app · topic 15 of 50 · 35 min

Everything is a widget

Explanation

In Flutter, everything on screen is a widget: text, buttons, padding, layouts and the whole app. Widgets are nested into a tree. Each widget's build method returns the widgets it is made of, and BuildContext is its location in the tree, used to look things up like Theme.of(context). MaterialApp sets up the app, and Scaffold gives a screen its standard structure with an AppBar and body.

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

output
MaterialApp
└── Scaffold
    ├── AppBar
    │   └── Text('Chai Shop')
    └── Center
        └── Column
            ├── Text('Masala chai')
            ├── Text('Rs 15')
            └── ElevatedButton
                └── Text('Order')
Widgets contain other widgets (children), forming a tree. Your screen is the tree drawn on the display.
dart
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 code mirrors the tree. child holds one widget; children holds a list.

The key widgets so far

WidgetRole
MaterialAppRoot of a Material Design app: theme, title, navigation, localisation
ScaffoldStandard screen layout: appBar, body, floatingActionButton, bottomNavigationBar, drawer
AppBarThe bar at the top with a title and actions
TextShows text with a style
CenterCentres its child
Column / RowArrange children vertically / horizontally (module 5)
ElevatedButtonA Material button; onPressed: null disables it
IconA 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.

Think of it like this: Building a Flutter screen is like building with LEGO. Each brick (widget) is simple; you snap bricks into bigger pieces (your own widgets), and pieces into the whole model (the app). To change the model, you swap bricks rather than carving the whole thing again.

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

Everything is a widget

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.

dart
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),
      ),
    );
  }
}
VoidCallback is a function that takes nothing and returns nothing, like () {}.

Practice (0/3 done)

Task 1 · Question

Which widget gives a screen a standard layout with an app bar and body?

Task 2 · Question

Why must build methods be fast and free of network calls?

Task 3 · Practice

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

Question 1

1.What is a widget?

Question 2

2.What does BuildContext represent?

Question 3

3.Which property holds a list of widgets in a Column?

Question 4

4.How do you disable an ElevatedButton?

Read more (free)

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

Answer: The element

Elements are long-lived.

From Interview questions: Flutter

Which widget controls whether the back button can leave a screen in current Flutter?Related

Answer: PopScope

PopScope replaced WillPopScope.

From Navigator basics

Which API rebuilds a widget only when one value of the model changes?Related

Answer: context.select

select listens to a part.

From Provider and ChangeNotifier

Which function defines a widget test?Related

Answer: testWidgets

testWidgets provides a WidgetTester.

From Widget tests

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. 1 more topic in “Flutter setup and your first app” before its exam: Stateless and stateful widgets.
  2. Module 4 exam: Flutter setup and your first app · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Layouts. 50 topics left in the course before the final exam.

← Previous: Project structure and hot reload