Flutter Row and Column widgets serve as the building blocks for arranging elements horizontally and vertically within your application.
Row and Column are flex widgets in Flutter that organize their children in a linear arrangement. The Row widget positions elements horizontally, while the Column widget arranges them vertically. Both widgets inherit from the Flex class and share many properties, making them consistent in their usage patterns.
When working with Row and Column, several properties control how child widgets are positioned:
The mainAxisAlignment
property offers six different options to position child widgets:
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(color: Colors.red, height: 50, width: 50),
Container(color: Colors.blue, height: 50, width: 50),
Container(color: Colors.green, height: 50, width: 50),
],
)
The crossAxisAlignment
property offers five options for positioning children along the cross axis:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(\"Flutter\"),
Text(\"Row\"),
Text(\"Column\"),
],
)
The mainAxisSize
property determines how much space the Row or Column should occupy along its main axis:
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.star),
Text(\"Rated 4.9\"),
],
)
To create sophisticated UI layouts, you can nest Row and Column widgets within each other. This nesting allows for the creation of grid-like structures and complex arrangements.
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(\"User Profile\"),
Icon(Icons.settings),
],
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(radius: 50),
],
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(children: [Icon(Icons.phone), Text(\"Call\")]),
Column(children: [Icon(Icons.message), Text(\"Message\")]),
Column(children: [Icon(Icons.video_call), Text(\"Video\")]),
],
),
],
)
The Expanded and Flexible widgets work with Row and Column to create responsive layouts:
Row(
children: [
Container(color: Colors.blue, width: 100),
Expanded(
child: Container(color: Colors.red),
),
Container(color: Colors.yellow, width: 100),
],
)
Using multiple Expanded widgets distributes space proportionally:
Row(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red),
),
Expanded(
flex: 2,
child: Container(color: Colors.green),
),
],
)
When children don't fit within a Row or Column, overflow occurs. Handle this with:
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.generate(
10,
(index) => Container(
width: 150,
height: 100,
color: Colors.primaries[index % Colors.primaries.length],
margin: EdgeInsets.all(8),
child: Center(child: Text('Item $index')),
),
),
),
)
Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
CircleAvatar(radius: 30),
SizedBox(width: 16),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('John Doe', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
Text('Flutter Developer'),
],
),
],
),
SizedBox(height: 16),
Text('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'),
SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(onPressed: () {}, child: Text('MESSAGE')),
TextButton(onPressed: () {}, child: Text('FOLLOW')),
],
),
],
),
),
)
Container(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
color: Colors.blue,
child: Row(
children: [
IconButton(icon: Icon(Icons.menu, color: Colors.white), onPressed: () {}),
Expanded(
child: Text(
'Flutter Row & Column',
style: TextStyle(color: Colors.white, fontSize: 20),
textAlign: TextAlign.center,
),
),
IconButton(icon: Icon(Icons.search, color: Colors.white), onPressed: () {}),
],
),
)