Sub topic 10
Layering widgets with Stack
This chapter assumes you know basic Dart syntax, StatelessWidget, and the build method. You also need a Flutter project that can run on a device or emulator.
Stack lays widgets on top of one another instead of placing them in a single row or column. The first child is painted first. Later children are painted over it, so child order controls which widget appears in front.
A Stack is useful for badges, labels over images, floating buttons, and decorative overlays. Use Row or Column when children should flow beside or below one another.
A first Stack
The example below creates a 240-by-160 card. The background fills the card, while a badge sits in the top-right corner.
import 'package:flutter/material.dart';
void main() {
runApp(const DemoApp());
}
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Stack example')),
body: Center(
child: SizedBox(
width: 240,
height: 160,
child: Stack(
children: [
Positioned.fill(
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(16)),
),
child: Center(
child: Text(
'Photo',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
),
),
Positioned(
top: 8,
right: 8,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
child: Padding(
padding: EdgeInsets.all(8),
child: Icon(
Icons.favorite,
color: Colors.white,
size: 20,
),
),
),
),
],
),
),
),
),
);
}
}Positioned.fill sets all four edges to zero. Its child therefore receives the entire size of the Stack. The second Positioned gives the icon explicit distances from the top and right edges.
Non-positioned children
A child without Positioned is called a non-positioned child. The alignment property controls where these children go.
Stack(
alignment: Alignment.center,
children: [
Container(width: 120, height: 120, color: Colors.blue),
const Text('Hi'),
],
)Both children are centered. The text is later in the list, so it appears above the blue container.
A Stack normally sizes itself from its non-positioned children. If every child is positioned, the parent must provide a size, such as with SizedBox, Container, or a screen layout constraint.
Common mistakes
- Giving a
Stackonly positioned children inside a parent that provides no finite size can produce an unbounded-constraint error. - Using
leftandrighttogether stretches a child horizontally. Usingtopandbottomstretches it vertically. - A child can draw outside the stack when
clipBehaviorpermits it. If the overflow should be visible, chooseClip.none; otherwise use the default clipping behavior appropriate for the design. - Do not use
Stackto replace normal responsive layout. Fixed offsets can overlap or become unusable on smaller screens.
> callout note > Think of Stack as a set of paint layers. First give the stack a reliable size, then decide which children define the background and which children should be positioned above it.
Exercise
In the example, what changes if the favorite icon is moved before the blue background in the children list?
Show answer
The blue background is painted later and covers the icon, so the icon is no longer visible.