Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if device needs SafeArea? (needs bottom/top padding)

Tags:

flutter

dart

Is there a way that I can check if the device that is running the app needs a SafeArea. I need to run code based on this true or false value. I do NOT need to use the SafeArea widget. All I need is the true or false answer if the device needs a padding.

eg. Devices like iPhone 8 does not need padding, will not have padding even if Widget is wrapped in SafeArea widget.

eg. Devices like iPhone X does need padding and will have padding if the Widget is wrapped in a SafeArea.

How does the SafeArea widget tell if the device needs padding or not? I looked into the SafeArea widget code and was not able to understand what happens to tell it if the device needs padding or not.

like image 983
punjabi4life Avatar asked Nov 14 '19 02:11

punjabi4life


People also ask

How do I know if my device has Notch flutter?

You can use MediaQuery. of(context). viewPadding and check whether the padding is greater than zero ,then you will need a safe area as there is a notch . And the best way to handle notch behaviour is to use the flutter_device_type package.

What is the purpose of a SafeArea?

A widget that insets its child by sufficient padding to avoid intrusions by the operating system. For example, this will indent the child by enough to avoid the status bar at the top of the screen.

How do you ignore safe area in flutter?

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.

What is safearea and how to use it?

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.

Why does safearea add padding around my Apps?

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.

What is the safe area on my screen?

The safe area represents the portion of your screen that is unobscured by bars and other operating system based content. Safe area is pre-defined by iOS across all Apple devices and is present in Android devices as well.

How do I check the dimensions of a safearea?

Internally SafeArea uses MediaQuery to check the dimensions of the display screen and includes extra padding if needed. 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.


2 Answers

You can use MediaQuery and get the viewPadding from that.

MediaQuery.of(context).viewPadding
like image 57
diegoveloper Avatar answered Nov 03 '22 02:11

diegoveloper


You can check if the top and bottom padding > 0 to see if the user's device has a notch to it:

window.viewPadding

Here's an example of iPhone 11 Pro Max:

Here's an example of iPhone 11 Pro Max

like image 43
digitaljoni Avatar answered Nov 03 '22 02:11

digitaljoni