Sub topic 7
Adding Space with SizedBox and Padding
Assume you know basic Dart syntax, StatelessWidget, and the build method. This chapter focuses on spacing inside a Flutter layout. Spacing changes position and size, but it does not add visible content.
`SizedBox`: a fixed gap or size
SizedBox is a widget that gives its child a fixed width, height, or both. When it has no child, it creates empty space.
Use a vertical SizedBox between items in a Column:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Account'),
const SizedBox(height: 12),
const Text('Manage your profile'),
],
)The SizedBox creates a 12-pixel gap between the two Text widgets. In a Row, use width instead:
Row(
children: [
const Icon(Icons.person),
const SizedBox(width: 8),
const Text('Profile'),
],
)A SizedBox can also give a child a fixed size:
const SizedBox(
width: 160,
height: 48,
child: ElevatedButton(
onPressed: null,
child: Text('Continue'),
),
)The parent still supplies constraints. The SizedBox requests its configured size, and the parent may tighten those constraints.
`Padding`: space around a child
Padding is a widget that adds empty space around one child. Its padding property uses EdgeInsets, which describes the space on each side.
Padding(
padding: const EdgeInsets.all(16),
child: const Text('This text has space on every side.'),
)You can choose individual sides:
Padding(
padding: const EdgeInsets.only(
left: 20,
right: 20,
top: 12,
bottom: 8,
),
child: const Text('Details'),
)EdgeInsets.symmetric is useful when opposite sides match:
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 10,
),
child: const Text('A padded message'),
)Padding affects layout in two steps. Flutter first gives the padded widget constraints. The padding uses part of that space, then the child lays itself out in the remaining space. The padded widget becomes the child’s size plus the padding, subject to the parent’s constraints.
| Widget | Main use | Typical example |
|---|---|---|
| `SizedBox` | Fixed gap or fixed child size | `SizedBox(height: 16)` |
| `Padding` | Space around one child | `Padding(padding: EdgeInsets.all(16), ...)` |
Choosing between them
Use SizedBox when the relationship between separate widgets needs a fixed gap. Use Padding when a widget needs breathing room inside its own boundary.
Avoid stacking many Padding widgets when one EdgeInsets value expresses the same design. Nested padding is valid, but the distances add together, which can make the result harder to reason about.
A complete example
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: SpacingExample()));
}
class SpacingExample extends StatelessWidget {
const SpacingExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Welcome'),
const SizedBox(height: 8),
const Text('Choose an option to continue.'),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {},
child: const Text('Continue'),
),
),
],
),
),
);
}
}Exercise
Replace the two SizedBox widgets after the heading with padding on the subtitle. What spacing changes, and why?
Show answer
The subtitle receives space around itself instead of separate gaps controlled by the column. Its top and bottom padding should equal the intended distances, while other children remain unaffected.