Flutter Stack & Positioned
Row and Column place children next to each other. Sometimes you need widgets on top of each other instead: a caption over a photo, a badge on an icon, a button floating over a map. That is what Stack is for.
Layering with Stack
Children of a Stack are painted in order: the first child is at the bottom, the last child on top.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: Layers()))));
class Layers extends StatelessWidget {
const Layers({super.key});
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: [
Container(width: 200, height: 200, color: Colors.indigo),
Container(width: 140, height: 140, color: Colors.indigo.shade300),
Container(width: 80, height: 80, color: Colors.indigo.shade100),
const Text('Top', style: TextStyle(fontWeight: FontWeight.bold)),
],
);
}
}The alignment property positions every child that is not wrapped in Positioned. The default is AlignmentDirectional.topStart (top-left in left-to-right languages). A Stack sizes itself to its largest non-positioned child.
Exact placement with Positioned
Wrap a child in Positioned to pin it to the stack's edges with top, bottom, left, right, width and height:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: PhotoCard()))));
class PhotoCard extends StatelessWidget {
const PhotoCard({super.key});
@override
Widget build(BuildContext context) {
return SizedBox(
width: 300,
height: 200,
child: Stack(
children: [
Positioned.fill(
child: Container(
decoration: BoxDecoration(
gradient: const LinearGradient(colors: [Colors.teal, Colors.lightGreen]),
borderRadius: BorderRadius.circular(16),
),
),
),
const Positioned(
left: 16,
bottom: 16,
child: Text('Ha Long Bay',
style: TextStyle(color: Colors.white, fontSize: 22, fontWeight: FontWeight.bold)),
),
Positioned(
top: 12,
right: 12,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(12)),
child: const Text('★ 4.9'),
),
),
],
),
);
}
}Positioned.fill is shorthand for top: 0, bottom: 0, left: 0, right: 0 — the child covers the whole stack.
| Positioned properties | Result |
|---|---|
top: 0, left: 0 |
Top-left corner |
bottom: 8, right: 8 |
8 px from the bottom-right corner |
left: 0, right: 0, bottom: 0 |
Stretched along the bottom edge |
top: 10, left: 10, width: 50 |
Fixed width, positioned from top-left |
Note: Don't set
left,rightandwidthtogether (ortop,bottomandheight) — that over-constrains the child and triggers an assertion.
A notification badge
A very common pattern: a small counter on top of an icon. clipBehavior: Clip.none allows the badge to stick out beyond the stack's bounds.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: CartIcon(count: 3)))));
class CartIcon extends StatelessWidget {
const CartIcon({super.key, required this.count});
final int count;
@override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
children: [
const Icon(Icons.shopping_cart_outlined, size: 40),
if (count > 0)
Positioned(
right: -6,
top: -6,
child: Container(
padding: const EdgeInsets.all(5),
decoration: const BoxDecoration(color: Colors.red, shape: BoxShape.circle),
child: Text('$count', style: const TextStyle(color: Colors.white, fontSize: 12)),
),
),
],
);
}
}The collection if inside children adds the badge only when there is something to show — no extra widgets for an empty cart.
Tip: Material 3 already includes a ready-made
Badgewidget:Badge(label: Text('3'), child: Icon(Icons.shopping_cart)). KnowingStacklets you build any custom overlay it can't.
Related widgets
IndexedStackshows only one child at a time (by index) but keeps all of them alive — handy for tab pages that should keep their scroll position.Aligninside aStackpositions a child by fractional alignment instead of pixel offsets.
Test yourself
In a Stack, which child is drawn on top?
- The first child
- The last child
- The largest child
- The child with the highest elevation
What does Positioned.fill do?
- Fills the child with a colour
- Pins the child to all four edges of the Stack
- Centers the child
- Makes the Stack fill the screen
Which Stack property lets a positioned child extend outside the stack without being cut off?
- overflow: visible
- clipBehavior: Clip.none
- fit: StackFit.expand
- alignment: Alignment.outside