Back to quest
Flutter Widgets BasicLevel 6 / 7

Stack & Positioning

Learn how to layer widgets on top of each other using Stack.

The Stack widget allows you to place multiple children relative to the edges of the box. The class is useful if you want to overlap several children in a simple way.

Stack Widget

Children are painted in the order they are added. The first child is painted at the bottom (back).

Simple Overlap

example.dart
1 
2Stack(
3 alignment: Alignment.center,
4 children: [
5 Container(
6 width: 200,
7 height: 200,
8 color: Colors.blue,
9 ),
10 Container(
11 width: 100,
12 height: 100,
13 color: Colors.red,
14 ),
15 Text('On Top', style: TextStyle(color: Colors.white)),
16 ],
17)
18

HintThis stack centers a red box on top of a blue box, with text on the very top.

Positioned Widget

Use Positioned to specify where a child is placed inside the Stack relative to the edges (top, bottom, left, right).

Using Positioned

example.dart
1 
2Stack(
3 children: [
4 Container(color: Colors.green),
5 Positioned(
6 bottom: 20,
7 right: 20,
8 child: FloatingActionButton(onPressed: () {}),
9 ),
10 ],
11)
12

HintThis positions a Floating Action Button specifically 20 pixels from the bottom and right edges of the Stack.

Loot collected

Key takeaways
  • Stack allows for overlapping widgets.
  • Widgets are painted in order (bottom-up).
  • Use Positioned to place children exactly where you want relative to the boundaries.
Party chat

Comments