Using Rows and Columns in Flutter
Using Rows and Columns in Flutter
Flutter uses Rows and Columns as fundamental layout widgets to arrange child widgets horizontally and vertically.
What Are Rows and Columns?
Row: Places its children in a horizontal line (left to right).
Column: Places its children in a vertical line (top to bottom).
Both are subclasses of the Flex widget, and you can customize their alignment, spacing, and size.
Basic Syntax
dart
Copy
Edit
Row(
children: [
Widget1(),
Widget2(),
Widget3(),
],
)
Column(
children: [
WidgetA(),
WidgetB(),
WidgetC(),
],
)
Important Properties
Property Description
mainAxisAlignment How to align children along the main axis (horizontal for Row, vertical for Column). Examples: start, center, spaceBetween, spaceAround.
crossAxisAlignment How to align children along the cross axis (vertical for Row, horizontal for Column). Examples: start, center, stretch.
mainAxisSize How much space the Row/Column takes along the main axis: min (wrap content) or max (expand).
Example: Row
dart
Copy
Edit
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(Icons.star, color: Colors.red),
Text('Star'),
ElevatedButton(onPressed: () {}, child: Text('Click')),
],
)
Places the icon, text, and button evenly spaced horizontally.
Vertically centers them.
Example: Column
dart
Copy
Edit
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Title', style: TextStyle(fontSize: 24)),
Text('Subtitle'),
ElevatedButton(onPressed: () {}, child: Text('Submit')),
],
)
Stacks the texts and button vertically.
Centers them vertically in the available space.
Aligns them to the start horizontally (left side).
Nesting Rows and Columns
You can nest Rows inside Columns and vice versa to create complex layouts.
dart
Copy
Edit
Column(
children: [
Row(
children: [
Icon(Icons.home),
Text('Home'),
],
),
Row(
children: [
Icon(Icons.settings),
Text('Settings'),
],
),
],
)
Tips
Use Expanded or Flexible widgets inside Rows or Columns to control how children resize.
Avoid overflow errors by wrapping content with SingleChildScrollView if space might be limited.
Use Spacer() widget to insert flexible gaps between children.
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
Comments
Post a Comment