Column and Row are the primary layout widgets in Flutter for arranging multiple children in a single direction.
Column Widget
Arranges children vertically (top-to-bottom).
Vertical Alignment
example.dart
1 2Column(3 mainAxisAlignment: MainAxisAlignment.center, // Vertically center4 crossAxisAlignment: CrossAxisAlignment.stretch, // Stretch horizontally5 children: [6 Container(color: Colors.red, height: 50),7 Container(color: Colors.green, height: 50),8 Container(color: Colors.blue, height: 50),9 ],10)11 HintThis column centers the colored boxes vertically and stretches them horizontally across the available width.
Row Widget
Arranges children horizontally (left-to-right based on locale).
Horizontal Alignment
example.dart
1 2Row(3 mainAxisAlignment: MainAxisAlignment.spaceEvenly, // Equal space between widgets4 children: [5 Icon(Icons.star, size: 50),6 Icon(Icons.star, size: 50),7 Icon(Icons.star, size: 50),8 ],9)10 HintThis row places three star icons with equal spacing between them.
Common Properties
- mainAxisAlignment: Controls alignment along the main axis (vertical for Column, horizontal for Row). Options: start, end, center, spaceBetween, spaceAround, spaceEvenly.
- crossAxisAlignment: Controls alignment perpendicular to the main axis. Options: start, end, center, stretch, baseline.
- mainAxisSize: Determines how much space the layout should occupy along its main axis (min or max).
Loot collected
Key takeaways- Use Column for vertical layout.
- Use Row for horizontal layout.
- Control alignment using MainAxisAlignment and CrossAxisAlignment.
- Be mindful of overflow issues if content exceeds the available space.
Party chat