Sub topic 2
Customizing the AppBar, Filling the Scaffold, and Theming with ThemeData
This chapter assumes you know Dart classes and named parameters, can run a Flutter app, and understand StatelessWidget and build. You should also be comfortable with Scaffold and AppBar from the previous chapter.
Customizing the AppBar: title, leading, and actions
An AppBar has three slots you can fill. The leading slot sits at the left edge, the title sits next to it, and the actions list sits at the right edge. If you set leading to nothing, Flutter shows a back button when the route can pop, or a drawer icon when the Scaffold has a drawer.
Use IconButton for both leading and actions. Always set tooltip. Screen readers use it, and Flutter shows it on long press.
Filling the Scaffold: body and floating action button
The body holds the screen's main content. The floatingActionButton (FAB) is a round button that floats above the body, usually for the one action the screen is built around.
Inside a FAB's onPressed, ScaffoldMessenger.of(context) finds the nearest messenger and shows a SnackBar. The context must come from a widget below MaterialApp, which the code below satisfies.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
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(
leading: IconButton(
icon: const Icon(Icons.menu),
tooltip: 'Menu',
onPressed: () {},
),
title: const Text('Inbox'),
centerTitle: true,
actions: [
IconButton(
icon: const Icon(Icons.search),
tooltip: 'Search',
onPressed: () {},
),
],
),
body: const Center(child: Text('No messages')),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Compose tapped')),
);
},
child: const Icon(Icons.edit),
),
);
}
}Theming the whole app with ThemeData
ThemeData sets the look of every Material widget below it. Setting colorScheme on ThemeData with ColorScheme.fromSeed builds a full palette from one seed color. Setting useMaterial3: true opts into the current Material 3 styles.
Read theme values in a widget with Theme.of(context). Hard-coded colors break when you change the theme, so prefer values from the scheme.
Common errors:
- Using
Scaffold.of(context)orScaffoldMessenger.of(context)with acontextfrom the samebuildthat creates theScaffold. The lookup fails because theScaffoldis not yet an ancestor. Move the call into a child widget or a callback. - Passing
titleas a plainString. It must be a widget, so wrap the text inText.
Exercise
The FAB's onPressed calls ScaffoldMessenger.of(context). Which widget must the context come from for this to work?
Show answer
A widget below MaterialApp, such as InboxPage. ScaffoldMessenger is provided by MaterialApp, so a context above it finds nothing.