When working with layouts in Flutter, especially in overlapping scenarios, the Stack
widget becomes your go-to choice. But within it, two powerful children stand out: Align
and Positioned
. Although they might seem to do similar things, their differences can significantly affect your layout behavior. Let’s walk through a new set of examples and explanations to better understand how these two work in different use cases.
๐ The Stack Refresher
The Stack
widget allows you to layer widgets on top of one another. You can think of it like a pile of cards where each widget stacks above the previous one. By default, children inside a Stack
align in the top-left corner, but we can change this behavior using widgets like Align
and Positioned
.
๐งญ Align Widget โ Relative Positioning
The Align
widget allows you to place a widget relative to the parent Stackโs dimensions. It uses Alignment
values, like Alignment.center
, Alignment.topRight
, etc.
๐ Example: Centering an Icon
Stack(
children: [
Container(
width: 200,
height: 200,
color: Colors.blue[100],
),
Align(
alignment: Alignment.center,
child: Icon(Icons.star, size: 40, color: Colors.orange),
),
],
)
๐ This centers the star icon inside the 200×200 container.
๐ Positioned Widget โ Absolute Positioning
The Positioned
widget lets you place a child based on distance from the Stackโs sides โ top, right, bottom, and left.
๐ Example: Place a button 20px from the bottom-right corner
Stack(
children: [
Container(
width: 250,
height: 250,
color: Colors.green[100],
),
Positioned(
bottom: 20,
right: 20,
child: ElevatedButton(
onPressed: () {},
child: Text('Click'),
),
),
],
)
๐ This button always stays 20 pixels from the bottom and right edges.
๐ Key Differences Recap
Feature | Align | Positioned |
---|---|---|
Positioning | Relative (using Alignment ) | Absolute (using pixel values) |
Flexibility | Great for responsive design | Better for fixed layout |
Nesting | Can wrap any widget | Must be a direct child of Stack |
โ ๏ธ Common Mistakes and How to Avoid Them
โ Using Positioned
without Stack
Positioned(
top: 10,
child: Text("Oops")
)
๐ ๏ธ Fix: Always wrap Positioned
in a Stack
.
โ Misaligning with Align
Using wrong alignment values can confuse layouts.
Align(
alignment: Alignment(3, 3),
child: Text('Misplaced'),
)
๐ ๏ธ Fix: Stick to standard values like Alignment.center
, or use Alignment(x, y)
within the range of -1.0 to 1.0.
โ Overlapping multiple children with no clarity
Stack(
children: [
Text('One'),
Text('Two'),
],
)
๐ ๏ธ Fix: Use Align
or Positioned
to place them clearly.
๐งช Bonus Example โ Combining Both
Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Text('Header'),
),
Positioned(
bottom: 10,
right: 10,
child: Text('Footer'),
),
],
)
๐งฉ This gives a clean top and bottom layout inside a Stack.
โ When to Use What?
- Useย
Align
ย when your layout needs to be responsive and relative to parent. - Useย
Positioned
ย when you need precise pixel control over placement.
๐ Thanks for Reading!
If you found this article helpful, consider sharing it or leaving a comment! Your support helps keep the Flutter love flowing. ๐
Happy Fluttering! ๐