What is Stack in Flutter?
Stack is a layout widget that positions its children relative to its edges. Children can overlap—later children are painted on top of earlier children. This makes Stack perfect for creating overlays, badges, gradients, and complex UI compositions. Stack is based on the Flex widget but doesn't arrange children in a line; instead, it lets you place them anywhere within its bounds.
Basic Usage
To use a Stack, simply give it a list of children. By default, children are placed in the top‑left corner. You can later move them using the Positioned widget. Here’s a minimal example:
This creates three colored squares stacked on top of each other, all aligned to the top‑left corner. The blue square (last child) appears on top of the green and red ones.
Key Properties Explained
Stack provides several properties to control how children are laid out:
- children: The list of widgets to display. Later children are drawn on top.
- alignment: Aligns children that aren’t wrapped in Positioned. Default is
AlignmentDirectional.topStart(top‑left in left‑to‑right locales). You can use values likeAlignment.center,Alignment.bottomRight, etc. - fit: Determines how non‑positioned children size themselves.
StackFit.loose(default) lets them keep their original size;StackFit.expandforces them to expand to the Stack's size. - clipBehavior: Controls whether content that overflows the Stack is clipped. Options:
Clip.none,Clip.hardEdge,Clip.antiAlias,Clip.antiAliasWithSaveLayer. Default isClip.hardEdge. - overflow: Deprecated, use
clipBehaviorinstead.
Alignment Examples
The alignment property affects all children that are not wrapped in Positioned. Here's how to center the non‑positioned children:
Now all squares are centered within the Stack. The largest one (red) determines the Stack's size because it's the biggest child (unless you constrain the Stack externally).
Using Positioned for Precise Control
To place a child at an exact offset from the Stack's edges, wrap it in a Positioned widget. You can set top, right, bottom, left, width, and height. For example:
The yellow container is non‑positioned and will be placed according to the Stack's alignment (top‑left by default). The red one is 20 pixels from the top and left, while the blue one is 30 from the bottom and right.
Stack with Non‑positioned Children
If you mix positioned and non‑positioned children, the non‑positioned ones are aligned using the Stack's alignment and also influence the Stack's size. The Stack sizes itself to contain all non‑positioned children plus any positioned children that have both width/height or are fully constrained. Positioned children without explicit size don't contribute to the Stack's size, so the Stack might shrink to fit only the non‑positioned ones.
Common Use Cases
- Badges and notifications: Place a small circle on top of an icon using Stack and Positioned.
- Overlapping images: Create a photo collage with partially overlapping pictures.
- Gradients overlays: Stack a gradient container above an image.
- Custom progress indicators: Overlay a spinner on top of content.
- Floating action buttons: Simulate a FAB over a bottom sheet.
Common Mistakes Beginners Make
- Forgetting that later children paint on top: Always put the bottommost widget first.
- Not constraining the Stack: If all children are positioned and have no size, the Stack shrinks to zero size and becomes invisible. Provide at least one sized child or wrap the Stack with a SizedBox.
- Misunderstanding
fit: UsingStackFit.expandwithout constraints can cause unexpected expansion. Make sure the parent provides enough space. - Overflow not visible: If a positioned child goes outside the Stack, it may be clipped. Adjust
clipBehaviortoClip.noneto allow overflow (but be careful with layout).
Key Points to Remember
- Stack overlays its children; later children appear on top.
- Use
alignmentto position all non‑positioned children. - Use
Positionedto place a child at specific coordinates. - The Stack sizes itself to contain its non‑positioned children and fully‑constrained positioned children.
- Set
clipBehaviorto control overflow clipping. StackFit.expandforces non‑positioned children to fill the Stack.
Common Interview Questions
- How does Stack determine its own size?
- What's the difference between a positioned and a non‑positioned child in a Stack?
- How can you create a red circle badge in the top‑right corner of an icon using Stack?
- Explain the
fitproperty of Stack and when you would useStackFit.expand. - What happens if you put two
Positionedwidgets with the sametopandleftvalues? Which one appears on top? - How would you make a Stack take the full screen size and then center all its children?