Sub topic 6
Filling the Scaffold: body and floating action button
This chapter assumes you know basic Dart classes, named parameters, StatelessWidget, and the build method. You also need a working Flutter project.
The `body` slot
Scaffold provides the main layout for one screen. Its body property holds the screen's primary content. The value must be a widget, so you can place a single widget such as Text, or a layout widget such as Column, Row, ListView, or CustomScrollView.
The Scaffold passes available screen space to its body. Your body widget then lays out its children within those constraints.
flowchart TD
A[MaterialApp] --> B[HomePage]
B --> C[Scaffold]
C --> D[AppBar]
C --> E[body]
C --> F[floatingActionButton]
E --> G[Main content]
F --> H[User action]The body is not automatically a scrolling area. If its content can become taller than the screen, use a widget such as ListView or SingleChildScrollView.
Choosing a body layout
Use the layout widget that matches the content:
| Content | Useful body widget |
|---|---|
| One centered message | `Center` |
| A short vertical group | `Column` |
| A long vertical list | `ListView` |
| A grid of items | `GridView` |
| Scrollable sections with headers | `CustomScrollView` |
When a Column contains a child that should take the remaining height, wrap that child in Expanded. This is common when a screen has a heading above a list.
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text(
'Inbox',
style: TextStyle(fontSize: 24),
),
),
Expanded(
child: ListView(
children: const [
ListTile(title: Text('Welcome')),
ListTile(title: Text('Project update')),
],
),
),
],
),Without Expanded, the ListView may not receive a useful height because Column measures its children differently from the screen.
Adding a floating action button
The floatingActionButton property places a floating action button above the body. A FloatingActionButton is a button designed for the screen's main action, such as creating a message or adding an item.
Its onPressed callback runs when the user taps it. Its child usually contains an Icon.
floatingActionButton: FloatingActionButton(
onPressed: () {
// Start the primary action.
},
tooltip: 'Compose',
child: const Icon(Icons.edit),
),A Scaffold chooses a default position, usually near the lower-right corner. Use floatingActionButtonLocation when the default does not fit your design.
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,Keep the button's action focused. If a screen has several equally important actions, consider placing them in the AppBar, a toolbar, or the body instead.
A complete example
The following app combines a body, a scrollable list, and a floating action button. The callback displays a SnackBar through ScaffoldMessenger, which is available below MaterialApp.
import 'package:flutter/material.dart';
void main() {
runApp(const InboxApp());
}
class InboxApp extends StatelessWidget {
const InboxApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Inbox',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true,
),
home: const InboxPage(),
);
}
}
class InboxPage extends StatelessWidget {
const InboxPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
),
body: ListView.builder(
padding: const EdgeInsets.symmetric(vertical: 8),
itemCount: 20,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
child: Text('${index + 1}'),
),
title: Text('Message ${index + 1}'),
subtitle: const Text('Tap the compose button to add a message.'),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Compose tapped')),
);
},
tooltip: 'Compose',
child: const Icon(Icons.edit),
),
);
}
}The ListView.builder creates list rows as needed. This is more suitable than manually placing many children when the list may grow. The Scaffold keeps the button separate from the body, so the button remains positioned as the body scrolls.
Exercise
Why is Expanded useful when a ListView appears below other children in a Column?
Show answer
It gives the ListView the remaining available height, allowing it to scroll instead of receiving an unbounded or unusable height.