Understanding Align vs Positioned in Flutter Stack (With New Examples)

By | April 20, 2025

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.centerAlignment.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

FeatureAlignPositioned
PositioningRelative (using Alignment)Absolute (using pixel values)
FlexibilityGreat for responsive designBetter for fixed layout
NestingCan wrap any widgetMust 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! ๐Ÿš€

Leave a Reply

Your email address will not be published. Required fields are marked *