Sub topic 3
Row and Column Basics
Assume you know basic Dart syntax, can create a StatelessWidget, and can run a Flutter app. This chapter focuses on the two most common Flutter layout widgets: Row and Column.
Row arranges its children horizontally. Column arranges its children vertically. Both widgets use the same layout ideas, but their main axes point in different directions.
Choosing between Row and Column
| Widget | Main axis | Typical use |
|---|---|---|
| `Row` | Horizontal | Icon and label, actions, toolbar items |
| `Column` | Vertical | Form fields, headings, page sections |
A widget's children property receives a list of widgets. Flutter lays out those children in list order.
import 'package:flutter/material.dart';
void main() {
runApp(const LayoutExampleApp());
}
class LayoutExampleApp extends StatelessWidget {
const LayoutExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Row and Column')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Account',
style: TextStyle(fontSize: 24),
),
const SizedBox(height: 16),
Row(
children: [
const Icon(Icons.person),
const SizedBox(width: 8),
const Text('Alex Morgan'),
],
),
const SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Status'),
Text(
'Active',
style: TextStyle(
color: Colors.green.shade700,
),
),
],
),
],
),
),
),
);
}
}The outer Column places the title, first Row, and second Row from top to bottom. Each Row places its children from left to right. SizedBox adds fixed space between widgets.
The widget tree and layout direction
Flutter builds a widget tree. In the example, the Column is the parent of several children, and each Row is a child of that Column.
The parent gives each child layout constraints. A child chooses a size within those constraints, and the parent positions it. This is the basic “constraints down, sizes up, positions down” model.
A Row receives available width and arranges children along that width. A Column receives available height and arranges children along that height. The opposite direction is called the cross axis.
Basic sizing and common mistakes
A Row does not automatically make each child fill the available width. A Column does not automatically make every child fill the available height. Their children usually take the space they need.
For example, this Column contains two text widgets with their natural widths:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text('Short title'),
Text('A longer description can use more width'),
],
)If you need a child to occupy remaining space, use Expanded, which is covered in the next layout step. Do not solve every spacing problem with large margins. Prefer small, intentional gaps with SizedBox, or use Padding when space belongs around a widget.
Exercise
Which widget should you use to place an icon to the left of a label, and which widget should you use to place a heading above a paragraph?
Show answer
Use a Row for the icon and label. Use a Column for the heading and paragraph.