Sub topic 13
Putting It Together: A Composed Screen Layout
Assume you know basic Dart syntax, StatelessWidget, and the build method. This chapter combines Row, Column, and Stack into one screen. The main idea is to give each part a clear job:
Columndivides the screen vertically.Rowdivides a section horizontally.Stacklayers widgets inside a bounded area.Expandedshares any remaining space.
Plan the widget tree
A widget tree is the parent-child structure that describes an interface. This screen has a page column, a header, a body row, and two panels:
The Expanded around the body is important. It gives the body a finite height after the header takes its space. The Expanded children inside the body then divide the available width.
Build the composed layout
The following complete example creates a small product screen. The preview uses a Stack for layering. The details panel uses a Column, and its buttons use a Row.
import 'package:flutter/material.dart';
void main() {
runApp(const LayoutExampleApp());
}
class LayoutExampleApp extends StatelessWidget {
const LayoutExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Product preview')),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Featured item',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
IconButton(
onPressed: () {},
icon: const Icon(Icons.favorite_border),
),
],
),
const SizedBox(height: 16),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 3,
child: Stack(
children: [
Positioned.fill(
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.blue.shade100,
borderRadius: BorderRadius.circular(16),
),
child: const Center(
child: Icon(
Icons.headphones,
size: 120,
color: Colors.blue,
),
),
),
),
Positioned(
top: 12,
right: 12,
child: Chip(
label: const Text('New'),
backgroundColor: Colors.white,
),
),
],
),
),
const SizedBox(width: 16),
Expanded(
flex: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Studio headphones',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
const Text(
'Clear sound for work, travel, and music.',
),
const SizedBox(height: 16),
const Text(
'\$129',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () {},
child: const Text('Buy'),
),
),
const SizedBox(width: 8),
IconButton(
onPressed: () {},
icon: const Icon(Icons.more_horiz),
),
],
),
],
),
),
],
),
),
],
),
),
),
),
);
}
}The outer Column receives the screen height from Scaffold. Its header has a natural height, while the body Expanded receives the remaining height. Inside the body, the preview receives three flex parts and the details receive two. Therefore, the preview is wider than the details.
The Stack fills the preview panel because its background is a non-positioned child using Positioned.fill. The chip is painted later, so it appears above the background.
Common composition errors
A Row can overflow when its children need more width than the row receives. Wrap text or a large control in Expanded, then use TextOverflow.ellipsis when truncation is acceptable.
A scrollable widget inside a Column needs a bounded height. Wrap the ListView in Expanded; otherwise, the column may give it unbounded height.
A Stack also needs usable bounds. If every child is positioned and the stack has no explicit size, it may not receive the dimensions you expect. Place it inside Expanded, SizedBox, or another widget that supplies constraints.
Exercise
In the example, what would happen if the body Row were not wrapped in Expanded?
Show answer
The outer Column could give the Row unbounded height. The Row and its Stack might then fail to determine a finite height or produce an unbounded-constraint error. Expanded gives the body the remaining screen height.