Notification texts go here Contact Us Buy Now!

How to get safe area size/padding in Flutter

To get Bottom Padding use

``` final bottomPadding = MediaQuery.of(context).padding.bottom; ```

To get Top Padding use

``` final topPadding = MediaQuery.of(context).padding.top; ```

If you want to get the height of the App Bar which is used as a flutter Material component

``` AppBar appBar = new AppBar(); appBar.preferredSize.height; ```

SafeArea widget's bottom and top properties by default are set to true. Setting 'bottom: false' and 'top: false' returns the actual SafeArea's bottom and top padding value.

``` return SafeArea( top: false, bottom: false, child: Scaffold( body: SingleChildScrollView( child: Container( height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, ... ```

To get only Safe Area's height, try to access MediaQuery somewhere before adding SafeArea in the widget tree. This way you will get MediaQuery.of(context).padding with some value instead of EdgeInsets.zero. Padding is set to zero after insertion of SafeArea in the widget tree.

``` Widget build(BuildContext context) { final size = MediaQuery.of(context).size; final padding = MediaQuery.of(context).padding; final heigth = size.height - padding.top - padding.bottom; return SafeArea( ... ); } ``` ``` import 'dart:ui' as ui; MediaQueryData.fromWindow(ui.window).size; MediaQueryData.fromWindow(ui.window).padding; ```

Since version 3.10.0, the window singleton is deprecated( https://docs.flutter.dev/release/breaking-changes/window-singleton). Therefore, to obtain the safe area padding, you must now apply the following:

``` MediaQueryData.fromView(ui.PlatformDispatcher.instance.implicitView!).padding ```

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.