Flutter Padding, Center, Align & Expanded
Flutter favours small widgets that each do one layout job. This lesson covers the helpers you will reach for constantly: Padding, Center, Align, SizedBox, Expanded, Flexible and Spacer.
| Widget | Job |
|---|---|
Padding |
Adds empty space around its child |
Center |
Centres its child in the available space |
Align |
Places its child at any alignment |
SizedBox |
Forces a size, or creates a fixed gap |
Expanded |
Makes a Row/Column child fill the remaining space |
Flexible |
Lets a Row/Column child up to its share of space |
Spacer |
An empty Expanded — pushes siblings apart |
Padding, Center and Align
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: AlignDemo()));
class AlignDemo extends StatelessWidget {
const AlignDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(24),
child: Container(
color: Colors.blueGrey.shade50,
child: const Stack(
children: [
Align(alignment: Alignment.topLeft, child: Text('topLeft')),
Align(alignment: Alignment.topRight, child: Text('topRight')),
Center(child: Text('Center')),
Align(alignment: Alignment(0, 0.6), child: Text('Alignment(0, 0.6)')),
Align(alignment: Alignment.bottomRight, child: Text('bottomRight')),
],
),
),
),
);
}
}Alignment(x, y) uses a coordinate system where (-1, -1) is the top-left corner, (0, 0) the centre and (1, 1) the bottom-right corner. Center is literally an Align with Alignment.center.
Expanded: share the remaining space
Inside a Row or Column, children normally take only the size they need. Wrap one in Expanded and it grows to fill whatever space is left. With several Expanded children, the flex factor sets their proportions:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
body: Center(
child: SizedBox(
height: 80,
child: Row(
children: [
Container(width: 60, color: Colors.grey), // fixed 60 px
Expanded(flex: 2, child: Container(color: Colors.orange)),
Expanded(flex: 1, child: Container(color: Colors.deepOrange)),
],
),
),
),
),
));First the grey box gets its 60 px. The remaining 300 px are split 2:1 — 200 px orange, 100 px deep orange.
Note:
Expandedmust be a direct child of aRow,ColumnorFlex. Using it anywhere else causes an "Incorrect use of ParentDataWidget" error.
Flexible vs Expanded
Expanded forces its child to fill the space. Flexible allows the child to be smaller. This matters for text that should wrap or truncate instead of overflowing, but should not stretch:
Row(
children: [
const Icon(Icons.label),
const SizedBox(width: 8),
Flexible(
child: Text(
'A rather long tag name that would overflow a row',
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 8),
const Chip(label: Text('new')),
],
)SizedBox and Spacer
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(
home: Scaffold(
body: Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
Row(
children: [
Text('Subtotal'),
Spacer(), // pushes the price to the right
Text('\$18.00'),
],
),
SizedBox(height: 12), // fixed vertical gap
Row(
children: [
Text('Total', style: TextStyle(fontWeight: FontWeight.bold)),
Spacer(),
Text('\$19.80', style: TextStyle(fontWeight: FontWeight.bold)),
],
),
SizedBox(height: 24),
SizedBox(
width: double.infinity, // full-width button
child: FilledButton(onPressed: null, child: Text('Pay')),
),
],
),
),
),
));SizedBox(width: double.infinity) makes its child as wide as allowed. The button is greyed out because onPressed is null (disabled).
Tip: Prefer
SizedBoxoverContainerfor gaps — it can beconst, which is cheaper.
Test yourself
A Row is 400 px wide and contains a 100 px box plus Expanded(flex: 1) and Expanded(flex: 2). How wide is the flex-2 child?
- 100 px
- 150 px
- 200 px
- 266 px
Which Alignment value is the bottom-centre of the parent?
- Alignment(0, 1)
- Alignment(1, 0)
- Alignment(0, -1)
- Alignment(1, 1)
What is a Spacer?
- A widget that adds fixed padding
- An empty Expanded that takes the remaining flex space
- A divider line
- An alias for SizedBox