SafeArea in Flutter

Learn via video courses
Topics Covered

Overview

The SafeArea widget in Flutter is a powerful feature that guarantees your app's content stays within the device screen's safe area, preventing overlap with notches, status bars, or navigation bars. In this article, we'll look at how to manage different device notches, system bars, and screen edges using safe area flutter. Developers may utilize safe area flutter to create aesthetically beautiful and user-friendly programs that adapt gracefully to multiple device configurations, providing a smooth and immersive user experience while preventing content clipping or interference.

Usage of SafeArea

SafeArea's constructor is as follows.

The constructor for SafeArea is given above. The constructor includes four arguments that represent the screen's four sides: left, top, right, and bottom. These settings are used to specify if system intrusions should be avoided on the relevant side. All of them are set to true by default, which implies that if you enable SafeArea, Flutter will strive to avoid system intrusions from all angles. Another option, minimum, can be used to specify the minimum padding. You can specify the minimum padding for each side by passing an instance of EdgeInsets (which is often used to set padding). By altering the boolean value to true or false, you may select whether to prevent incursions in a certain direction.

For example, if you just want to use SafeArea in the top and bottom directions, you may do it as follows.

The above code would only apply padding at the top and bottom, leaving the other directions (left and right) unchanged. If no directions are specified, the default is true for all directions.

If you wish to add minimal padding in any of the directions, do the following:

The preceding code snippet specifies the minimum padding that must be added in all directions. If you do not supply this, Flutter will compute the necessary padding for all directions.

Handling SafeArea Insets

The system UI, for example, the clock and battery, might also impede your UI. The term safe area insets refers to enough cushioning to avoid certain difficulties. SafeArea is a widget that gives its child enough padding to avoid obstructions. Flutter's view ignores the safe region by default. As a result, your perspective may be influenced by the hardware and software components. To lay out widgets inside the safe area, simply add a widget to the SafeArea widget that will avoid those barriers. For example, this will provide the toddler with adequate offset to avoid any obstacles.

The text will no longer be cut by anything. This is because Flutter gives enough padding to the kid to ensure that it is not displayed in an area where the content can be intruded.

Properties

PropertyFunction
bottomThis property is of the bool type. It is set to true by default, and if changed to false, SafeArea will no longer provide padding to the bottom of the screen.
topThis attribute is also of the bool type, and setting it to false prevents padding at the top of the screen.
leftSetting this attribute to false will prevent padding on the left side of the screen.
rightSetting this attribute to false will prevent padding on the right side of the screen.
minimumThis property has the EdgeInsets type. This parameter allows you to set the minimum amount of padding to be applied.
maintainBottomViewPaddingThis bool attribute determines whether SafeArea should keep the viewPadding instead of padding. For example, if you use an on-screen keyboard with SafeArea, the padding can be kept below the blockage rather than eaten.

Example App

Create a new project. Your entire code in the main.dart file will look like this:

main.dart

firstpage.dart

secpage.dart

Output:
This is how your app will appear.

example app of using safearea widget in flutter

Conclusion

  • SafeArea is a crucial Flutter widget that makes the user interface dynamic and adaptable to various devices.
  • It is a padding widget that adds necessary padding to the app based on the device it is running on.
  • If your app's widgets overlap any system features, SafeArea will provide padding around the app as needed.
  • The constructor for SafeArea includes four arguments representing the screen's sides: left, top, right, and bottom. These settings specify if system intrusions should be avoided on the relevant side.
  • The minimum parameter, set to true or false, allows you to specify the minimum padding for each side by passing an instance of EdgeInsets.
  • By changing the boolean value to true or false, you can select whether to prevent incursions in a certain direction. If not supplied with any information, Flutter will compute the necessary padding for all directions.
  • SafeArea insets refer to enough cushioning to avoid certain difficulties.
  • By adding widgets inside the safe area, you can lay out widgets that avoid obstructions.
  • The bottom property is of the bool type, and setting it to false prevents padding at the bottom of the screen.
  • The top property is also of the bool type, and setting it to false prevents padding at the top of the screen.
  • The maintainBottomViewPadding attribute determines whether SafeArea should keep viewPadding instead of padding.