Sub topic 4
Main axis and cross axis
Assume you know basic Dart syntax, StatelessWidget, and the build method. You should also have a Flutter project that can run on a device or emulator.
Row and Column are flex layout widgets. They arrange their children along two directions:
- The main axis is the direction in which children are placed.
- The cross axis is perpendicular to the main axis.
The widget determines these axes from its type:
| Widget | Main axis | Cross axis |
|---|---|---|
| `Row` | Horizontal, left to right | Vertical, top to bottom |
| `Column` | Vertical, top to bottom | Horizontal, left to right |
Aligning along each axis
mainAxisAlignment controls how Flutter places children along the main axis. For a Row, it controls horizontal placement. For a Column, it controls vertical placement.
Common values include:
start: place children at the beginning.end: place children at the end.center: place children in the middle.spaceBetween: put equal space between children, with no extra space at the ends.spaceAround: put equal space around each child.spaceEvenly: divide all gaps, including the outer gaps, evenly.
crossAxisAlignment controls placement across the main axis. For a Row, it controls vertical placement. For a Column, it controls horizontal placement.
Common values include:
startendcenterstretch
The meaning of start and end can depend on text direction. In a left-to-right interface, start usually means the left side. For a right-to-left interface, it usually means the right side.
A working example
This example uses the same alignment properties on both widgets. Run it in a Flutter project to compare their axes.
import 'package:flutter/material.dart';
void main() {
runApp(const AxisDemoApp());
}
class AxisDemoApp extends StatelessWidget {
const AxisDemoApp({super.key});
Widget box(String label, Color color) {
return Container(
width: 90,
height: 50,
color: color,
alignment: Alignment.center,
child: Text(
label,
style: const TextStyle(color: Colors.white),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Axes')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
box('A', Colors.blue),
box('B', Colors.green),
box('C', Colors.orange),
],
),
),
const Divider(),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
box('1', Colors.purple),
box('2', Colors.red),
box('3', Colors.teal),
],
),
),
],
),
),
),
);
}
}In the Row, spaceEvenly divides horizontal space because horizontal is the main axis. In the Column, the same value divides vertical space because vertical is its main axis.
A common mistake
Developers often choose an alignment property based on the screen direction instead of the widget's main axis. For example, to center children vertically in a Row, use:
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Icon(Icons.star),
Text('Rating'),
],
)The vertical direction is the Row's cross axis, so crossAxisAlignment is the correct property.
Exercise
In a Column, which property centers its children horizontally?
Show answer
Use crossAxisAlignment: CrossAxisAlignment.center, because a Column's horizontal direction is its cross axis.