Intermediate · Layouts · topic 19 of 50 · 25 min
Stack, Positioned and Align
Explanation
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.
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),
),
],
)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
| Widget | Use when | Example |
|---|---|---|
| Positioned | You want exact distances from the edges | Positioned(top: 8, right: 8) |
| Positioned.fill | A child should cover the whole Stack | A dark overlay on an image |
| Align | You want a relative spot | Align(alignment: Alignment.bottomCenter) |
| Center | Exactly the middle | A play button over a thumbnail |
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)),
),
),
],
)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: ...).
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 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.
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)),
),
],
)Practice (0/3 done)
In Stack(children: [A, B, C]), which child appears on top?
Which widget makes a dark overlay cover the entire Stack?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Understanding constraints and fixing overflowLayouts · same module
- Positioning and stackingFrontend Web Development · CSS layout and responsive design
- Layouts: Column, Row, Box and ScaffoldKotlin and Android App Development · Jetpack Compose UI
- FlexboxFrontend Web Development · CSS layout and responsive design
- Trees: words and shapesData Structures and Algorithms (DSA) for interviews · Trees and binary search trees
- StacksC Programming (from zero to systems) · Data structures in C
Questions people ask about this
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.
In a Column, what does verticalArrangement control?Related
Answer: Spacing along the vertical (main) axis
Arrangement works on the main axis.
Items overflow on a small phone. Which property lets them move to a new line?Related
Who decides a widget's position?Related
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
- 2 more topics in “Layouts” before its exam: Understanding constraints and fixing overflow, Images, icons, fonts and themes.
- Module 5 exam: Layouts · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- 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