Sub topic 8
Sharing Space with Expanded, Flexible, and Spacer
This chapter assumes basic Dart syntax, StatelessWidget, the build method, and a working Flutter project. You should also understand that a Row lays out children horizontally and a Column lays them out vertically.
Expanded, Flexible, and Spacer are flex widgets. They work inside a Row, Column, or another Flex widget. They control how children use the remaining space after their non-flex children have been measured.
Expanded takes all assigned space
Expanded forces its child to fill the space assigned to it. The flex value controls the proportion of the available space.
For example, two children with flex values of 2 and 1 receive two-thirds and one-third of the remaining space:
Row(
children: [
Expanded(
flex: 2,
child: ColoredBox(color: Colors.blue),
),
Expanded(
child: ColoredBox(color: Colors.red),
),
],
)The default flex value is 1. If every Expanded has the default value, they divide the space equally.
Flex space is calculated after fixed-size children, such as icons, text, and SizedBox, are measured.
Flexible allows unused space
Flexible also participates in the flex calculation, but it does not force its child to fill the entire allocation. The child can use less space when its natural size is smaller.
This is useful when a child should shrink to fit but should not stretch unnecessarily:
Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
Flexible(
child: Text(
'This message can shrink when the available width is small.',
),
),
],
)The default fit for Flexible is loose. You can use FlexFit.tight to make it behave like Expanded:
Flexible(
fit: FlexFit.tight,
child: const Text('Fills its allocation'),
)| Widget | Child must fill its allocation? | Typical use |
|---|---|---|
| `Expanded` | Yes | Divide remaining space |
| `Flexible` | No, by default | Let content shrink naturally |
| `Spacer` | Yes, with no visible child | Push widgets apart |
Spacer creates an invisible gap
Spacer is a short form of an empty Expanded. Its flex value determines how much remaining space becomes a gap.
Row(
children: [
const Text('Cancel'),
const Spacer(),
FilledButton(
onPressed: null,
child: const Text('Save'),
),
],
)The button moves to the end of the row without manually calculating screen width. Use Spacer(flex: 2) when one gap should be twice as large as another.
A complete example
The following app uses Expanded for equal content areas, Flexible for text that can shrink, and Spacer to separate controls:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: FlexExample()));
}
class FlexExample extends StatelessWidget {
const FlexExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flex widgets')),
body: Center(
child: SizedBox(
width: 320,
height: 220,
child: Column(
children: [
Flexible(
child: const Text(
'A title that can use less space when needed',
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(height: 12),
Expanded(
child: Row(
children: [
Expanded(
child: ColoredBox(color: Colors.blue),
),
const SizedBox(width: 8),
Expanded(
child: ColoredBox(color: Colors.orange),
),
],
),
),
const SizedBox(height: 12),
Row(
children: [
const Text('Status'),
const Spacer(),
FilledButton(
onPressed: () {},
child: const Text('Done'),
),
],
),
],
),
),
),
);
}
}The SizedBox gives the Column a finite height. Without a bounded height, an Expanded child may cause an unbounded-constraint error.
Exercise
A Row contains a label, a long description, and a button. Which widget should wrap the description so it can shrink without pushing the button off-screen?
Show answer
Wrap the description in Flexible or Expanded. Flexible is usually preferable when the description should use only the space it needs, while Expanded is useful when it should fill all remaining space.