Stack lets you place widgets on top of each other like layers. Positioned helps you put these widgets exactly where you want inside the Stack.
Stack and Positioned in Flutter
Stack(
children: [
Widget1(),
Positioned(
top: 10.0,
left: 20.0,
child: Widget2(),
),
],
)Stack's children are drawn in order, so later children appear on top.
Positioned requires at least one of top, bottom, left, or right to place the child.
Stack(
children: [
Container(color: Colors.blue, width: 100, height: 100),
Positioned(
top: 10,
left: 10,
child: Container(color: Colors.red, width: 50, height: 50),
),
],
)Stack(
children: [
Text('Hello'),
Positioned(
bottom: 0,
right: 0,
child: Icon(Icons.star),
),
],
)Stack(
children: [
Container(color: Colors.green, width: 100, height: 100),
// No Positioned, child fills Stack by default
Container(color: Colors.yellow.withOpacity(0.5), width: 50, height: 50),
],
)This app shows a blue square with a smaller red square positioned inside it near the top-left corner. The text 'Top Layer' is positioned near the bottom-right corner on top of both squares.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Stack and Positioned Example')), body: Center( child: Stack( children: [ Container( width: 150, height: 150, color: Colors.blue, ), Positioned( top: 20, left: 20, child: Container( width: 100, height: 100, color: Colors.red, ), ), Positioned( bottom: 10, right: 10, child: const Text( 'Top Layer', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold), ), ), ], ), ), ), ); } }
Stack paints children in order, so later children appear on top.
Positioned widgets must have at least one position property: top, bottom, left, or right.
Using Stack and Positioned is great for overlapping widgets or custom layouts.
Stack lets you layer widgets on top of each other.
Positioned places a child widget at a specific position inside the Stack.
Use Stack and Positioned to create complex, overlapping UI elements.