Notification texts go here Contact Us Buy Now!

How to get safe area size/padding in Flutter

To determine the padding and safe area size in Flutter, consider the following methods:

  • Bottom Padding:
  • final bottomPadding = MediaQuery.of(context).padding.bottom;
    
  • Top Padding:
  • final topPadding = MediaQuery.of(context).padding.top;
    
  • Height of App Bar:
  • AppBar appBar = new AppBar();
    appBar.preferredSize.height;
    
  • SafeArea Widget Properties:
  • 
    return SafeArea(
          top: false,
          bottom: false,
          child: Scaffold(
              body: SingleChildScrollView(
                child: Container(
                  height: MediaQuery.of(context).size.height,
                  width: MediaQuery.of(context).size.width,
                  ...
    
  • Access MediaQuery before SafeArea:
  • 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(
            ...
        );
    }
    
  • Use ui.window:
  • import 'dart:ui' as ui;
    
    MediaQueryData.fromWindow(ui.window).size;
    MediaQueryData.fromWindow(ui.window).padding;
    
  • Use the Updated Method for Flutter 3.10.0+:
  • MediaQueryData.fromView(ui.PlatformDispatcher.instance.implicitView!).padding
    
Select the method that best suits your needs and refer to the provided code examples for guidance.

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.