Learn/Orientation/Understanding Widgets

Understanding Widgets

Learn the core concept that makes Flutter tick. Everything is a widget.

7 min~8 min read+35 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. Examples of Widgets
  2. 2. The Widget Tree
  3. 3. Two Types of Widgets
  4. 4. StatelessWidget: Does Not Change
  5. 5. StatefulWidget: Can Change Over Time
  6. 6. The build() Method
  7. 7. Widget Properties
  8. 8. Quick Reference: Common Widgets
  9. 9. Widget Thinking Workflow
  10. 10. This Lesson In Simple Words
  11. 11. Your level focus
  12. 12. One practical habit
  13. 13. Deep Understanding Guide
  14. 14. Why this lesson matters
  15. 15. Study flow for better retention
  16. 16. Quick Glossary

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Understanding Widgets

The most important concept in Flutter is that everything is a widget.

A widget is a building block of your user interface. Think of widgets like Lego bricks. You combine small, simple widgets together to create complex and beautiful UIs. Every element you see on screen, from a piece of text to a button to an entire page layout, is a widget.

Examples of Widgets

UI ElementWidget NameWhat It Does
TextTextShows text on screen
ButtonElevatedButtonA clickable button
ImageImageDisplays an image
Text fieldTextFieldAccepts user input
LayoutColumn, RowArranges children vertically or horizontally
SpacingSizedBox, PaddingAdds space around or between widgets
Screen structureScaffoldProvides the basic app skeleton

The Mental Model

💡 The Mental Model

If you have used HTML before, you know about elements like div, p, and button. In Flutter, everything serves a similar purpose but they are all called widgets. Even padding, alignment, and gesture detection are implemented as widgets.

The Widget Tree

Your app UI is organized as a tree of widgets. Each widget can contain other widgets called children, forming a hierarchy. Here is a simple example:

MaterialApp
  └── Scaffold
        ├── AppBar
        │     └── Text("My App")
        └── body: Center
              └── Column
                    ├── Icon(Icons.star)
                    ├── SizedBox(height: 10)
                    └── Text("Hello!")

This tree structure is fundamental to how Flutter works. Flutter reads this tree and draws the corresponding UI on screen. Whenever something changes, Flutter rebuilds the affected part of the tree and updates the display.

Two Types of Widgets

Every widget in Flutter is either a StatelessWidget or a StatefulWidget. Understanding the difference between them is important.

StatelessWidget: Does Not Change

Use a StatelessWidget when the UI does not need to update after it is built. For example, a simple text label or a static card.

dart
1// StatelessWidget: Shows static content
2class GreetingCard extends StatelessWidget {
3 final String name; // Data passed in (won't change)
4
5 const GreetingCard({required this.name});
6
7 @override
8 Widget build(BuildContext context) {
9 // build() returns the UI
10 // Called once, never changes
11 return Text('Hello, $name!');
12 }
13}

StatefulWidget: Can Change Over Time

Use a StatefulWidget when the UI needs to update based on user interaction or data changes. For example, a like button, a counter, a form input, or an animation.

dart
1// StatefulWidget: UI can change
2class LikeButton extends StatefulWidget {
3 @override
4 State<LikeButton> createState() => _LikeButtonState();
5}
6
7class _LikeButtonState extends State<LikeButton> {
8 bool isLiked = false; // This is the state. It can change over time.
9
10 @override
11 Widget build(BuildContext context) {
12 return IconButton(
13 icon: Icon(
14 isLiked ? Icons.favorite : Icons.favorite_border,
15 color: isLiked ? Colors.red : Colors.grey,
16 ),
17 onPressed: () {
18 setState(() { // setState tells Flutter to rebuild the UI
19 isLiked = !isLiked; // Toggle the state
20 });
21 },
22 );
23 }
24}

When to Use Which?

⚠️ When to Use Which?

Start with StatelessWidget for most of your widgets. Only switch to StatefulWidget when you need the UI to change based on user interaction or data updates. We will cover this topic in much more detail later in the course.

The build() Method

Every widget must have a build() method that returns another widget. This method:

  • Describes what the UI looks like
  • Is called by Flutter whenever the UI needs to update
  • Should be fast, so avoid putting heavy logic or network calls here
