What is SafeArea?
SafeArea is a widget that insets its child by enough padding to avoid intrusions from the operating system. These intrusions include the status bar, the notch on modern phones, the home indicator (on iOS), and rounded screen corners. By using SafeArea, you ensure that your content is visible and tappable, regardless of the device. Without it, parts of your UI might be hidden behind the system UI or cut off by screen curves.
Why Use SafeArea?
- Notches and cutouts: Phones with notches (e.g., iPhone X, Pixel 3) have a physical cutout that can obscure content.
- Status bar: The status bar area (time, battery) should not be overlapped by app content.
- Navigation bar: On Android, the navigation bar (back, home, recents) can overlap the bottom of the screen.
- Rounded corners: Some devices have rounded corners; you might want to keep content away from the edges.
- Gesture indicators: On iOS, the home indicator at the bottom requires extra space for gestures.
Basic Usage
Simply wrap your top‑level widget (usually the body of a Scaffold) with SafeArea. It automatically adds padding on all sides where needed.
SafeArea can also be used inside a Scaffold's body or anywhere you have a widget that might be overlapped by system UI. Note that AppBar already handles safe area for the top, so you usually don't need to wrap an AppBar.
Properties of SafeArea
minimum: The minimum padding to apply, of typeEdgeInsets. Default isEdgeInsets.zero.
left,top,right,bottom: Booleans to control which sides are inset. Default istruefor all sides.
child: The widget that gets padded.
maintainBottomViewPadding: Iftrue, the padding includes the view padding (keyboard) as well. Defaultfalse.
Selective Safe Areas
You can opt out of safe areas on specific sides. For example, if you want the content to go under the status bar but avoid the notch on the top, you can set top: false.
Minimum Padding
The minimum property adds extra padding on top of the system insets. This is useful when you want to add a custom offset (e.g., to avoid a persistent header).
SafeArea vs MediaQuery.padding
MediaQuery.of(context).padding gives you the system insets directly. SafeArea is a convenience widget that automatically applies those insets as padding. You can use the padding manually if you need more control. For example:
However, SafeArea is simpler for most cases and works even if the widget tree doesn't have a MediaQuery ancestor (though it does need a MediaQuery from the context, which is provided by MaterialApp).
Common Mistakes
- Wrapping the entire
Scaffold: TheScaffolditself already handles some safe areas (e.g., itsappBaris safe by default). Wrapping the wholeScaffoldmay create double padding.
- Wrapping the entire
- Using
SafeAreainside aListView: If theListViewis already scrollable, safe area insets are applied only to the topmost visible part, but the list content may still be overlapped if the list is long and scrolls under the system UI. Usually, you wrap thebodyof theScaffoldwithSafeArea, not the list itself.
- Using
- Forgetting to set
maintainBottomViewPaddingwhen needed: If you have aTextFieldat the bottom and the keyboard opens, you may want the bottom padding to remain even when the keyboard is dismissed. SettingmaintainBottomViewPadding: truecan help, but it's often better to useScaffold'sresizeToAvoidBottomInset.
- Forgetting to set
Best Practices
- Always wrap the
bodyof aScaffoldwithSafeArea(or useScaffold'sextendBodyBehindAppBarif you want to go under the app bar).
- Always wrap the
- If you have a custom widget that should avoid the system UI, wrap it in
SafeArea.
- If you have a custom widget that should avoid the system UI, wrap it in
- Use
maintainBottomViewPadding: truewhen you have persistent bottom elements (e.g., a bottom navigation bar) that should stay above the keyboard.
- Use
- Test on devices with notches and rounded corners (emulators work).
- Combine
SafeAreawithMediaQueryfor advanced customisation.
- Combine
Key Takeaways
SafeAreaadds padding to avoid system UI intrusions (notches, status bar, home indicator).
- It is typically used to wrap the
bodyof aScaffold.
- It is typically used to wrap the
- You can enable/disable padding on specific sides with
left,top,right,bottom.
- You can enable/disable padding on specific sides with
minimumadds extra padding on top of system insets.
- Avoid wrapping the entire
ScaffoldwithSafeArea; use it only where needed.
- Avoid wrapping the entire