Sub topic 7
Adding space with SizedBox and Padding
This chapter assumes you can read basic Dart, write a StatelessWidget, and run a Flutter app. It also assumes you know the rule from earlier chapters that constraints go down and sizes go up. Spacing follows from that rule, because both SizedBox and Padding are ordinary widgets that change constraints or sizes.
Space is a widget, not a property
Flutter has no CSS-style margin or gap that you set on any widget. To add space, you place a widget in the tree. Two widgets cover most cases:
SizedBoxtakes up a fixed amount of room, or forces a child to a given size.Paddingwraps one child and surrounds it with empty room.
Choose by asking one question. If the space sits between siblings, use SizedBox. If it sits around one widget, use Padding.
SizedBox as a gap
A SizedBox with no child is an invisible box of the size you give it. Put one between siblings to separate them.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: GapDemo())));
class GapDemo extends StatelessWidget {
const GapDemo({super.key});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text('Title'),
SizedBox(height: 8),
Text('Subtitle'),
SizedBox(height: 24),
Row(
children: [
Icon(Icons.star),
SizedBox(width: 8),
Text('4.8'),
],
),
],
);
}
}The gap runs along the main axis of the parent. In a Column, use height. In a Row, use width. If you pick the wrong one, nothing visibly breaks and nothing happens. A SizedBox(height: 8) inside a Row is 8 pixels tall and 0 wide, so the siblings stay touching.
// No visible gap: the box has height 8 but width 0.
Row(
children: const [
Text('A'),
SizedBox(height: 8),
Text('B'),
],
)Prefer const
A gap never changes, so write const SizedBox(width: 8). Flutter can then reuse a single instance and skip rebuilding it.
The spacing parameter on Row and Column
Recent Flutter versions (3.27 and later) add a spacing parameter to Row and Column. It inserts a gap between each pair of children. It adds nothing before the first child or after the last.
Column(
spacing: 12,
children: const [
Text('One'),
Text('Two'),
Text('Three'),
],
)This replaces the repeated SizedBox lines. If your project targets an older SDK, spacing will not compile, so check your environment constraint in pubspec.yaml. SizedBox gaps also remain the better choice when the gaps differ in size.
SizedBox as a size constraint
When you give a SizedBox a child, it stops acting as a gap. It tells the child exactly how big to be. Internally it passes tight constraints, where minimum equals maximum, to its child.
const SizedBox(
width: 120,
height: 48,
child: ElevatedButton(onPressed: null, child: Text('Save')),
)The button now measures 120 by 48. This works only if the SizedBox itself is allowed to be that size. A parent with tight constraints overrides it, because constraints go down and the parent has the final say. Several named constructors cover common cases:
| Constructor | Result |
|---|---|
| `SizedBox(width: w, height: h)` | Fixed size, or a gap if there is no child |
| `SizedBox.square(dimension: d)` | Width and height both equal `d` |
| `SizedBox.expand()` | Takes the largest size the parent allows |
| `SizedBox.shrink()` | Zero by zero, useful as an empty placeholder |
| `SizedBox(width: double.infinity)` | Fills the available width |
The double.infinity form is a common way to stretch a Column child to full width. It works only when the parent bounds that axis. In an unbounded axis, such as inside a horizontal scroll view, it throws a layout error.
Padding
Padding takes an EdgeInsets value and one child.
const Padding(
padding: EdgeInsets.all(16),
child: Text('Inside a 16-pixel frame'),
)EdgeInsets has several constructors:
EdgeInsets.all(16)applies the same inset on all four sides.EdgeInsets.symmetric(horizontal: 16, vertical: 8)sets left and right together, and top and bottom together.EdgeInsets.only(left: 8, top: 4)sets only the sides you name. The rest are zero.EdgeInsets.fromLTRB(8, 4, 8, 12)sets left, top, right, bottom in that order.
For text that must work in right-to-left languages, use EdgeInsetsDirectional.only(start: 16). The word start means left in English and right in Arabic, so the padding follows reading direction.
What Padding does to constraints
Padding follows the same layout rule as every other widget. It subtracts the insets from the constraints it received, passes the smaller constraints to its child, then reports a size equal to the child's size plus the insets.
Two consequences follow:
- A child inside
Paddinghas less room than its parent offered. ATextthat fit before may now wrap or overflow. - If the
Paddingis under tight constraints, for example insideSizedBox(width: 100), thePaddingis forced to 100 wide and the child gets 100 minus the insets.
const SizedBox(
width: 100,
child: Padding(
padding: EdgeInsets.all(10),
child: Placeholder(fallbackHeight: 40), // child is 80 wide
),
)Padding versus Container padding and margin
A Container has both padding and margin. Which one you use decides whether the empty space is painted.
Column(
children: [
// Padding inside the colored area: the blue fills the padding too.
Container(
color: Colors.blue,
padding: const EdgeInsets.all(16),
child: const Text('Inner'),
),
// Margin outside the colored area: the blue hugs the text.
Container(
color: Colors.blue,
margin: const EdgeInsets.all(16),
child: const Text('Outer'),
),
],
)Padding wrapped around a colored widget behaves like margin, and Padding placed inside it behaves like Container.padding. If you need no color, decoration, or size, use Padding directly. It is a smaller widget than Container, and it states what you mean.
Choosing between them
| Situation | Use |
|---|---|
| Fixed gap between two siblings | `SizedBox` with `width` or `height` |
| Same gap between every sibling | `spacing` on `Row` or `Column` |
| Empty frame around one widget | `Padding` |
| Force a child to an exact size | `SizedBox` with a child |
| Space that should also show a background color | `Container.padding` |
Common mistakes
- Wrong axis. A height gap in a
Rowhas no effect. Match the gap dimension to the parent's main axis. - Padding and overflow. Adding padding can push a row over its limit. If you see a RenderFlex overflow after adding padding, the child is now getting fewer pixels than it needs.
- Stacking insets by accident. Nested
Paddingwidgets add up. Three levels ofEdgeInsets.all(8)give 24 pixels, which is easy to forget when the layout looks too loose. - Hard-coded numbers everywhere. A handful of named constants, such as 8, 16, and 24, keeps a screen consistent.
Exercise
A Row contains Text('A'), SizedBox(height: 16), and Text('B'). The two letters touch. Why, and how do you fix it?
Show answer
In a Row, the main axis is horizontal, so the gap must come from width. The SizedBox has height 16 and no child, so its width is 0. Change it to SizedBox(width: 16). On Flutter 3.27 or later you could instead set spacing: 16 on the Row.
Exercise
A Padding with EdgeInsets.symmetric(horizontal: 20, vertical: 10) sits under a parent that offers a maximum width of 360. It wraps a child that takes 200 width and 50 height. What maximum width does the child receive, and what size does the Padding report to its parent?
Show answer
The child receives a maximum width of 360 - 40 = 320. The Padding reports 200 + 40 = 240 wide and 50 + 20 = 70 tall.