Skip to main content
Flutter Basics – Complete Beginner to Advanced Guide
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 MaterialApp root widget.
  • Build a standard app layout using the Scaffold widget.
  • Utilize basic widgets: Text, Center, and Container.

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 Text widget.
  • Want it centered? Wrap the Text widget inside a Center widget.
  • Want it to have a blue background? Wrap the Center widget inside a Container widget with a blue color.

4. MaterialApp (The App Shell)

The absolute root of almost every Flutter app is the MaterialApp widget. It provides the standard Material Design styling, handles routing (moving between screens), and sets the overall theme of the app.
dart
12345678910
import &#039;package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      title: "My App",
      home: MyScreen(), // Points to the first screen to show!
    )
  );
}

5. Scaffold (The Screen Blueprint)

While MaterialApp 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
12345678910111213
class MyScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Home Screen")),
      body: Text("This is the main content!"),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
      ),
    );
  }
}

6. Core Structural Widgets

Let's look at the basic building blocks placed inside the Scaffold's body:
  • Text: Displays a string of characters.
  • Center: A layout widget. It takes one child widget and positions it exactly in the dead center of the available space.
  • Container: The ultimate utility box. It can take a child and 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 the child 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
123456789101112
Scaffold(
  body: Center(
    child: Container(
      color: Colors.blue,
      height: 100,
      width: 200,
      child: Center(
        child: Text("Hello Flutter!"),
      ),
    ),
  ),
);

8. Visual Learning: The Widget Tree

txt
12345678
[ MaterialApp ] (The App Wrapper)
      |
[ Scaffold ] (The Screen Layout)
      |-- appBar: [ AppBar ]
      |
      |-- body: [ Center ]
                  |-- child: [ Container ] (Blue Box)
                               |-- child: [ Text ] "Hello!"

9. Common Mistakes

  • Forgetting the Scaffold: Beginners sometimes try to return a Text widget or a Container directly as the home: of their MaterialApp. 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 a Scaffold!

10. Best Practices

  • Use the VS Code Refactor Menu: If you write a Text widget and realize it needs to be centered, DO NOT manually type Center(child: ...) and try to match the parentheses. Click on the Text widget, press Ctrl + . (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. 1. Open lib/main.dart and delete EVERYTHING.
  1. 2. Type import 'package:flutter/material.dart';.
  1. 3. Create the void main() { runApp(MyApp()); } function.
  1. 4. Below it, type stl and press Enter. This uses a VS Code snippet to generate a StatelessWidget. Name it MyApp.
  1. 5. Inside MyApp, return a MaterialApp.
  1. 6. Set the home property to a Scaffold.
  1. 7. Give the Scaffold an AppBar with the text "Welcome".
  1. 8. Give the Scaffold a body. Make it a Center widget, holding a Container with a red background, holding a Text widget that says "I built this!".

12. Practice Exercises

  1. 1. What property of the Scaffold widget allows you to place content in the top bar of the app?
  1. 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 Scaffold widget? 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 using MaterialApp 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 the child 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.

17. Next Chapter Recommendation

Our screen looks nice, but it is completely frozen. It cannot change or react to clicks. To make it dynamic, proceed to Chapter 6: Stateless Widgets vs Stateful Widgets.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·