SafeArea is an important and useful widget in Flutter which makes UI dynamic and adaptive to a wide variety of devices. While designing the layout of widgets, we consider different types of devices and their pre-occupied constraints of screen like status bar, notches, navigation bar, etc. But new devices are being launched with different designs and in certain scenarios, your app might overlay any of those pre-occupied constraints. So, in order to make our UI adaptive and error-free, we use SafeArea widget.
In simple words, SafeArea is basically a padding widget, which adds any necessary padding to your app, based on the device it is running on. If your app's widgets are overlaying any of the system's features like notches, status bar, camera holes, or any other such features, then SafeArea would add padding around the app, as required.
Internally SafeArea uses MediaQuery to check the dimensions of the display screen and includes extra padding if needed.
Constructor :
const SafeArea({
Key key,
bool left: true,
bool top: true,
bool right: true,
bool bottom: true,
EdgeInsets minimum: EdgeInsets.zero,
bool maintainBottomViewPadding: false,
@required Widget child}
)
Above shown is the constructor for SafeArea. You can decide whether to avoid intrusions in a particular direction by changing the boolean value to true or false.
For instance, you want to use SafeArea in only top and bottom directions, then you can specify in following way.
SafeArea(
left: false,
top: true,
bottom: true,
right: false,
child: Text('Your Widget')
)
The above code would add padding only at top and bottom while other directions (left and right) would remain unaffected. If you don't specify any directions, then the default would be true for all directions.
If you want to add minimum padding in any of the directions, you can do it in the following way:
SafeArea(
minimum: const EdgeInsets.all(15.0),
child: Text('Your Widget'),
)
In the above code snippet, we are specifying the minimum padding that we need to add in all the directions. If you don't specify this Flutter would automatically calculate the required padding for all the directions.
Properties :
- bottom : This property is of type bool. It is true by default and setting it to false would disable SafeArea from adding padding to the bottom of the screen.
- top : This property is also of type bool and setting it to false would avoid padding at top of the screen.
- left : This property is of type bool and setting it to false would avoid padding at left side of the screen.
- right : This property is of type bool and setting it to false would avoid padding at right side of the screen.
- minimum : This property is of type EdgeInsets. You can specify the minimum padding to be added using this property.
- maintainBottomViewPadding : This property is of type bool and it specifies whether SafeArea should maintain the viewPadding instead of padding. For instance, if you are using an on-screen keyboard with SafeArea , the the padding can be maintained below the obstruction rather than being consumed.
Explanation :
Now we'll see how the above-mentioned properties work and when to use them.
Suppose, you are displaying a Text widget or any other widget at the bottom end of the screen, then on your device it might work, but on other devices, it might not be properly formatted.
Refer to the below screenshot to verify above mentioned scenario.
You can see that in the above app the text widget is not properly formatted. The above-shown device is rectangular in shape, but if you test the same app on an iOS device, then the text might cover the app drawer. As a developer, your app should be free from all bugs on all devices. In this case, we will use SafeArea.
Refer to below example :
SafeArea(
minimum: const EdgeInsets.all(12.0),
child: Text('Your Widget'),
)
In the above example, we have used minimum property. Here, minimum padding of 12 is added to all the sides, as all directions (top, bottom, right, left) are true by default.
If you want to use SafeArea in particular direction, then you can do it in the following way :
SafeArea(
top: true,
left: false,
bottom: true,
right: true,
minimum: const EdgeInsets.all(12.0),
child: Text('Your Widget'),
)
In the above example, we can see that all directions are set to true except left and hence SafeArea is not used in that direction.
Example: Without SafeArea
Dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Text('This is an example explaining use of SafeArea',
style: TextStyle(color: Colors.green,
fontSize: 20
),
),
),
);
}
}
In the above code, we have not used SafeArea and as a result Text is printed on the status bar. Notice that, we have not used App Bar here, because App Bar automatically calculates the values and adds the required padding.
Output:
With SafeArea :
Dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
top: true
child: Scaffold(
body: Text(
'This is an example explaining use of SafeArea',
style: TextStyle(color: Colors.green, fontSize: 20),
),
),
),
);
}
}
In the above code, we have used SafeArea and we can see that automatically padding is added around the text and is not covering the status bar.
Output:
In the above output, screen is only covered by status bar. Even if, it had been covered with notches, cameras etc even then the UI would adjust according to it.
Note: It is not necessary to use SafeArea above Scaffold. You can use it above any widget, which you think can scatter or create obstruction on screen elements.
Similar Reads
SQLite in Flutter SQLite is a very popular choice of local storage database. It is an SQL lightweight and serverless SQL database engine that is highly efficient and user-friendly. Flutter, Google's UI toolkit for building natively compiled applications, doesn't come with built-in support for local data storage but p
5 min read
Flutter - Verified Tick In many Android applications, we need to make the user account verified. Showing the verified icon in the flutter application is pretty simple. We can use the visibility widget to show and hide the icon. A sample video is given below to get an idea about what we are going to do in this article. How
3 min read
Flutter - String Validator If you are a developer and making applications and websites then you must have to validate different things whether it is email, URL, Credit Card Number, divisibility, and many more. If you get the same issue in Flutter to write different functions for validation then now you can use this package in
2 min read
MaterialApp class in Flutter MaterialApp Class: MaterialApp is a predefined class or widget in flutter. It is likely the main or core component of a flutter app. The MaterialApp widget provides a wrapper around other Material Widgets. We can access all the other components and widgets provided by Flutter SDK like Text widget, D
7 min read
Floating SnackBar in Flutter The floating_snackbar in Flutter is used to display a temporary message to users, often as feedback after an action.We know how to show the snack bar in a flutter, but everybody wants the application must look more interactive and user-friendly, that's why we can use animations or new designs in the
3 min read
Jetpack for flutter Flutter Jetpack is a Collection of Abstract Class, useful to handle the state of a flutter application with low boiler code unlike BLOC, Provider, River Pod State management Patterns. Let's check an example implementing Flutter Jetpack.Key FeaturesLiveData : It works as a State holder and Change not
4 min read