Sub topic 3
Scaffold: the Structure of a Screen
This chapter assumes basic Dart syntax, a working Flutter SDK, and familiarity with StatelessWidget and the build method.
A Scaffold is a Material Design layout widget. It provides standard places for the main parts of a screen, such as the top app bar, page content, navigation drawer, and floating action button. It does not decide what your screen means. It gives those parts a reliable structure.
How Scaffold Fits in the Widget Tree
A typical screen nests Scaffold below MaterialApp:
MaterialApp
└── HomePage
└── Scaffold
├── AppBar
├── body
├── Drawer
└── floatingActionButtonMaterialApp provides app-wide services, including the theme and navigation. The screen widget chooses a Scaffold. The Scaffold then arranges the screen's visible regions.
The Main Scaffold Slots
The most important Scaffold properties are:
| Property | Purpose |
|---|---|
| `appBar` | Places a bar at the top of the screen |
| `body` | Holds the primary screen content |
| `drawer` | Provides navigation from the left side |
| `floatingActionButton` | Adds a prominent circular action |
| `bottomNavigationBar` | Places navigation or actions at the bottom |
Each property accepts a widget. The body property is usually required for useful screens, but it can contain any widget, including a Column, ListView, or custom layout.
A Complete Screen Example
The following app shows how these slots work together:
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(
title: 'Scaffold Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const [
DrawerHeader(
child: Text('Navigation'),
),
ListTile(
leading: Icon(Icons.inbox),
title: Text('Inbox'),
),
],
),
),
body: const Center(
child: Text('Your messages appear here'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Compose pressed')),
);
},
child: const Icon(Icons.edit),
),
);
}
}The AppBar occupies the top area. The body fills the remaining primary area. The drawer is hidden until the user opens it, usually by tapping the menu icon that Flutter adds to the AppBar. The floating action button sits above the body near the lower edge.
How Scaffold Handles Layout
Scaffold accounts for system areas such as the status bar and, in many cases, the device's bottom navigation area. Its layout also positions the floating action button relative to the body and bottom widgets.
A common mistake is to put a second Scaffold inside the body of the first one. This can produce duplicate app bars, incorrect drawer behavior, or confusing SnackBar placement. Usually, one screen should have one main Scaffold.
Another common mistake is placing a large, scrolling child inside a Column without giving it a bounded height. Use Expanded around the scrolling child:
Scaffold(
body: Column(
children: [
const Text('Messages'),
Expanded(
child: ListView(
children: const [
ListTile(title: Text('First message')),
ListTile(title: Text('Second message')),
],
),
),
],
),
)Expanded tells the list to use the remaining height instead of asking for unlimited space.
Exercise
Exercise
Where should the primary content of a screen go, and why should you usually avoid nesting another Scaffold inside it?
Show answer
Put the primary content in the outer Scaffold's body. A second Scaffold can create duplicate screen structure and confusing behavior for app bars, drawers, and SnackBars.