What is Scaffold in Flutter?
Scaffold is a widget that provides a consistent, high‑level structure for a screen that follows Material Design guidelines. It acts as a container for common visual components like an app bar, a body (main content), a floating action button, a drawer, a bottom navigation bar, and a persistent footer. Almost every screen in a Material App will use a Scaffold as its root widget.
Basic Usage
The simplest Scaffold contains just a body. But you'll usually add an appBar as well. Here's the most basic example:
This creates a screen with a standard app bar at the top and a centered text in the main area.
Key Properties of Scaffold
Scaffold offers many properties to build rich interfaces. Here are the most important ones:
- appBar: An
AppBardisplayed at the top of the screen. Typically contains a title, navigation icon, and actions. - body: The primary content of the screen. Usually a widget like
Center,Column,ListView, orContainer. - floatingActionButton: A circular button that hovers over the body, used for primary actions. Often paired with
floatingActionButtonLocation. - drawer: A slide‑out panel from the left edge (or right, depending on locale) for navigation. Provide a
Drawerwidget. - endDrawer: Similar to
drawerbut slides from the right. - bottomNavigationBar: A bar at the bottom of the screen with icons and labels for top‑level navigation.
- bottomSheet: A persistent sheet anchored to the bottom of the screen.
- backgroundColor: The background color of the body area (defaults to
ThemeData.scaffoldBackgroundColor). - resizeToAvoidBottomInset: Whether the body should resize when the keyboard appears (default
true). - drawerScrimColor: The color of the scrim that shades the rest of the screen when the drawer is open.
- floatingActionButtonLocation: Controls where the FAB is placed (e.g.,
FloatingActionButtonLocation.centerFloat).
Adding a Floating Action Button
A Floating Action Button (FAB) is a common way to trigger a primary action. Add it via floatingActionButton and optionally set its location.
Using a Drawer
A drawer slides in from the left and typically contains navigation options. Provide a Drawer widget as the drawer property.
Adding a Bottom Navigation Bar
For switching between different sections of your app, use a BottomNavigationBar.
Handling Keyboard with resizeToAvoidBottomInset
By default, when the keyboard appears, Scaffold resizes its body to avoid the keyboard. This is controlled by resizeToAvoidBottomInset (default true). If you set it to false, the body will be overlapped by the keyboard. Usually you want it true.
Common Mistakes Beginners Make
- Forgetting to return a Scaffold in a route: Every screen (page) should have its own Scaffold as the top‑level widget, not nested inside another Scaffold.
- Using multiple Scaffolds on one screen: Only one Scaffold per screen. If you need a different layout inside, use other widgets (like Column, Container) but not another Scaffold.
- Not closing the drawer after navigation: When a user taps a drawer item, you should call
Navigator.pop(context)to close the drawer before navigating, unless you want a custom behavior. - Misplacing the FloatingActionButton: FAB should be inside the Scaffold, not inside the body; otherwise it might not position correctly.
- Ignoring
resizeToAvoidBottomInsetwhen needed: If you have a text field near the bottom and the keyboard covers it, setresizeToAvoidBottomInset: true(the default) to automatically adjust. - Overlooking the
drawerScrimColor: The semi‑transparent overlay when the drawer is open can be customized for better UX.
Key Points to Remember
- Scaffold implements the basic Material Design layout structure.
- It must be a child of a
MaterialApp(directly or indirectly) to inherit theme and navigation. - Only one Scaffold per screen – it's the top‑level container for that route.
- Use
appBar,body,floatingActionButton,drawer, andbottomNavigationBarto build full‑featured screens. - The body takes all remaining space after the app bar and bottom bar.
resizeToAvoidBottomInsethelps when the keyboard appears.
Common Interview Questions
- What is the purpose of Scaffold in Flutter?
- Can you have two Scaffolds on the same screen? Why or why not?
- How do you add a drawer to a screen? How do you close it programmatically?
- Explain the difference between
drawerandendDrawer. - How does
resizeToAvoidBottomInsetwork? When would you set it to false? - What is
floatingActionButtonLocationand what are some common options? - How do you change the background color of the body area in a Scaffold?