Sub topic 11
Placing children with Positioned
This chapter assumes you know basic Dart, StatelessWidget, the build method, and the Stack widget. A Stack layers children in the same rectangular area. Positioned controls where a child sits inside that area.
Positioning by edges
Positioned accepts top, right, bottom, and left values. Each value is a distance from the matching edge of the Stack.
SizedBox(
width: 240,
height: 160,
child: Stack(
children: [
const Positioned.fill(
child: ColoredBox(color: Color(0xffe0e0e0)),
),
const Positioned(
left: 16,
top: 12,
child: Text('Top left'),
),
const Positioned(
right: 16,
bottom: 12,
child: Icon(
Icons.favorite,
color: Colors.red,
size: 28,
),
),
],
),
)The first child fills the stack and acts as a background. The text is 16 logical pixels from the left edge and 12 from the top. The icon is 16 pixels from the right edge and 12 from the bottom.
A logical pixel is Flutter's layout unit. It is independent of the physical pixel density of the device.
You can set one, two, or three edge values:
leftandrightdetermine the child's width when both are set.topandbottomdetermine the child's height when both are set.- A single edge leaves the child's natural size along that axis.
- Opposite edges and an explicit
widthorheightcan conflict. Avoid specifying all three unless you understand which constraints the render object receives.
`Positioned.fill` and inset layouts
Positioned.fill is shorthand for setting left, right, top, and bottom to zero. It is useful for backgrounds, overlays, and hit-test areas.
You can give it an inset to leave a border around the child:
Positioned.fill(
left: 12,
top: 12,
right: 12,
bottom: 12,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(12)),
),
),
)Positioned only works as a direct child of Stack. To position a widget inside a Column or Row, use their alignment and flex widgets instead. Wrapping Positioned in another widget removes the relationship that Stack needs.
Alignment and positioning trade-offs
A non-positioned child can use the stack's alignment property. This is better when the child should remain centered or aligned consistently without fixed edge distances.
Stack(
alignment: Alignment.center,
children: [
Container(
width: 120,
height: 120,
color: Colors.blue,
),
const Text(
'Loading',
style: TextStyle(color: Colors.white),
),
],
)Children are painted in list order. Later children appear on top of earlier children. This lets you place badges, labels, and controls over images. However, a visually topmost widget can still receive pointer events according to the hit-testing behavior of its render object, so test overlapping interactive controls carefully.
Common errors
A Stack needs a meaningful size. If every child is non-positioned and has no usable size, the stack may not occupy the area you expect. Give the stack a parent with dimensions, such as SizedBox, or include a non-positioned child that establishes its size.
Positioned values are not responsive by themselves. A fixed right: 8 works across widths, but a fixed left: 180 may collide with text on a narrow screen. Prefer constraints, alignment, and flexible widgets for content that must adapt.
Exercise
Why does a child with Positioned(left: 16, right: 16) usually stretch across the available width?
Show answer
The Stack gives the child horizontal constraints from both edges, so its width becomes the stack's width minus 32 logical pixels.