Sub topic 1
Everything is a widget
In Flutter, the user interface is described with widgets. A widget is an immutable object that tells Flutter what part of the screen should look like. “Immutable” means that the object’s fields do not change after the object is created.
A widget does not usually draw pixels directly. Instead, it describes configuration such as:
- Which text to display
- Which color or padding to use
- Which child widgets to place inside it
- What should happen when a user taps it
Flutter reads these descriptions and builds the visible interface from them.
Prerequisites
This chapter assumes that you know basic Dart syntax, including classes, constructors, final fields, and named parameters. You should also understand Dart null safety and have a Flutter project that you can run.
Widgets form a tree
Flutter interfaces are nested. A page contains a layout widget, which contains other layout widgets and visible widgets. This nesting forms a widget tree.
For example, a simple screen might contain:
MaterialAppfor application-level Material Design behaviorScaffoldfor the basic page structureCenterfor positioning its childTextfor displaying words
The child and children properties connect widgets to one another. A widget with one child creates one branch. A widget with a list of children creates several branches.
The tree is a useful working model. When Flutter needs to display the app, it evaluates the descriptions from the root down through their children. A change near the root can affect a large part of the interface. A change in a small branch can affect only that branch.
A widget describes configuration
Consider this example:
import 'package:flutter/material.dart';
class Greeting extends StatelessWidget {
const Greeting({super.key, required this.name});
final String name;
@override
Widget build(BuildContext context) {
return Text('Hello, $name');
}
}Greeting is a widget with one input, name. The constructor receives that input and stores it in a final field. The build method describes the widget that Flutter should display.
Using the widget might look like this:
const Greeting(name: 'Mina')The Greeting object does not change its name. To display a different name, code creates another widget configuration, such as const Greeting(name: 'Ravi').
This separation makes UI code composable. A larger widget can combine smaller widgets without knowing how every smaller widget works internally. It only supplies their inputs and places them in the tree.
| Widget property | Example | Purpose |
|---|---|---|
| Child | `child: Text('Hi')` | Places one widget inside another |
| Children | `children: [Text('A'), Text('B')]` | Places several widgets inside another |
| Configuration | `padding: EdgeInsets.all(16)` | Controls appearance or layout |
Rebuilding the description
Flutter may call build again when inputs or relevant application state change. A rebuild creates or returns widget descriptions again. This does not mean that Flutter throws away and redraws every physical pixel. Flutter compares the new description with the existing structure and updates what needs to change.
This model explains why widgets should be small and focused. A widget should describe one meaningful part of the interface. For example, a profile screen can be composed from widgets for the avatar, name, action buttons, and biography.
Exercise
What does the widget tree represent, and why can a widget contain other widgets?
Show answer
The widget tree represents the nested structure of the interface. A widget can contain other widgets through child or children properties so larger UI parts can be composed from smaller, reusable descriptions.