Skip to content

Intermediate · Layouts · topic 19 of 50 · 25 min

Stack, Positioned and Align

Explanation

Stack places children on top of each other, the last child on top. Position children with Positioned (distances from the Stack's edges) or Align (a relative position such as Alignment.topRight). Use Stack for badges on icons, text over images and floating labels; set fit and clipBehavior to control sizing and overflow.

Why layering?

Some designs put things on top of each other: a "3" badge on a cart icon, a discount label on a product photo, a play button over a video thumbnail, a gradient behind text on a banner. Rows and Columns cannot overlap children; Stack can.

dart
Stack(
  children: [
    Image.network('https://example.com/saree.jpg', height: 200, width: double.infinity, fit: BoxFit.cover),
    Positioned(
      top: 8,
      left: 8,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
        color: Colors.red,
        child: const Text('30% OFF', style: TextStyle(color: Colors.white)),
      ),
    ),
    const Positioned(
      bottom: 8,
      right: 8,
      child: Icon(Icons.favorite_border, color: Colors.white),
    ),
  ],
)
The image is drawn first (bottom); the label and heart icon are drawn on top.

How Stack sizes itself

Children of a Stack are either positioned (wrapped in Positioned) or non-positioned. The Stack sizes itself to fit its largest non-positioned child; positioned children are then placed relative to the Stack's edges. The fit property changes how non-positioned children are sized: StackFit.loose (default) lets them be their natural size, StackFit.expand forces them to fill the Stack.

Positioned takes any of top, bottom, left and right (and width or height). Giving both left and right stretches the child between them; giving only top and right pins it to that corner. Positioned must be a direct child of a Stack; anywhere else it causes a ParentDataWidget error, just like Expanded outside a Row or Column.

Keep Stacks small and local, such as one card or one banner. Building a whole screen with Positioned and fixed numbers looks right on one phone and breaks on others, because screen sizes differ.

Positioned versus Align

WidgetUse whenExample
PositionedYou want exact distances from the edgesPositioned(top: 8, right: 8)
Positioned.fillA child should cover the whole StackA dark overlay on an image
AlignYou want a relative spotAlign(alignment: Alignment.bottomCenter)
CenterExactly the middleA play button over a thumbnail
dart
Stack(
  alignment: Alignment.center,                 // default position for non-positioned children
  children: [
    const Icon(Icons.shopping_cart, size: 36),
    Align(
      alignment: Alignment.topRight,
      child: Container(
        padding: const EdgeInsets.all(4),
        decoration: const BoxDecoration(color: Colors.red, shape: BoxShape.circle),
        child: const Text('3', style: TextStyle(color: Colors.white, fontSize: 10)),
      ),
    ),
  ],
)
A cart badge. Material 3 also offers a ready-made Badge widget: Badge(label: Text("3"), child: Icon(Icons.shopping_cart)).

Clipping

By default, a Stack clips children that are positioned outside its bounds (clipBehavior: Clip.hardEdge). If you want a badge to stick out beyond the icon's edge, set clipBehavior: Clip.none. Rounded images are usually wrapped in ClipRRect(borderRadius: ...).

Think of it like this: A Stack is like a pile of transparent sheets on an overhead projector. The bottom sheet is the photo, the next sheet has the price label in one corner, and the top sheet has the heart icon. Each sheet is placed precisely, and you see them all at once.

Common mistakes

  • A Stack with only positioned children has no size reference and may collapse; give it a size (SizedBox) or a non-positioned child.
  • Expecting a badge outside the bounds to show: set clipBehavior: Clip.none.
  • Using Stack for things a Row or Column can do: overlapping layouts are harder to make responsive.
  • Forgetting that later children are drawn on top: order matters.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Stack, Positioned and Align · 1/6

Stack = layers

Stack places children on top of each other. The last child is drawn on top.

Prefer a video by a teacher? Find free videos on “flutter stack positioned align badge overlay tutorial” on YouTube ↗

In real life: A video thumbnail in an e-learning app

An e-learning app shows each lesson as a thumbnail image with a semi-transparent dark overlay, a centred play button and the duration in the bottom-right corner. A Stack with Positioned.fill for the overlay, Center for the play icon and Positioned for the duration creates the familiar look.

dart
Stack(
  children: [
    Image.asset('assets/images/lesson1.jpg', fit: BoxFit.cover, width: double.infinity, height: 180),
    Positioned.fill(child: Container(color: Colors.black26)),
    const Positioned.fill(child: Center(child: Icon(Icons.play_circle_fill, size: 56, color: Colors.white))),
    const Positioned(
      right: 8,
      bottom: 8,
      child: Text('12:45', style: TextStyle(color: Colors.white)),
    ),
  ],
)
Bottom to top: image, overlay, play button, duration.

Practice (0/3 done)

Task 1 · Question

In Stack(children: [A, B, C]), which child appears on top?

Task 2 · Question

Which widget makes a dark overlay cover the entire Stack?

Task 3 · Practice

Build a restaurant banner: a Stack with an image (or a coloured Container), a gradient overlay from transparent to black at the bottom (Positioned.fill with a BoxDecoration gradient), the restaurant name and rating in the bottom-left with Positioned, and a "Pure Veg" badge in the top-right with Align.

Quick quiz

Question 1

1.How does a Stack decide its size?

Question 2

2.Which property lets children draw outside the Stack's bounds?

Question 3

3.What is a good use of Stack?

Question 4

4.Which ready-made Material widget shows a count on an icon?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

How does a Stack decide its size?

Answer: From its largest non-positioned child

Positioned children do not affect its size.

Which property lets children draw outside the Stack's bounds?

Answer: clipBehavior: Clip.none

The default clips.

What is a good use of Stack?

Answer: A badge over an icon

Overlapping content needs Stack.

Which ready-made Material widget shows a count on an icon?

Answer: Badge

Badge(label: ..., child: ...).

What is the default position value?Related

Answer: static

Every element starts as position: static, in the normal flow.

From Positioning and stacking

In a Column, what does verticalArrangement control?Related

Answer: Spacing along the vertical (main) axis

Arrangement works on the main axis.

From Layouts: Column, Row, Box and Scaffold

Items overflow on a small phone. Which property lets them move to a new line?Related

Answer: flex-wrap: wrap

flex-wrap: wrap lets items flow onto more lines.

From Flexbox

Who decides a widget's position?Related

Answer: Its parent

Parent sets position.

From Understanding constraints and fixing overflow

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Layouts” before its exam: Understanding constraints and fixing overflow, Images, icons, fonts and themes.
  2. Module 5 exam: Layouts · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Lists, navigation and forms. 50 topics left in the course before the final exam.

← Previous: Container, Padding, SizedBox, Expanded and Flexible