Sub topic 11
Placing children with Positioned
This chapter assumes you know how a Stack layers children and sizes itself, and that you understand box constraints (constraints go down, sizes go up). Here you will learn how Positioned pins a child to the edges of a Stack, which combinations of properties are legal, and why the common errors happen.
What Positioned actually does
Positioned is not a layout widget. It draws nothing and has no size of its own. It is a parent-data widget. It writes left, top, right, bottom, width, and height into the parent data of its child. The Stack's render object reads those values during layout and uses them to size and place that child.
This explains the most important rule. A Positioned must be a descendant of a Stack with only stateless or stateful widgets in between. Those widgets may build a Positioned as the root of their own build method, because they do not add a render object of their own. Any widget that creates a render object between the two, such as Padding, Expanded, or Align, breaks the link.
Edge offsets and sizes
Each offset is a distance from the matching edge of the Stack, measured inward.
left: 8puts the child's left edge 8 logical pixels from the Stack's left edge.bottom: 8puts the child's bottom edge 8 pixels above the Stack's bottom edge.widthandheightset the child's size directly.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(
home: Scaffold(body: Center(child: BadgeCard())),
));
class BadgeCard extends StatelessWidget {
const BadgeCard({super.key});
@override
Widget build(BuildContext context) {
return SizedBox(
width: 240,
height: 160,
child: Stack(
children: [
Positioned.fill(
child: ColoredBox(color: Colors.indigo.shade100),
),
const Positioned(
top: 8,
right: 8,
child: Chip(label: Text('New')),
),
const Positioned(
left: 12,
right: 12,
bottom: 12,
child: Text(
'This caption stretches between the left and right offsets',
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
],
),
);
}
}The SizedBox gives the Stack a size. The first Positioned.fill is shorthand for left: 0, top: 0, right: 0, bottom: 0, so it stretches the background over the whole Stack. The chip sets two offsets, so it keeps its natural size and sits in the top-right corner. The caption sets left and right, so its width is forced to the Stack width minus 24 pixels. Its height stays natural, and the text wraps and truncates inside that width.
How the child's constraints are built
The Stack gives each positioned child constraints based on which properties you set, one axis at a time.
| Properties set on an axis | Constraint the child receives on that axis |
|---|---|
| Neither edge nor size | Loose, from 0 up to the Stack's size (the same as a non-positioned child) |
| One edge only (for example `left`) | Loose, from 0 up to the Stack's size |
| `width` only, or `width` plus one edge | Tight at `width` |
| Both edges (`left` and `right`) | Tight at Stack width minus both offsets |
The second row can surprise you. A child positioned with only left and top is still allowed to grow up to the full Stack size. It is not limited to the space to the right of left. If the child is wider than the remaining space, it will extend past the Stack's edge.
A tight constraint means the child cannot choose its own size. For example, Positioned(left: 0, right: 0, child: Container(width: 50)) produces a full-width container, because the parent's tight constraint overrides the child's request. This is the same rule you saw in the constraints chapter.
Legal combinations
On each axis you may set at most two of the three values: left, right, width (and top, bottom, height vertically). Setting all three over-constrains the child, because the third value could contradict the first two. Flutter rejects it in debug mode with an assertion failure.
// Throws an assertion error in debug mode: left, right, and width.
Positioned(
left: 10,
right: 10,
width: 100,
child: Text('Over-constrained'),
)The legal patterns follow.
- One edge plus a size:
leftandwidthpin the child to a spot with a fixed width. - Two edges:
leftandrightstretch the child. - One edge only: the child keeps its natural size and is anchored to that edge.
- Nothing on an axis: the child is placed at 0 on that axis, which is the top or left edge.
That last point matters. If you set only bottom: 8, the child is anchored to the bottom vertically, but horizontally it sits at left: 0 because nothing was specified. If you want it centered horizontally, do not use Positioned offsets for that. Use the Stack's alignment, or wrap the child in an Align that you place inside a Positioned.fill.
Constructor variants
| Constructor | Use it when |
|---|---|
| `Positioned(...)` | You know offsets and sizes in logical pixels. |
| `Positioned.fill(...)` | The child should cover the whole Stack. All four edges default to 0, and you can override any of them. |
| `Positioned.fromRect(rect: ...)` | You have a `Rect` computed elsewhere, such as a hit area. |
| `Positioned.directional(textDirection: ..., start: ..., end: ...)` | The layout must flip in right-to-left languages. |
For apps that ship in Arabic or Hebrew, prefer Positioned.directional over left and right. It maps start and end to the correct physical side for the given TextDirection. Pass Directionality.of(context) to follow the ambient direction.
Positioned and the size of the Stack
Positioned children do not contribute to the Stack's size. With the default StackFit.loose, the Stack sizes itself to fit its non-positioned children. If there are none, it expands to the largest size its constraints allow.
That second case causes a common error. If every child is positioned and the Stack sits in an unbounded space, for example directly inside a Column or a ListView, the "largest size" is infinite. You get an unbounded-height error or an invisible layout. Give the Stack a bounded size with SizedBox, AspectRatio, or Expanded, or include one non-positioned child that defines the size.
// A non-positioned child defines the size. The Positioned items overlay it.
Stack(
children: [
Image.network('https://example.com/photo.jpg', width: 300, height: 200),
const Positioned(
right: 8,
bottom: 8,
child: Icon(Icons.favorite, color: Colors.red),
),
],
)Overflowing the Stack on purpose
Offsets can be negative. A badge that sticks out of the corner of an icon is a typical use. By default a Stack clips children that leave its bounds (Clip.hardEdge), so you must turn clipping off.
Stack(
clipBehavior: Clip.none,
children: [
const Icon(Icons.notifications, size: 32),
Positioned(
right: -4,
top: -4,
child: CircleAvatar(
radius: 8,
backgroundColor: Colors.red,
child: const Text('3', style: TextStyle(fontSize: 10)),
),
),
],
)Here the Stack is 32 by 32 pixels, the size of the icon, and the badge hangs 4 pixels outside the top-right corner.
Errors you will meet
The error "Incorrect use of ParentDataWidget" means a Positioned has no Stack as its closest render-object ancestor. Typical causes are:
Positionedplaced insidePadding,Center,Align, orExpandedthat is itself inside the Stack. MovePositionedup so it is the direct child ofStack, and put the wrapper insidePositioned.Positionedused in aRoworColumnby mistake. Those parents expectExpandedorFlexible.- A conditional such as
if (show) Positioned(...)is fine, because it adds or omits a child. But wrapping it in another widget such asVisibilityis not fine. PutVisibilityinside thePositionedinstead.
// Wrong: Padding sits between Stack and Positioned.
Stack(children: [
Padding(
padding: EdgeInsets.all(8),
child: Positioned(top: 0, child: Text('Oops')),
),
])
// Right: Positioned is the direct child, and Padding is inside it.
Stack(children: [
Positioned(
top: 0,
child: Padding(padding: EdgeInsets.all(8), child: Text('Fixed')),
),
])Choosing how to place a child
Use alignment when several children share one anchor point. Use Positioned when each child needs its own distances from the edges, or when you want a child stretched between two edges. For movement, AnimatedPositioned takes the same properties and animates between values over a duration.
Exercise
A Stack contains Positioned(top: 0, bottom: 0, left: 0, width: 80, child: ColoredBox(color: Colors.teal)). What size is the teal box, and why is this combination legal when left, right, and width together are not?
Show answer
The box is 80 pixels wide and as tall as the Stack. Horizontally it sets left and width (two of the three values), so the width is tight at 80. Vertically it sets top and bottom (two edges), so the height is tight at the Stack height. Setting all three of left, right, and width would over-constrain the horizontal axis, so that is the illegal combination.
Exercise
You place a badge with Positioned(right: -6, top: -6, ...) on a 40 by 40 Stack, but the badge appears cut off. What do you change, and what limitation remains for taps on the outside part?
Show answer
Set clipBehavior: Clip.none on the Stack, because the default Clip.hardEdge clips anything outside the Stack's bounds. The limitation is that hit testing stops at the Stack's own bounds, so the part of the badge outside the 40 by 40 area still paints but does not receive taps. To make it tappable, enlarge the Stack, for example by padding it, so the badge lies inside its bounds.