Skip to main content
Flutter Basics – Complete Beginner to Advanced Guide
CHAPTER 07 Beginner

Layouts in Flutter

Updated: May 16, 2026
20 min read

# CHAPTER 7

Layouts in Flutter

1. Introduction

If "Everything is a Widget," how do we arrange them? Unlike HTML/CSS, where elements naturally stack on top of each other in a document flow, Flutter relies on a strict mathematical layout engine based on Constraints. If you try to force a box to be 500 pixels wide on a 300-pixel phone screen, Flutter will throw a massive yellow-and-black caution tape error across your screen! In this chapter, we will master Layouts in Flutter. We will understand the golden rule of Flutter layouts, learn how to explicitly size objects using SizedBox, and explore dynamic spacing using Expanded and Flexible.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain the golden rule of Flutter layouts: "Constraints go down, Sizes go up."
  • Force specific dimensions using the SizedBox widget.
  • Understand the difference between Expanded and Flexible.
  • Solve common layout overflow errors.

3. The Golden Rule of Layouts

To master Flutter UI, you must memorize this sentence: "Constraints go down. Sizes go up. Parent sets position."
  1. 1. Constraints go down: The Parent widget tells its child, *"You can be anywhere between 0 and 300 pixels wide."*
  1. 2. Sizes go up: The Child widget decides its own size within those rules and tells the Parent, *"I want to be exactly 150 pixels wide."*
  1. 3. Parent sets position: The Parent widget then places the 150-pixel child wherever it wants (e.g., in the Center, or aligned to the Top-Left).

4. The SizedBox Widget

If you want an invisible box of an exact size, you use a SizedBox.
  • Use Case 1 (Spacing): Putting an empty SizedBox(height: 20) between two pieces of text to create a 20-pixel gap.
  • Use Case 2 (Forcing Size): Wrapping a generic button inside a SizedBox(width: 200) to force the button to stretch to exactly 200 pixels wide.
dart
123456
// Forcing a container to be exactly 100x100
SizedBox(
  width: 100,
  height: 100,
  child: Container(color: Colors.red),
)

5. Expanded Widget (Taking Remaining Space)

When building a row of items, you often want one item to stretch and fill all the remaining empty space on the screen. You do this by wrapping the widget in Expanded.
  • If you have a Row with a 50px Icon on the left, and a 50px Icon on the right, and you put a Text widget wrapped in Expanded in the middle, the text will stretch like a rubber band to push the icons to the far edges of the screen!
dart
123456789
Row(
  children: [
    Icon(Icons.home),
    Expanded( // This text box will stretch to fill the screen width!
      child: Container(color: Colors.blue, child: Text("Fills the space!")),
    ),
    Icon(Icons.settings),
  ],
)

6. Flexible Widget

Flexible is the less aggressive cousin of Expanded.
  • Expanded forces its child to stretch and fill 100% of the available space, even if the child doesn't need it.
  • Flexible tells its child: *"You can take up to the remaining space if you need it, but if you are small, you don't have to stretch."*

7. Resolving the "Yellow Caution Tape" (Overflow)

The most common error in Flutter is the RenderFlex Overflow. It looks like yellow and black hazard tape on the edge of the screen.
  • Why it happens: You placed a 500px wide box inside a Row on a phone that is only 400px wide. The parent cannot contain the child.
  • The Fix: Wrap the massive child inside an Expanded widget, or wrap the entire screen inside a SingleChildScrollView to allow the user to scroll!

8. Visual Learning: Expanded vs SizedBox

txt
1234567
[ Screen Width: 400px ]

[ SizedBox(100) ] [     Expanded (300px)     ]
+---------------+----------------------------+
|      [A]      |             [B]            |
+---------------+----------------------------+
(A asks for 100. B is Expanded, so it calculates 400 - 100, and takes 300!)

9. Best Practices

  • Avoid Hardcoding Sizes: Avoid writing width: 350. If the user opens your app on an iPhone SE (which is narrow), your app will crash with an Overflow error. Use percentages (via MediaQuery), or use Expanded to let Flutter dynamically calculate the math for you across all screen sizes.

10. Common Mistakes

  • Expanded outside Row/Column: The Expanded and Flexible widgets CANNOT be used just anywhere. They MUST be direct children of a Row, Column, or Flex widget. If you wrap a Center widget's child in Expanded, Flutter will throw an error and crash.

11. Mini Project: The Search Bar Layout

Objective: Build a common UI layout using SizedBox and Expanded.
  1. 1. Create a Scaffold with an AppBar.
  1. 2. In the body, create a Row.
  1. 3. The first child is an Icon(Icons.search).
  1. 4. The second child is a SizedBox(width: 10) to create a small invisible gap.
  1. 5. The third child is an Expanded widget. Give it a child Container(color: Colors.grey) holding a Text("Search here...").
  1. 6. The fourth child is another SizedBox(width: 10).
  1. 7. The fifth child is an Icon(Icons.filter_list).
  1. 8. Press play. Notice how the grey search bar magically resizes to perfectly fill the space between the two icons, creating a professional header layout!

12. Practice Exercises

  1. 1. What widget is specifically designed to create empty whitespace between two UI elements?
  1. 2. If you want a button to stretch to the absolute edges of a phone screen, regardless of the phone's physical width, which layout widget should wrap the button?

13. MCQs with Answers

Question 1

What is the fundamental golden rule of the Flutter layout engine?

Question 2

You place a line of text in a Row, but the text is too long for the screen. Flutter throws a "RenderFlex overflowed by 45 pixels" error (yellow/black tape). What is the easiest way to fix this without changing the text size?

14. Interview Questions

  • Q: Recite the golden rule of Flutter layouts. Explain what it means when we say "Constraints go down."
  • Q: Contrast the behavior of the Expanded widget with the Flexible widget. Provide a layout scenario where Flexible is the strictly correct choice.
  • Q: A developer uses a Container with height: 20 to create spacing between two text fields. Why might a Lead Developer request they change it to a SizedBox during a code review?

15. FAQs

Q: How do I align a widget to the bottom right corner? A: You can wrap the widget in the Align widget, and set the alignment property to Alignment.bottomRight!

16. Summary

In Chapter 7, we conquered the mathematical layout engine of Flutter. We learned the golden rule: Constraints go down, Sizes go up, Parent sets position. We utilized the lightweight SizedBox to force dimensions and create invisible gaps. We mastered dynamic layouts using Expanded to stretch elements across remaining space, preventing the dreaded Yellow Caution Tape Overflow errors.

17. Next Chapter Recommendation

We know how to stretch widgets, but how do we stack them vertically and horizontally? Proceed to Chapter 8: Rows, Columns, Containers, and Padding.

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: ·