CHAPTER 08
Beginner
Rows, Columns, Containers, and Padding
Updated: May 16, 2026
20 min read
# CHAPTER 8
Rows, Columns, Containers, and Padding
1. Introduction
If you look at any mobile app, the layout is simply a grid. An Instagram feed is a vertical list of posts. Each post has a horizontal list of elements (a profile picture next to a username). To build complex interfaces in Flutter, you combine these vertical and horizontal stacks. In this chapter, we will master Rows, Columns, Containers, and Padding. We will learn how to arrange multiple children, align them perfectly to the center or edges using Axis Alignment, and wrap them in beautifully styled, padded boxes.2. Learning Objectives
By the end of this chapter, you will be able to:-
Stack widgets vertically using the
Columnwidget.
-
Stack widgets horizontally using the
Rowwidget.
-
Align items using
MainAxisAlignmentandCrossAxisAlignment.
-
Apply spacing using the
Paddingwidget.
-
Style UI elements using the
Containerwidget'sBoxDecoration.
3. The Column Widget (Vertical Stacking)
A Column takes a list of widgets and stacks them vertically, from top to bottom.
-
Notice it uses
children: [](a Dart List), notchild:.
dart
4. The Row Widget (Horizontal Stacking)
A Row takes a list of widgets and places them side-by-side, from left to right.
dart
5. Alignment (Main and Cross Axes)
Rows and Columns don't just stack things; they align them. Every Row and Column has two axes:- Main Axis: The direction it flows. (For a Row, Main = Horizontal. For a Column, Main = Vertical).
- Cross Axis: The opposite direction. (For a Row, Cross = Vertical).
MainAxisAlignment properties:
-
MainAxisAlignment.start: Pushes everything to the top/left.
-
MainAxisAlignment.center: Bunches everything in the dead center.
-
MainAxisAlignment.spaceBetween: Pushes the first item to the far left, the last item to the far right, and spreads the rest evenly in the middle.
dart
6. The Padding Widget
In CSS, padding is a property. In Flutter, Padding is a Widget.
If you want text to not touch the edges of the screen, you wrap it in Padding.
dart
-
EdgeInsets.all(8): All sides.
-
EdgeInsets.symmetric(horizontal: 16, vertical: 8): Different X and Y values.
-
EdgeInsets.only(top: 20): Only add padding to the top.
7. The Container Styling (BoxDecoration)
The Container is the most versatile widget. However, if you want to add colors, rounded corners, or shadows, you must use the decoration property.
CRITICAL RULE: If a Container has a decoration, you MUST put the color: property *inside* the decoration, or Flutter will crash!
dart
8. Visual Learning: The Axis System
txt
9. Best Practices
-
Extracting Containers: A heavily styled
Containerwith shadows, padding, and text can be 20 lines of code. If you have a Row of 3 identical Containers, do not copy-paste that code 3 times. Right-click the Container, select Refactor -> Extract Widget, name itCustomCard, and use it likeCustomCard()three times!
10. Common Mistakes
-
Infinite Heights in Columns: A
Columntries to take up as much vertical space as possible. If you put aColumninside anotherColumn(or aListView), Flutter won't know how tall to make the inner Column, and it will crash. You must setmainAxisSize: MainAxisSize.minon the inner Column.
11. Mini Project: Build a User Profile Card
Objective: Combine Rows, Columns, and padded Containers to build a UI card.-
1.
Return a
Centerwidget holding aContainer.
-
2.
Give the
Containera white background, rounded corners (10px), and a drop shadow usingBoxDecoration. Set its width to 300, andpaddingto 20.
-
3.
Inside the
Container, place aRow.
-
4.
In the
Row, add anIcon(Icons.person, size: 50).
-
5.
Next to the Icon, add a
SizedBox(width: 15)for spacing.
-
6.
Next to the SizedBox, add a
Column.
-
7.
Inside the
Column, add twoTextwidgets: "John Doe" (bold, size 20) and "Software Engineer" (grey).
-
8.
Fix the Alignment: Set the
Column'scrossAxisAlignmenttoCrossAxisAlignment.startso the text aligns to the left perfectly!
12. Practice Exercises
-
1.
To spread three buttons evenly across the bottom of the screen inside a Row, which specific
MainAxisAlignmentproperty should you use?
- 2. Write the syntax to apply exactly 10 pixels of padding to the left and right sides of a widget, but 0 to the top and bottom.
13. MCQs with Answers
Question 1
A developer adds a color: Colors.red property to a Container, and then adds a decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)) to the same Container. What happens when they run the app?
Question 2
In a Column widget, what direction does the MainAxisAlignment control?
14. Interview Questions
-
Q: Contrast
MainAxisAlignmentandCrossAxisAlignment. How do their directions change depending on whether they are applied to aRowor aColumn?
-
Q: Explain the mechanical difference between
PaddingandMarginwhen applied to aContainerwith a colored background.
-
Q: Why is the
Paddingwidget preferred in Flutter over relying on standard CSS-style padding attributes inside every UI element?