Sub topic 3
Layouts that flex: Expanded, Flexible, Wrap and AspectRatio
This chapter assumes you know basic Dart, widget trees, and Row, Column, Scaffold, and ListView. It also assumes you can run a Flutter app. The goal is to build layouts that use the space they receive instead of depending on fixed screen sizes.
Constraints and flexible space
Flutter layout follows a simple direction:
- A parent sends constraints down to its children.
- A child chooses a size within those constraints.
- The child reports its size back up.
- The parent positions the child.
Expanded and Flexible work inside a Row, Column, or another Flex widget. They divide the free space along the main axis.
Expanded forces its child to fill the space assigned to it. Flexible lets its child use less space when its natural size is smaller.
| Widget | Child must fill assigned space | Typical use |
|---|---|---|
| `Expanded` | Yes | Equal-width panels or a body that fills the screen |
| `Flexible` | No | Text or content that may use less space |
| `Wrap` | Not applicable | Items that move onto additional lines |
| `AspectRatio` | Within constraints | Images, cards, or media with a fixed shape |
Using Expanded and Flexible
This row gives the second panel twice the remaining width of the first panel.
import 'package:flutter/material.dart';
class FlexiblePanels extends StatelessWidget {
const FlexiblePanels({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flexible panels')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: Container(
color: Colors.blue,
child: const Center(child: Text('One part')),
),
),
const SizedBox(width: 12),
Expanded(
flex: 2,
child: Container(
color: Colors.green,
child: const Center(child: Text('Two parts')),
),
),
],
),
),
);
}
}The flex value is a ratio, not a pixel width. The two children receive one-third and two-thirds of the remaining width.
Use Flexible when a child should not be forced to fill its share. This is useful for a Row containing text and an icon. Long text should usually be wrapped in Flexible so it can shrink instead of causing a layout overflow.
Using Wrap for changing row lengths
Row keeps all children on one line. If their combined width is too large, Flutter reports an overflow. Wrap moves children to the next run.
Wrap(
spacing: 8,
runSpacing: 8,
children: const [
Chip(label: Text('Dart')),
Chip(label: Text('Flutter')),
Chip(label: Text('Web')),
Chip(label: Text('Desktop')),
],
)spacing separates items within a run. runSpacing separates runs. Wrap works well for tags, filters, and compact action buttons. For large or potentially unbounded collections, prefer a lazy builder such as GridView.builder.
Preserving shape with AspectRatio
AspectRatio requests a width-to-height ratio. An aspect ratio of 16 / 9 creates a wide rectangle when the parent provides usable width.
AspectRatio(
aspectRatio: 16 / 9,
child: ColoredBox(
color: Colors.black12,
child: Center(
child: Text(
'16:9 preview',
style: TextStyle(fontSize: 20),
),
),
),
)The parent’s constraints still win. If the parent gives tight height constraints or no usable width, AspectRatio may not produce the ratio you expect. Test it inside the real parent, not only in isolation.
Common errors
- Put
ExpandedandFlexibleonly inside aFlexwidget. - Use
Flexiblefor content that may need less than its share. - Replace a fixed
RowwithWrapwhen items can move to another line. - Avoid hard-coded widths that prevent children from shrinking.
- Check text scaling and narrow windows when testing.
Exercise
Which widget should you choose for a row of filter chips that may need several lines?
Show answer
Choose Wrap, because it moves children onto additional runs when the available width is insufficient.