Sub topic 4
AppBar: the top bar of a screen
This chapter assumes that you know basic Dart syntax, can run a Flutter app, and understand StatelessWidget and its build method. You should also know that Flutter interfaces are built from nested widgets.
An AppBar is a Material Design widget that displays the top bar of a screen. It commonly contains a navigation control, a title, and action buttons. You normally place it in the appBar slot of a Scaffold.
Where AppBar fits in the widget tree
A typical screen has this shape:
MaterialApp provides app-wide Material behavior. A screen returns a Scaffold, and the Scaffold places the AppBar above its body.
The appBar property accepts a PreferredSizeWidget. AppBar implements that interface, which lets Scaffold determine how much vertical space the bar needs.
The main AppBar parts
The most important properties are:
| Property | Purpose |
|---|---|
| `title` | Main text or widget shown in the bar |
| `leading` | Widget at the start, often a back or menu button |
| `actions` | A list of widgets at the end, usually `IconButton` objects |
| `centerTitle` | Controls whether the title is centered |
| `backgroundColor` | Overrides the bar's background color |
| `bottom` | Adds content below the main bar, such as tabs |
The title is usually a Text widget, but it can be any widget. For example, a search field or a row containing an icon and text can act as the title.
The actions property expects a list. Use tooltips on icon buttons so that their purpose is available to users who use accessibility tools or press and hold an icon.
Automatic leading controls
An AppBar can create its leading widget automatically.
- If the current route was pushed over another route, Flutter commonly shows a back button.
- If the
Scaffoldhas adrawerand there is no back button, Flutter can show a menu button. - If you provide
leading, your widget replaces the automatically chosen control.
The automatic back button calls Navigator.pop when pressed. This removes the current route and reveals the previous screen. You usually do not need to implement that button yourself.
Use automaticallyImplyLeading: false when you want to prevent automatic leading behavior. You can then provide your own layout or leave the leading area empty.
A complete AppBar example
The following app has a title, a custom leading button, two actions, and a floating action button. Each button displays a SnackBar.
import 'package:flutter/material.dart';
void main() {
runApp(const AppBarExample());
}
class AppBarExample extends StatelessWidget {
const AppBarExample({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AppBar example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
home: const InboxPage(),
);
}
}
class InboxPage extends StatelessWidget {
const InboxPage({super.key});
void showMessage(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
centerTitle: true,
leading: IconButton(
icon: const Icon(Icons.menu),
tooltip: 'Open menu',
onPressed: () => showMessage(context, 'Menu pressed'),
),
actions: [
IconButton(
icon: const Icon(Icons.search),
tooltip: 'Search messages',
onPressed: () => showMessage(context, 'Search pressed'),
),
IconButton(
icon: const Icon(Icons.more_vert),
tooltip: 'More options',
onPressed: () => showMessage(context, 'More options pressed'),
),
],
),
body: const Center(
child: Text('Your messages appear here'),
),
floatingActionButton: FloatingActionButton(
onPressed: () => showMessage(context, 'Compose pressed'),
child: const Icon(Icons.edit),
),
);
}
}The AppBar does not own the screen's content. It describes the top area, while Scaffold positions it and manages related features such as the SnackBar.
A screen with a drawer can omit leading and let the Scaffold and AppBar provide the menu control. A screen opened with Navigator.pushNamed can also omit leading and receive a back button automatically.
AppBar and scrolling content
A normal AppBar stays at the top while the body scrolls. For a bar that collapses, floats, or changes as the user scrolls, use SliverAppBar inside a CustomScrollView. SliverAppBar is a related widget with different layout rules; it does not go in the Scaffold.appBar slot.
Exercise
What happens to the automatic back button when you provide an AppBar leading widget?
Show answer
Your leading widget replaces the automatically selected back or menu control. If it should navigate back, call Navigator.pop(context) in its callback.