CHAPTER 05
Beginner
Widgets in Flutter
Updated: May 16, 2026
20 min read
# CHAPTER 5
Widgets in Flutter
1. Introduction
If you ask any Flutter developer to summarize the framework in four words, they will reply: "Everything is a widget." In HTML, you have tags like<div> and <p>. In Flutter, you have Widgets. A button is a widget. The text inside the button is a widget. The padding around the text is a widget. Even the entire application itself is a widget! In this chapter, we will master Widgets in Flutter. We will explore the concept of the Widget Tree, build the foundation of our app using MaterialApp and Scaffold, and place our first visual elements on the screen.
2. Learning Objectives
By the end of this chapter, you will be able to:- Explain the "Everything is a Widget" philosophy.
- Understand parent/child relationships within the Widget Tree.
-
Implement the
MaterialApproot widget.
-
Build a standard app layout using the
Scaffoldwidget.
-
Utilize basic widgets:
Text,Center, andContainer.
3. Everything is a Widget
A widget is an immutable description of part of a user interface. When you build a Flutter app, you are nesting widgets inside of other widgets to create a Widget Tree.-
Want text? Use a
Textwidget.
-
Want it centered? Wrap the
Textwidget inside aCenterwidget.
-
Want it to have a blue background? Wrap the
Centerwidget inside aContainerwidget with a blue color.
4. MaterialApp (The App Shell)
The absolute root of almost every Flutter app is theMaterialApp widget.
It provides the standard Material Design styling, handles routing (moving between screens), and sets the overall theme of the app.
dart
5. Scaffold (The Screen Blueprint)
WhileMaterialApp represents the entire application, the Scaffold represents a single screen.
A Scaffold provides a pre-built layout structure, giving you designated slots for an AppBar (top bar), a body (the middle content), and a FloatingActionButton (the circle button at the bottom right).
dart
6. Core Structural Widgets
Let's look at the basic building blocks placed inside the Scaffold'sbody:
-
Text: Displays a string of characters.
-
Center: A layout widget. It takes onechildwidget and positions it exactly in the dead center of the available space.
-
Container: The ultimate utility box. It can take achildand wrap it in background colors, borders, margins, padding, and specific widths/heights. (Similar to a<div>in web development).
7. Building a Widget Tree
Let's combine these widgets. Note the use of thechild property. A widget that can only hold one thing uses child. A widget that can hold multiple things uses children (we will cover this in Layouts).
dart
8. Visual Learning: The Widget Tree
txt
9. Common Mistakes
-
Forgetting the Scaffold: Beginners sometimes try to return a
Textwidget or aContainerdirectly as thehome:of theirMaterialApp. The text will appear, but it will be horribly unformatted, covered in red error lines, and shoved behind the phone's battery icon. Always wrap your screen content inside aScaffold!
10. Best Practices
-
Use the VS Code Refactor Menu: If you write a
Textwidget and realize it needs to be centered, DO NOT manually typeCenter(child: ...)and try to match the parentheses. Click on theTextwidget, pressCtrl + .(or Cmd + . on Mac), and select Wrap with Center. Let the IDE write the boilerplate code for you!
11. Mini Project: Build Your First Screen
Objective: Replace the default Flutter counter app with your own custom layout.-
1.
Open
lib/main.dartand delete EVERYTHING.
-
2.
Type
import 'package:flutter/material.dart';.
-
3.
Create the
void main() { runApp(MyApp()); }function.
-
4.
Below it, type
stland press Enter. This uses a VS Code snippet to generate aStatelessWidget. Name itMyApp.
-
5.
Inside
MyApp, return aMaterialApp.
-
6.
Set the
homeproperty to aScaffold.
-
7.
Give the
ScaffoldanAppBarwith the text "Welcome".
-
8.
Give the
Scaffoldabody. Make it aCenterwidget, holding aContainerwith a red background, holding aTextwidget that says "I built this!".
12. Practice Exercises
-
1.
What property of the
Scaffoldwidget allows you to place content in the top bar of the app?
- 2. What layout widget would you use to place a piece of text exactly in the middle of the screen?
13. MCQs with Answers
Question 1
In the Flutter architecture, which widget is typically used at the absolute root of the application to provide theming, routing, and standard Material Design configurations?
Question 2
You want to create a blue box that is 150 pixels wide and 150 pixels tall. Which widget is best suited for this task?
14. Interview Questions
- Q: Explain the phrase "Everything is a Widget" in Flutter. Contrast this with traditional Android XML or HTML/CSS layouts regarding properties like Padding and Center.
-
Q: What is the specific purpose of the
Scaffoldwidget? Name three structural slots it provides to a developer.
- Q: Explain the parent/child hierarchy in a Widget Tree. How does data typically flow down the tree?
15. FAQs
Q: What if I want an iOS-style app instead of an Android-style app? A: Instead of usingMaterialApp and Scaffold, you can use CupertinoApp and CupertinoPageScaffold. However, Material design looks great on iOS, and 90% of developers just use MaterialApp for both platforms, perhaps swapping out a few buttons to look like iOS toggles.
16. Summary
In Chapter 5, we unlocked the secret of Flutter: Everything is a Widget. We learned that apps are constructed by endlessly nesting widgets into a vast Widget Tree using thechild property. We wrapped our entire app in the routing shell of MaterialApp, constructed a screen layout using Scaffold (with its appBar and body), and painted our first visuals using Center, Container, and Text widgets.