What is the Positioned Widget?
Positioned is a widget that controls where a child of a Stack is placed. It allows you to set absolute coordinates (top, bottom, left, right) relative to the Stack’s edges, as well as optionally specify a width and height. It is only valid when used as a direct child of a Stack. Positioned gives you precise control over the placement of widgets, making it ideal for creating overlays, badges, floating action buttons, and complex UI arrangements.
Basic Usage
Wrap a child with Positioned and provide at least one of top, bottom, left, or right (or a combination). The child will be positioned relative to the corresponding edge of the Stack.
Positioning Properties
The Positioned widget has several properties to control placement and size:
top: Distance from the top edge of theStack.
bottom: Distance from the bottom edge.
left: Distance from the left edge.
right: Distance from the right edge.
width: Explicit width of the child (if both left and right are given, width is ignored).
height: Explicit height of the child (if both top and bottom are given, height is ignored).
If you specify both top and bottom, the widget is vertically constrained between those edges; the height is determined by the space available (or the child's natural height). Similarly, left and right together stretch the widget horizontally. If you set width or height along with opposite edges, the explicit size takes precedence and the edges may be ignored (or cause an assertion if they conflict).
Examples
- Corner Badge
- Stretched Overlay
- Centered Floating Button (without FAB)
Positioned vs Align vs Padding
Positioned: Absolute positioning within aStack. You specify exact pixel offsets from edges. TheStackmust be the parent.
Align: Positions a child within its parent based on a fraction (e.g.,Alignment.topRight). It does not require aStackand is often used withContainerorSizedBox.
Padding: Adds space around the child, but does not change the child's position relative to the parent edges; it only adds empty space inside the parent.
Common Mistakes
- Using
Positionedoutside aStack: This causes a runtime error becausePositionedonly works inside aStack.
- Using
- Specifying conflicting constraints: For example, setting both
leftandrightand alsowidthcan cause an assertion. Flutter prefers the explicitwidth/heightover the edge constraints.
- Specifying conflicting constraints: For example, setting both
- Forgetting to give the
Stacka size: If theStackitself has no size (e.g., no width/height and no positioned children that give it size), the positioned child may not be visible. Ensure theStackhas a bounded size (e.g., via a parentContaineror an expanding child).
- Forgetting to give the
- Not handling overflow: If a
Positionedchild extends beyond theStackbounds, it may be clipped (depending on theStack'sclipBehavior). UseclipBehavior: Clip.noneto allow overflow, but be mindful of UI boundaries.
- Not handling overflow: If a
Key Takeaways
Positionedis used inside aStackto place children at absolute coordinates.
- Specify
top,bottom,left,rightto position relative to theStack's edges.
- Specify
- Use
widthandheightto explicitly size the child.
- Use
- When both opposite edges (e.g.,
leftandright) are given, the child is stretched; explicitwidthoverrides this.
- When both opposite edges (e.g.,
- Always ensure the
Stackhas a defined size, otherwise positioned children may not behave as expected.
- Always ensure the
- For simple alignment without absolute coordinates, consider
AlignorPadding.
- For simple alignment without absolute coordinates, consider