Positioned
Places its child in a Stack at absolute coordinates: offsets from the edges and/or an explicit size. Used for badges, "close" buttons, captions over an image.
Type:
single-child
Stack
SizedBox width:200 height:120
ColoredBox color:#36345E
Positioned top:8 right:8
Text "NEW"
Works
only
directly inside a
Stack
.
Parameters
| Parameter | Type | Description |
|---|---|---|
top
|
length |
Offset from the top edge of the
Stack
|
bottom
|
length | Offset from the bottom edge |
left
|
length | Offset from the left edge |
right
|
length | Offset from the right edge |
width
|
length | Width |
height
|
length | Height |
All parameters are optional; sides that are not set stay free. A length is a number (pixels) or a string with a unit:
"50%"
,
"10svh"
(a percentage is taken along its own axis, as in CSS:
left
/
right
of the width,
top
/
bottom
of the height).
Setting both
left
and
right
(or
top
and
bottom
) stretches the child across the whole distance between them — then you do not need to write the width (height).
Examples
A caption over an image
Stack
SizedBox width:320 height:180
Image src:"https://example.com/poster.jpg" fit:cover
Positioned left:12 right:12 bottom:12
Text "Movie title"
A full-width shade
Stack
SizedBox width:320 height:180
ColoredBox color:#585593
Positioned bottom:0 left:0 right:0 height:60
ColoredBox color:#99000000
A close button in the corner
@opened:true
Stack
SizedBox width:240 height:140
ColoredBox color:#1e1b4b
Positioned top:4 right:4
InkWell onTap:{opened = false}
Padding
#padding all 8
Icon close
Differences between the app and the site
On the site
Positioned
always becomes an absolutely positioned block; if the child takes the whole area, it fills it (as in Flutter, where a child with a size gets tight constraints). There are no other differences.
Bad practices
Positioned outside a Stack
// BAD — Positioned outside a Stack makes no sense: in Flutter it is an error
Column
Positioned top:8 left:8
Text "Badge"
// GOOD — the parent is a Stack
Stack
SizedBox width:100 height:100
ColoredBox color:#36345E
Positioned top:8 left:8
Text "Badge"
Best practices
-
Set the size of the
Stackwith an ordinary (non-Positioned) child — aSizedBoxor an image. The size of aStackis defined by its non-positioned children; without one,Positionedhas nothing to measure from. -
For simple alignment of one element (centered, in a corner)
alignmenton theStackor Align is enough —Positionedis for when you need exact offsets.