Using Rows and Columns in Flutter
๐ Row vs. Column Overview
Widget Layout Direction Example Use
Row Horizontal Buttons next to each other
Column Vertical Stack elements like title, subtitle, and button
๐น Basic Syntax
๐ธ Row Example
dart
Copy
Edit
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(Icons.menu),
Text('Title'),
Icon(Icons.search),
],
)
๐ธ Column Example
dart
Copy
Edit
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Hello'),
Text('World'),
ElevatedButton(
onPressed: () {},
child: Text('Click me'),
),
],
)
๐ฏ Key Properties
๐น mainAxisAlignment
Controls alignment along the main axis:
start, center, end, spaceBetween, spaceAround, spaceEvenly
๐น crossAxisAlignment
Controls alignment perpendicular to the main axis:
start, center, end, stretch, baseline
๐ In a Row, the main axis is horizontal, and the cross axis is vertical. In a Column, it's the reverse.
⚠️ Common Gotchas
❗ Row or Column Takes Infinite Space
Wrap them in Expanded, Flexible, or give them size constraints using Container, SizedBox, etc.
dart
Copy
Edit
Row(
children: [
Expanded(child: Text("This text will expand to fill space")),
Icon(Icons.more_vert),
],
)
๐งฑ Nesting Rows and Columns
You can nest them to build complex layouts:
dart
Copy
Edit
Column(
children: [
Row(
children: [
Icon(Icons.star),
Text('Rating: 4.5'),
],
),
Text('Review goes here...'),
],
)
๐งฐ Bonus Tips
Use Spacer() inside a Row or Column to push elements apart.
Use SizedBox(height: 10) for spacing in Columns, or width for Rows.
Wrap text-heavy widgets in Expanded or Flexible to avoid overflow.
Learn Flutter Training in Hyderabad
Read More
Creating Responsive Layouts in Flutter
Working with Text and Images in Flutter
How to Use the Flutter Debug Console
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment