These properties help you arrange widgets inside a row or column neatly. They control where the widgets sit horizontally and vertically.
MainAxisAlignment and CrossAxisAlignment in Flutter
Row(
mainAxisAlignment: MainAxisAlignment.<value>,
crossAxisAlignment: CrossAxisAlignment.<value>,
children: [
// your widgets here
],
)
Column(
mainAxisAlignment: MainAxisAlignment.<value>,
crossAxisAlignment: CrossAxisAlignment.<value>,
children: [
// your widgets here
],
)MainAxisAlignment controls alignment along the main axis (horizontal for Row, vertical for Column).
CrossAxisAlignment controls alignment along the cross axis (vertical for Row, horizontal for Column).
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('A'),
Text('B'),
Text('C'),
],
)Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(onPressed: () {}, child: Text('One')),
ElevatedButton(onPressed: () {}, child: Text('Two')),
ElevatedButton(onPressed: () {}, child: Text('Three')),
],
)This app shows three star icons in a row. They are spaced evenly across the screen horizontally and aligned vertically in the center.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Alignment Demo')), body: Center( child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center, children: const [ Icon(Icons.star, size: 50, color: Colors.red), Icon(Icons.star, size: 30, color: Colors.green), Icon(Icons.star, size: 40, color: Colors.blue), ], ), ), ), ); } }
If you use CrossAxisAlignment.stretch, the children will expand to fill the cross axis size.
Use MainAxisAlignment.spaceBetween to put space only between widgets, not at the ends.
Remember that main and cross axis directions depend on whether you use Row (horizontal main axis) or Column (vertical main axis).
MainAxisAlignment controls how widgets are placed along the main direction (row or column).
CrossAxisAlignment controls how widgets are placed perpendicular to the main direction.
These properties help make your app look neat and organized.
Practice
MainAxisAlignment control in a Flutter Row widget?Solution
Step 1: Understand the main axis in a Row
In a Row widget, the main axis runs horizontally from left to right.Step 2: Role of MainAxisAlignment
MainAxisAlignment controls how children are placed along this horizontal main axis.Final Answer:
How children are aligned horizontally along the row -> Option CQuick Check:
MainAxisAlignment = horizontal alignment in Row [OK]
- Confusing main axis with cross axis
- Mixing Row and Column directions
- Thinking MainAxisAlignment controls vertical alignment in Row
Row using MainAxisAlignment?Solution
Step 1: Identify the property for main axis alignment
The property to align children along the main axis ismainAxisAlignment.Step 2: Use the correct enum value
To center children, useMainAxisAlignment.center.Final Answer:
mainAxisAlignment: MainAxisAlignment.center, -> Option DQuick Check:
Center horizontally in Row = mainAxisAlignment.center [OK]
- Using crossAxisAlignment instead of mainAxisAlignment
- Mixing enum types for properties
- Forgetting the comma at the end
Row?
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(height: 50, width: 50, color: Colors.red),
Container(height: 100, width: 50, color: Colors.blue),
],
)Solution
Step 1: Understand crossAxisAlignment in Row
In a Row, the cross axis is vertical. CrossAxisAlignment.end aligns children at the bottom.Step 2: Apply to given containers
The containers have different heights, so aligning at the bottom means their bottom edges line up.Final Answer:
Children aligned at the bottom of the Row -> Option AQuick Check:
CrossAxisAlignment.end = bottom alignment in Row [OK]
- Confusing CrossAxisAlignment.end with top alignment
- Mixing main and cross axis directions
- Assuming children stretch by default
Column(
mainAxisAlignment: MainAxisAlignment.left,
children: [
Text('Hello'),
Text('World'),
],
)Solution
Step 1: Check MainAxisAlignment values
MainAxisAlignment enum does not have a 'left' value; valid values include start, end, center, spaceBetween, etc.Step 2: Confirm correct usage
To align children to the start horizontally in a Column, use MainAxisAlignment.start.Final Answer:
MainAxisAlignment.left is invalid; use start or end instead -> Option BQuick Check:
MainAxisAlignment.left is not a valid enum [OK]
- Using left or right instead of start or end
- Confusing MainAxisAlignment with CrossAxisAlignment
- Ignoring enum value errors
Column where children are spaced evenly vertically, but each child is aligned to the start horizontally. Which combination of MainAxisAlignment and CrossAxisAlignment should you use?Solution
Step 1: Understand vertical spacing in Column
MainAxisAlignment controls vertical spacing in Column. To space children evenly, use MainAxisAlignment.spaceEvenly.Step 2: Understand horizontal alignment in Column
CrossAxisAlignment controls horizontal alignment. To align children to the start (left), use CrossAxisAlignment.start.Final Answer:
mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start -> Option AQuick Check:
Vertical spacing evenly + horizontal start = mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start [OK]
- Mixing up main and cross axis in Column
- Using spaceBetween instead of spaceEvenly
- Choosing wrong crossAxisAlignment for start alignment
Ready to prove it?
Pass the test and this pattern moves unknown