@override
Widget build(BuildContext context) {
  return Text('I am a widget!');  // Must return a widget
}

Widget Properties

Widgets are configured using their constructor parameters. You pass in values when creating a widget to control how it looks and behaves.

dart
1// Text widget with styling
2Text(
3 'Hello World', // Required: the text to show
4 style: TextStyle( // Optional: styling
5 fontSize: 24,
6 fontWeight: FontWeight.bold,
7 color: Colors.blue,
8 ),
9 textAlign: TextAlign.center, // Optional: alignment
10)
11
12// Container with decoration
13Container(
14 width: 200, // Width in logical pixels
15 height: 100, // Height in logical pixels
16 padding: EdgeInsets.all(16),
17 decoration: BoxDecoration(
18 color: Colors.white,
19 borderRadius: BorderRadius.circular(12),
20 boxShadow: [BoxShadow(blurRadius: 5, color: Colors.black12)],
21 ),
22 child: Text('Inside a container'),
23)

Quick Reference: Common Widgets

CategoryWidgets
TextText, RichText, SelectableText
ButtonsElevatedButton, TextButton, IconButton
InputTextField, Checkbox, Switch, Slider
LayoutColumn, Row, Stack, Wrap
ContainersContainer, Card, SizedBox, Padding
ListsListView, GridView, ListTile
NavigationAppBar, BottomNavigationBar, Drawer
MediaImage, Icon, CircleAvatar

You do not need to memorize all of these right now. As you build more apps, you will naturally learn which widgets to use for different situations. We will cover the most important ones in detail throughout this course.

Widget Thinking Workflow

When building any screen, think in this order:

  1. Screen frame: Scaffold, AppBar, body
  2. Main layout direction: Column, Row, or Stack
  3. Content widgets: Text, Image, Icon, ListTile
  4. Spacing and alignment: Padding, SizedBox, Align
  5. Styling and polish: Container decoration, color, typography

This sequence helps you avoid random trial-and-error and gives you a repeatable system for building UIs.

Widget Practice Reps

  • Build the same card UI in two ways: nested Containers vs cleaner extracted widgets.
  • Take one screen and identify every widget role (layout, content, spacing, interaction).
  • Convert a StatefulWidget to StatelessWidget by lifting state up.
  • Recreate a simple app screen from memory using only core widgets.

Pro Beginner Move

💡 Pro Beginner Move

Name small reusable widgets early (for example ProfileHeader, StatRow). This builds better architecture habits from day one.

This Lesson In Simple Words

Learn the core concept that makes Flutter tick. Everything is a widget. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.

Your level focus

Build confidence with basics first; speed comes later.

One practical habit

Sketch the widget tree first, then code from top to bottom.

Deep Understanding Guide

Why this lesson matters

Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.

Study flow for better retention

  1. Read one section, run one tiny example, then explain the idea in your own words.
  2. When a UI bug appears, inspect constraints and parent-child relations before changing random values.
  3. If you can teach it in 45 seconds without notes, you understood it.

Common Mistakes To Avoid

  • Nesting too many widgets without extracting reusable pieces.
  • Ignoring constraints and guessing with random SizedBox heights.
  • Using one giant build method for unrelated UI sections.
  • Putting heavy calculations directly in build methods.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one card layout with Row/Column and rebuild it using Stack for comparison.
  • Split a large widget into 3 smaller widgets with clear responsibilities.
  • Use const constructors where possible and observe rebuild behavior.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "What is the most important concept in Flutter?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.
  • state: Data that can change and update what the user sees.
  • build: The method that describes UI for the current state.
  • bloc: An event-to-state pattern that separates logic from UI.
  • animation: Controlled change in UI properties over time.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Understanding Widgets" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Nesting too many widgets without extracting reusable pieces.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

What is the most important concept in Flutter?

In Flutter, everything you see on screen is a widget: text, buttons, padding, layouts, even the entire app itself.

When should you use StatefulWidget instead of StatelessWidget?

StatefulWidget is used when the widget's appearance can change over time: like toggles, counters, or form inputs.

What method must every widget implement?

The build() method describes the widget's UI and is called by Flutter's framework whenever the widget needs to be rendered.

What does setState() do in a StatefulWidget?

setState() notifies Flutter that the internal state has changed, triggering a rebuild of the widget with the new data.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue