Question #326EasyWidgets & UI

How do you use the Positioned widget in Flutter?

#widget#positioned#stack

Answer

Overview

The

text
Positioned
widget is used inside a
text
Stack
to place a child at a specific position relative to the Stack's edges.


Basic Usage

dart
Stack(
  children: [
    Container(width: 300, height: 200, color: Colors.grey[300]),

    Positioned(
      top: 10,
      left: 10,
      child: Text('Top Left'),
    ),

    Positioned(
      bottom: 10,
      right: 10,
      child: Text('Bottom Right'),
    ),
  ],
)

All Properties

dart
Positioned(
  top: 10,      // Distance from top edge
  bottom: 10,   // Distance from bottom edge
  left: 10,     // Distance from left edge
  right: 10,    // Distance from right edge
  width: 100,   // Explicit width (use instead of left+right)
  height: 50,   // Explicit height (use instead of top+bottom)
  child: Widget(),
)

⚠️ You cannot set both

text
left
+
text
right
AND
text
width
simultaneously. Same for
text
top
+
text
bottom
and
text
height
.


Fill Entire Stack

dart
Stack(
  children: [
    Positioned.fill(
      child: Image.network(url, fit: BoxFit.cover), // Background
    ),
    Positioned(
      bottom: 0, left: 0, right: 0,
      child: Container(
        color: Colors.black54,
        padding: EdgeInsets.all(8),
        child: Text('Caption', style: TextStyle(color: Colors.white)),
      ),
    ),
  ],
)

Practical: Badge on Icon

dart
Stack(
  clipBehavior: Clip.none,
  children: [
    Icon(Icons.notifications, size: 30),
    Positioned(
      top: -4,
      right: -4,
      child: Container(
        padding: EdgeInsets.all(4),
        decoration: BoxDecoration(
          color: Colors.red,
          shape: BoxShape.circle,
        ),
        child: Text('3', style: TextStyle(color: Colors.white, fontSize: 10)),
      ),
    ),
  ],
)

Positioned vs Align

WidgetUse When
text
Positioned
Exact pixel offset from Stack edges
text
Align
Fractional position within the Stack (e.g.,
text
Alignment.center
)
dart
// Align — fractional positioning
Stack(
  children: [
    Align(alignment: Alignment.bottomRight, child: FloatingActionButton(...)),
  ],
)

Key Rule:

text
Positioned
only works as a direct child of
text
Stack
. Outside a Stack it has no effect.