Sub topic 12
How a Stack Sizes Itself, Aligns Children, and Clips
Assume you know basic Dart, StatelessWidget, and the build method. This chapter focuses on how Stack chooses its size and places its children.
A Stack paints children on top of one another. Its child list is ordered from back to front: the first child is painted first, and later children appear above it.
How a Stack chooses its size
A Stack usually gets its outer constraints from its parent. It then uses its non-positioned children to determine its own size.
A non-positioned child is a child that is not wrapped in Positioned. With the default StackFit.loose, these children receive loose constraints. The Stack becomes large enough to contain them.
If every child is positioned, no child can determine the Stack's natural size. In that case, the Stack expands to the largest size allowed by its parent.
| Child type | Effect on Stack size |
|---|---|
| Non-positioned | Helps determine the Stack's width and height |
| Positioned | Does not determine the Stack's size |
| All children positioned | Stack uses the largest size allowed by its parent |
For predictable results, give the Stack a size through its parent, such as SizedBox, Container, or a screen layout.
Alignment of non-positioned children
The alignment property controls where non-positioned children go when the Stack is larger than those children. The default is AlignmentDirectional.topStart.
For example, Alignment.center places a smaller child in the center of the Stack. Later children still paint over earlier children.
Stack(
alignment: Alignment.center,
children: [
Container(width: 160, height: 100, color: Colors.blue),
const Text('Centered label'),
],
)Alignment also affects a positioned child along an axis where no position was supplied. For example, a child with left and right set has a horizontal position, but its vertical position can still use the Stack's alignment.
Positioned children and Stack size
Positioned supplies distances from the Stack's edges. You can set left, right, top, and bottom, or provide width and height.
Do not provide conflicting values on one axis. For example, left, right, and width together over-specify the horizontal layout. Use either:
leftandright, which stretch the childleftandwidth, which fix its position and widthrightandwidth, which fix its position and width
Positioned.fill is a shortcut that sets all four edges to zero. It is useful for a background layer.
Clipping overflow
A child can extend beyond the Stack's bounds. The clipBehavior property controls whether that overflow is hidden.
The default behavior is Clip.hardEdge. Use Clip.none when an intentional overlay must remain visible outside the Stack, such as a badge extending beyond a card. Clipping does not fix an incorrectly sized layout. It only controls what is painted.
SizedBox(
width: 200,
height: 120,
child: Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
child: ColoredBox(color: Colors.blue),
),
const Positioned(
right: -12,
top: -12,
child: CircleAvatar(child: Text('3')),
),
],
),
)A complete example
This example gives the Stack a fixed size, fills its background, centers a title, and places an icon in the bottom-right corner.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: StackExample()));
}
class StackExample extends StatelessWidget {
const StackExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
width: 240,
height: 160,
child: Stack(
alignment: Alignment.center,
children: [
Positioned.fill(
child: ColoredBox(color: Colors.indigo),
),
const Text(
'Profile',
style: TextStyle(color: Colors.white, fontSize: 24),
),
const Positioned(
right: 12,
bottom: 12,
child: Icon(Icons.edit, color: Colors.white),
),
],
),
),
),
);
}
}Exercise
Why might a Stack with only Positioned children fill all available space?
Show answer
Positioned children do not determine the Stack's size, so the Stack uses the largest size allowed by its parent.