Learn/Core Widgets/Stack Widget

Stack Widget

Overlay widgets on top of each other

4 min~5 min read+25 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. Positioned Widget
  2. 2. Stack Alignment
  3. 3. Practical Example: Profile with Badge
  4. 4. Layering Without Confusion
  5. 5. This Lesson In Simple Words
  6. 6. Your level focus
  7. 7. One practical habit
  8. 8. Deep Understanding Guide
  9. 9. Why this lesson matters
  10. 10. Study flow for better retention
  11. 11. Quick Glossary

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Stack Widget

The Stack widget places its children on top of each other in layers, similar to how layers work in a design tool. The first child is at the bottom and the last child is on top.

dart
1Stack(
2 children: [
3 // Bottom layer (first)
4 Container(
5 width: 200,
6 height: 200,
7 color: Colors.blue,
8 ),
9 // Top layer (last)
10 Container(
11 width: 100,
12 height: 100,
13 color: Colors.red,
14 ),
15 ],
16)

Positioned Widget

The Positioned widget lets you place a child at specific coordinates within the Stack by setting top, right, bottom, or left values.

dart
1Stack(
2 children: [
3 Container(color: Colors.grey[200]),
4 Positioned(
5 top: 10,
6 right: 10,
7 child: Icon(Icons.close),
8 ),
9 Positioned(
10 bottom: 20,
11 left: 20,
12 child: Text('Hello'),
13 ),
14 Positioned.fill( // Fills the entire stack
15 child: Center(child: CircularProgressIndicator()),
16 ),
17 ],
18)

Stack Alignment

The alignment property controls where non-positioned children are placed within the Stack by default.

dart
1Stack(
2 alignment: Alignment.center, // Aligns non-positioned children
3 children: [
4 Container(width: 200, height: 200, color: Colors.blue),
5 Text('Centered'), // Will be centered
6 ],
7)
8
9// Alignment options:
10// topLeft, topCenter, topRight
11// centerLeft, center, centerRight
12// bottomLeft, bottomCenter, bottomRight

Common Use Cases

💡 Common Use Cases

- Badge on avatar - Text over image - Loading overlay - Floating action button position

Practical Example: Profile with Badge

Here is a common real-world pattern where Stack is used to overlay a verification badge on top of a profile avatar.

dart
1Stack(
2 children: [
3 CircleAvatar(
4 radius: 40,
5 backgroundImage: NetworkImage('https://...'),
6 ),
7 Positioned(
8 bottom: 0,
9 right: 0,
10 child: Container(
11 padding: EdgeInsets.all(4),
12 decoration: BoxDecoration(
13 color: Colors.green,
14 shape: BoxShape.circle,
15 border: Border.all(color: Colors.white, width: 2),
16 ),
17 child: Icon(Icons.check, size: 16, color: Colors.white),
18 ),
19 ),
20 ],
21)

Layering Without Confusion

When using Stack, define a clear layer order:

  1. Background visual layer
  2. Content layer
  3. Interaction overlays (badges, buttons, status)

If layering is unclear, users can miss actions or tap the wrong element. Design with visual and interaction clarity together.

Stack UX Checklist

  • Top interactive elements have enough touch target size.
  • Overlay text remains readable against background images.
  • Badge and action positions stay correct across sizes.
  • Tap areas are not blocked by invisible stacked layers.

This Lesson In Simple Words

Overlay widgets on top of each other. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.

Your level focus

Build confidence with basics first; speed comes later.

One practical habit

Sketch the widget tree first, then code from top to bottom.

Deep Understanding Guide

Why this lesson matters

Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.

Study flow for better retention

  1. Read one section, run one tiny example, then explain the idea in your own words.
  2. When a UI bug appears, inspect constraints and parent-child relations before changing random values.
  3. If you can teach it in 45 seconds without notes, you understood it.

Common Mistakes To Avoid

  • Nesting too many widgets without extracting reusable pieces.
  • Ignoring constraints and guessing with random SizedBox heights.
  • Using one giant build method for unrelated UI sections.
  • Putting heavy calculations directly in build methods.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one card layout with Row/Column and rebuild it using Stack for comparison.
  • Split a large widget into 3 smaller widgets with clear responsibilities.
  • Use const constructors where possible and observe rebuild behavior.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "In a Stack, which child appears on top?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Stack Widget" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Nesting too many widgets without extracting reusable pieces.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

In a Stack, which child appears on top?

Children are stacked in order - last child is on top.

How do you place a widget at specific coordinates in Stack?

Wrap the child in Positioned with top/left/right/bottom values.

What does Positioned.fill() do?

Positioned.fill() sets all four edges to 0, filling the Stack.

What property aligns non-positioned children in a Stack?

Stack's alignment property controls the default alignment of non-positioned children.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue