Sub topic 5
Customizing the AppBar: title, leading, and actions
Assume you know basic Dart syntax, can run a Flutter app, and understand StatelessWidget and its build method. This chapter focuses on the three main regions of an AppBar: the leading widget, the title, and the actions.
An AppBar is a horizontal bar normally placed in a Scaffold's appBar property. Its layout communicates where the user is, how to go back, and which actions are available.
The title
The title property usually contains a Text widget. It identifies the current screen and should be short enough to fit beside the leading widget and actions.
AppBar(
title: const Text('Inbox'),
)You can center the title when that matches the design:
AppBar(
title: const Text('Inbox'),
centerTitle: true,
)centerTitle is a request to center the title according to the platform and app bar layout. Avoid assuming that every device will position it identically when other widgets take space.
The leading widget
The leading property is the widget at the start of the bar. It is often an IconButton:
AppBar(
leading: IconButton(
icon: const Icon(Icons.menu),
tooltip: 'Open navigation menu',
onPressed: () {
// Open the drawer or another navigation control.
},
),
title: const Text('Inbox'),
)The tooltip gives the button an accessible description and helps users who pause over it.
When a screen is pushed onto a navigation stack, Flutter often supplies a back button automatically. Setting leading replaces that automatically chosen widget. This is useful when you need a custom control, but it also means you must preserve an appropriate way to leave the screen.
A Scaffold with a drawer can provide a menu button automatically. In that case, you usually do not need to set leading yourself:
Scaffold(
appBar: AppBar(title: const Text('Inbox')),
drawer: const Drawer(
child: Center(child: Text('Navigation')),
),
body: const Center(child: Text('Messages')),
)The actions list
The actions property contains widgets placed at the end of the bar. It is commonly a list of IconButton widgets.
AppBar(
title: const Text('Inbox'),
actions: [
IconButton(
icon: const Icon(Icons.search),
tooltip: 'Search messages',
onPressed: () {},
),
IconButton(
icon: const Icon(Icons.more_vert),
tooltip: 'More options',
onPressed: () {},
),
],
)Actions should be commands that apply to the current screen. Keep the list short. If too many actions compete for space, move less frequent commands into a menu such as PopupMenuButton.
A complete screen
The following app shows a custom leading button, a centered title, two actions, and a floating action button. The ScaffoldMessenger displays a SnackBar when an action is selected.
import 'package:flutter/material.dart';
void main() => runApp(const App());
class App extends StatelessWidget {
const App({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: 'Open menu',
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Menu selected')),
);
},
),
title: const Text('Inbox'),
centerTitle: true,
actions: [
IconButton(
icon: const Icon(Icons.search),
tooltip: 'Search',
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Search selected')),
);
},
),
PopupMenuButton<String>(
tooltip: 'More options',
onSelected: (value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$value selected')),
);
},
itemBuilder: (context) => const [
PopupMenuItem(
value: 'Settings',
child: Text('Settings'),
),
PopupMenuItem(
value: 'Help',
child: Text('Help'),
),
],
),
],
),
body: const Center(child: Text('No messages')),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Compose message',
child: const Icon(Icons.edit),
),
);
}
}Common layout mistakes
- Putting screen content in
actionsinstead of theScaffold'sbody. - Replacing an automatic back button with a custom leading widget that does nothing.
- Adding too many action buttons for narrow screens.
- Omitting tooltips from icon-only controls.
- Using a title that is so long that it competes with the actions.
The app bar is part of the widget tree below MaterialApp. It receives inherited theme values, so its colors, typography, and icon appearance can change when you configure the app-wide ThemeData.
Exercise
What happens to the automatic back button when you set the AppBar's leading property?
Show answer
Flutter uses the widget you provide instead of the automatically selected leading widget. Your custom widget should still provide a clear way to navigate back when the screen needs one.