Building Layouts with Row and Column
Row and Column are the fundamental layout widgets in Flitter. Row arranges widgets horizontally, while Column arranges them vertically. These two widgets are the foundation for creating all UI layouts.
๐ฏ Learning Objectives
After completing this tutorial, youโll be able to:
- Arrange widgets horizontally with Row
- Arrange widgets vertically with Column
- Control spacing and alignment between widgets
- Create navigation bars and simple layouts
๐ Basic Concepts
Row: Horizontal Arrangement
Row({
children: [
// Widgets to arrange horizontally
],
mainAxisAlignment: MainAxisAlignment.center, // Horizontal alignment
crossAxisAlignment: CrossAxisAlignment.center // Vertical alignment
})
Column: Vertical Arrangement
Column({
children: [
// Widgets to arrange vertically
],
mainAxisAlignment: MainAxisAlignment.center, // Vertical alignment
crossAxisAlignment: CrossAxisAlignment.center // Horizontal alignment
})
๐ Practice 1: Basic Row Layout
Letโs start by arranging widgets horizontally.
import { Row, Container, Text, Center } from "@meursyphus/flitter";
const basicRow = Row({
children: [
Container({
width: 80,
height: 80,
color: '#FF6B6B',
child: Center({
child: Text("Box 1")
})
}),
Container({
width: 80,
height: 80,
color: '#4ECDC4',
child: Center({
child: Text("Box 2")
})
}),
Container({
width: 80,
height: 80,
color: '#45B7D1',
child: Center({
child: Text("Box 3")
})
})
]
});
๐ป Try It Out
- Change the colors of each Container
- Modify the size of the Containers
- Change the text inside each box
๐ Practice 2: Row Alignment Options
Letโs learn how to control the spacing and alignment of widgets in a Row.
import { Row, Container, Text, Center, MainAxisAlignment } from "@meursyphus/flitter";
// Center alignment
const centeredRow = Row({
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container({ width: 50, height: 50, color: '#FF6B6B' }),
Container({ width: 50, height: 50, color: '#4ECDC4' }),
Container({ width: 50, height: 50, color: '#45B7D1' })
]
});
// Space between
const spaceBetweenRow = Row({
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container({ width: 50, height: 50, color: '#FF6B6B' }),
Container({ width: 50, height: 50, color: '#4ECDC4' }),
Container({ width: 50, height: 50, color: '#45B7D1' })
]
});
// Space evenly
const spaceEvenlyRow = Row({
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container({ width: 50, height: 50, color: '#FF6B6B' }),
Container({ width: 50, height: 50, color: '#4ECDC4' }),
Container({ width: 50, height: 50, color: '#45B7D1' })
]
});
MainAxisAlignment Options
MainAxisAlignment.start
: Align to start (left)MainAxisAlignment.end
: Align to end (right)MainAxisAlignment.center
: Center alignmentMainAxisAlignment.spaceBetween
: Space between widgetsMainAxisAlignment.spaceAround
: Space around widgetsMainAxisAlignment.spaceEvenly
: Even spacing
๐ป Try It Out
- Try each MainAxisAlignment option
- Change the number of widgets
- Modify widget sizes to see alignment effects
๐ Practice 3: Basic Column Layout
Now letโs arrange widgets vertically.
import { Column, Container, Text, Center } from "@meursyphus/flitter";
const basicColumn = Column({
children: [
Container({
width: 150,
height: 60,
color: '#FF6B6B',
child: Center({
child: Text("Item 1")
})
}),
Container({
width: 150,
height: 60,
color: '#4ECDC4',
child: Center({
child: Text("Item 2")
})
}),
Container({
width: 150,
height: 60,
color: '#45B7D1',
child: Center({
child: Text("Item 3")
})
})
]
});
๐ป Try It Out
- Change the height of each Container
- Try different colors
- Add more items to the Column
๐ Practice 4: Column Alignment Options
Letโs learn Column alignment options.
import { Column, Container, MainAxisAlignment, CrossAxisAlignment } from "@meursyphus/flitter";
// Vertical spacing control
const spacedColumn = Column({
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container({ width: 100, height: 40, color: '#FF6B6B' }),
Container({ width: 100, height: 40, color: '#4ECDC4' }),
Container({ width: 100, height: 40, color: '#45B7D1' })
]
});
// Horizontal alignment control
const alignedColumn = Column({
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container({ width: 80, height: 40, color: '#FF6B6B' }),
Container({ width: 120, height: 40, color: '#4ECDC4' }),
Container({ width: 100, height: 40, color: '#45B7D1' })
]
});
CrossAxisAlignment Options
CrossAxisAlignment.start
: Align to startCrossAxisAlignment.end
: Align to endCrossAxisAlignment.center
: Center alignmentCrossAxisAlignment.stretch
: Stretch to full width
๐ป Try It Out
- Try different CrossAxisAlignment options
- Use containers with different widths
- Combine MainAxisAlignment and CrossAxisAlignment
๐ Practice 5: Adding Spacing with SizedBox
Letโs learn how to add spacing between widgets.
import { Column, Row, Container, Text, SizedBox, EdgeInsets } from "@meursyphus/flitter";
const spacedLayout = Column({
children: [
Text("First section"),
SizedBox({ height: 20 }), // Vertical spacing
Container({
padding: EdgeInsets.all(16),
color: '#F5F5F5',
child: Row({
children: [
Container({ width: 50, height: 50, color: '#FF6B6B' }),
SizedBox({ width: 10 }), // Horizontal spacing
Container({ width: 50, height: 50, color: '#4ECDC4' }),
SizedBox({ width: 10 }),
Container({ width: 50, height: 50, color: '#45B7D1' })
]
})
}),
SizedBox({ height: 20 }),
Text("Second section")
]
});
SizedBox Usage
SizedBox({ width: 10 })
: Horizontal spacing of 10 pixelsSizedBox({ height: 20 })
: Vertical spacing of 20 pixelsSizedBox({ width: 10, height: 20 })
: Both horizontal and vertical spacing
๐ป Try It Out
- Change SizedBox dimensions
- Add more SizedBoxes between widgets
- Remove SizedBoxes to see the difference
๐ Practice 6: Nesting Row and Column
Letโs create complex layouts by nesting Row and Column.
import { Column, Row, Container, Text, SizedBox, EdgeInsets, TextStyle, Center } from "@meursyphus/flitter";
const complexLayout = Container({
padding: EdgeInsets.all(20),
child: Column({
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header
Text("Dashboard", {
style: new TextStyle({
fontSize: 24,
fontWeight: 'bold'
})
}),
SizedBox({ height: 20 }),
// Statistics row
Row({
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column({
children: [
Text("Users", {
style: new TextStyle({ color: '#666' })
}),
Text("1,234", {
style: new TextStyle({
fontSize: 20,
fontWeight: 'bold',
color: '#2196F3'
})
})
]
}),
Column({
children: [
Text("Orders", {
style: new TextStyle({ color: '#666' })
}),
Text("567", {
style: new TextStyle({
fontSize: 20,
fontWeight: 'bold',
color: '#4CAF50'
})
})
]
}),
Column({
children: [
Text("Revenue", {
style: new TextStyle({ color: '#666' })
}),
Text("$89,123", {
style: new TextStyle({
fontSize: 20,
fontWeight: 'bold',
color: '#FF5722'
})
})
]
})
]
}),
SizedBox({ height: 30 }),
// Action buttons
Row({
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container({
padding: EdgeInsets.symmetric({ horizontal: 16, vertical: 8 }),
decoration: new BoxDecoration({
color: '#2196F3',
borderRadius: BorderRadius.circular(4)
}),
child: Text("View Report", {
style: new TextStyle({ color: 'white' })
})
}),
Container({
padding: EdgeInsets.symmetric({ horizontal: 16, vertical: 8 }),
decoration: new BoxDecoration({
color: '#4CAF50',
borderRadius: BorderRadius.circular(4)
}),
child: Text("Add New", {
style: new TextStyle({ color: 'white' })
})
})
]
})
]
})
});
๐ป Try It Out
- Add more statistics to the Row
- Change the layout structure
- Add more buttons or sections
๐จ Practice 7: Creating a Navigation Bar
Letโs create a practical navigation bar using Row.
import { Container, Row, Text, TextStyle, BoxDecoration, EdgeInsets, BorderRadius } from "@meursyphus/flitter";
const navigationBar = Container({
decoration: new BoxDecoration({
color: '#1976D2',
boxShadow: [
new BoxShadow({
color: 'rgba(0, 0, 0, 0.1)',
offset: { dx: 0, dy: 2 },
blurRadius: 4
})
]
}),
padding: EdgeInsets.symmetric({ horizontal: 20, vertical: 16 }),
child: Row({
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// Logo
Text("MyApp", {
style: new TextStyle({
fontSize: 20,
fontWeight: 'bold',
color: 'white'
})
}),
// Navigation links
Row({
children: [
Text("Home", {
style: new TextStyle({
color: 'white',
fontSize: 16
})
}),
SizedBox({ width: 24 }),
Text("About", {
style: new TextStyle({
color: 'white',
fontSize: 16
})
}),
SizedBox({ width: 24 }),
Text("Services", {
style: new TextStyle({
color: 'white',
fontSize: 16
})
}),
SizedBox({ width: 24 }),
Text("Contact", {
style: new TextStyle({
color: 'white',
fontSize: 16
})
})
]
}),
// User menu
Container({
padding: EdgeInsets.symmetric({ horizontal: 12, vertical: 6 }),
decoration: new BoxDecoration({
color: 'rgba(255, 255, 255, 0.2)',
borderRadius: BorderRadius.circular(16)
}),
child: Text("Login", {
style: new TextStyle({
color: 'white',
fontSize: 14
})
})
})
]
})
});
๐ป Try It Out
- Change navigation link text
- Adjust colors and spacing
- Add more navigation items
- Try different button styles
โ ๏ธ Common Mistakes and Solutions
1. Overflow Issues
// โ Problem: Too many widgets causing overflow
Row({
children: [
Container({ width: 200, height: 50, color: 'red' }),
Container({ width: 200, height: 50, color: 'blue' }),
Container({ width: 200, height: 50, color: 'green' })
// This might overflow on narrow screens
]
})
// โ
Solution: Use Flexible or Expanded
Row({
children: [
Flexible({ child: Container({ height: 50, color: 'red' }) }),
Flexible({ child: Container({ height: 50, color: 'blue' }) }),
Flexible({ child: Container({ height: 50, color: 'green' }) })
]
})
2. Missing SizedBox Import
// โ Using SizedBox without import
SizedBox({ height: 20 }) // Error!
// โ
Correct import
import { SizedBox } from "@meursyphus/flitter";
3. Alignment Confusion
// In Row:
// mainAxisAlignment controls horizontal alignment
// crossAxisAlignment controls vertical alignment
// In Column:
// mainAxisAlignment controls vertical alignment
// crossAxisAlignment controls horizontal alignment
๐ Challenge
Create a mobile app layout that includes:
- Header: App title on the left, menu icon on the right
- Main content: 2x2 grid using nested Rows and Columns
- Bottom navigation: 3 tabs evenly spaced
Requirements:
- Use proper spacing between elements
- Apply appropriate colors and text styles
- Ensure proper alignment
๐ Next Steps
Now that youโve mastered Row and Column layouts, letโs learn about Stack positioning!
๐ฏ Next Tutorial: Stack Positioning
Learn how to overlay widgets and create complex layered layouts.
๐ก Key Summary
- Row: Arranges widgets horizontally
- Column: Arranges widgets vertically
- MainAxisAlignment: Controls alignment along the main axis
- CrossAxisAlignment: Controls alignment along the cross axis
- SizedBox: Adds spacing between widgets
- Nesting: Combine Row and Column for complex layouts
These layout widgets are the foundation of all Flitter UIs!