What is AppBar in Flutter?
AppBar is a Material Design widget that represents the top app bar, typically placed at the top of a screen. It usually contains a leading icon (like a menu or back button), a title, and optional action buttons. AppBar is most commonly used as the appBar property of a Scaffold.
Basic Usage
The simplest AppBar just has a title. Place it inside a Scaffold:
Key Properties
AppBar offers many properties to customize its appearance and behavior:
- leading: A widget displayed before the title. Typically an
IconButton(like menu or back). If not set, the AppBar automatically shows a back button when the previous route is known. - title: The primary widget (usually a
Text). It's centered by default, but you can put any widget here. - actions: A list of widgets (usually
IconButtons) displayed after the title. - flexibleSpace: A widget that sits behind the app bar content and can be animated (e.g., for collapsible effects).
- bottom: A widget at the bottom of the app bar, often a
TabBar. - elevation: The z-coordinate at which to place this app bar (shadow depth).
- backgroundColor: The color of the app bar.
- iconTheme: Color and opacity of icons in the leading and actions.
- textTheme: Text style for the title.
- centerTitle: Whether the title should be centered (default varies by platform).
- automaticallyImplyLeading: If true (and
leadingis null), automatically add a back button when the route can be popped. - shape: Defines the app bar's shape (e.g., rounded rectangle).
- shadowColor: Color of the shadow below the app bar.
Adding Actions
Use the actions property to add icons or buttons on the right side:
Using leading
You can replace the default back button with a custom leading icon:
AppBar with Tabs (bottom)
Combine AppBar with a TabBar to create a tabbed interface:
Customizing Appearance
You can change colors, elevation, and shape to match your design:
FlexibleSpace and SliverAppBar
For more advanced scrolling effects (like collapsible headers), use SliverAppBar inside a CustomScrollView. The flexibleSpace property allows you to place a background that animates as the user scrolls.
Common Mistakes Beginners Make
- Using AppBar without Scaffold: AppBar is designed to be placed inside a Scaffold's
appBarproperty. Using it elsewhere can lead to layout issues. - Forgetting to handle back navigation: When you provide a custom
leading, you lose the automatic back button; you must handle navigation yourself. - Overcrowding actions: Too many actions can overflow. Use a
PopupMenuButtonfor overflow items. - Not setting
centerTitleproperly: On iOS, the title is centered by default; on Android, it's left-aligned. UsecenterTitleto enforce your preference. - Misunderstanding
automaticallyImplyLeading: This only adds a back button if the route can be popped. If you want a menu button, you must provide your own leading. - Forgetting to wrap TabBar with a
DefaultTabController: Tabs need a controller to sync the TabBar with the TabBarView.
Key Points to Remember
- AppBar is typically used inside Scaffold's
appBarproperty. - It has three main areas: leading, title, and actions.
- Use
bottomto add a TabBar. - Customize colors with
backgroundColor,iconTheme,textTheme. - Control the back button with
automaticallyImplyLeadingand customleading. - For complex scroll effects, consider
SliverAppBar.
Common Interview Questions
- What is the purpose of the AppBar widget?
- How do you add icons to the right side of an AppBar?
- Explain the difference between
leadingandautomaticallyImplyLeading. - How would you create an AppBar with a search field instead of a title?
- What is
flexibleSpaceand when would you use it? - How do you add tabs below the AppBar? What controller is needed?