Sub topic 3
Placing children with Positioned
This chapter assumes you know basic Dart, StatelessWidget, and build, and that you can run a Flutter app. You should already be comfortable with Row and Column.
A Stack draws its children on top of each other. Each child is either positioned or non-positioned. Positioned is a widget that pins a child to an edge of the stack using top, right, bottom, or left. Non-positioned children are placed using the stack's alignment property.
Placing children with Positioned
import 'package:flutter/material.dart';
class CardPhoto extends StatelessWidget {
const CardPhoto({super.key});
@override
Widget build(BuildContext context) {
return SizedBox(
width: 200,
height: 200,
child: Stack(
children: [
Positioned.fill(child: ColoredBox(color: Colors.grey)),
const Positioned(
right: 8,
bottom: 8,
child: Icon(Icons.favorite, color: Colors.red),
),
],
),
);
}
}Positioned.fill stretches its child to the full size of the stack. The icon sits 8 pixels from the bottom-right corner. Later children paint over earlier ones, so the icon appears on top of the background.
How a Stack sizes itself, aligns children, and clips
A Stack sizes itself to its non-positioned children. Positioned children do not affect its size. If every child is positioned, the stack takes the largest size its constraints allow. Wrap it in a SizedBox when you need an exact size.
Non-positioned children are aligned with alignment, which defaults to AlignmentDirectional.topStart. Content that extends beyond the stack's bounds is clipped by default. Set clipBehavior: Clip.none to let it paint outside.
flowchart TD
S[Stack] --> B[Background with Positioned.fill]
S --> I[Icon with right and bottom offsets]
S --> T[Non-positioned child placed by alignment]Putting it together: a composed screen layout
Real screens combine these widgets. A common pattern is a Column with a header, an Expanded body, and a footer, where the body is a Stack holding a background image and a floating button. Each widget handles one job. Column handles the vertical split, Expanded gives the body the leftover height, and Stack and Positioned place the button.
When Row and Column are not enough: Wrap and scrolling
Row and Column do not wrap. When items overflow the main axis, they show an overflow error. Use Wrap for items that should flow onto a new line:
Wrap(
spacing: 8,
runSpacing: 8,
children: [
for (final tag in ['Flutter', 'Dart', 'Layouts', 'Stack'])
Chip(label: Text(tag)),
],
)Use a scrollable widget such as ListView for long lists. Remember that a ListView inside a Column needs an Expanded around it.
Inspecting layouts with Flutter DevTools
Flutter DevTools is a set of debugging tools that runs in a browser. Open the Widget Inspector and select a widget on the device or in the tree. The Layout Explorer panel shows that widget's constraints, its size, and the sizes of its children. Use it to find which widget is causing an overflow or an unexpected size.
Exercise:
Exercise
A Stack contains only Positioned children. How does the stack size itself?
Show answer
It takes the largest size its constraints allow, because positioned children do not affect its size. Wrap it in a SizedBox to set an exact size.