Notification texts go here Contact Us Buy Now!

How to get safe area size/padding in Flutter

Getting the safe area size or padding in Flutter is crucial for ensuring that your app's content is displayed correctly within the available screen space. Here are several methods to achieve this:

  1. Using MediaQuery:
  2. final bottomPadding = MediaQuery.of(context).padding.bottom;
    final topPadding = MediaQuery.of(context).padding.top;
    
  3. Accessing AppBar's Preferred Size:
  4. AppBar appBar = new AppBar();
    appBar.preferredSize.height;
    
  5. Disabling SafeArea's Default Padding:
  6. return SafeArea(
      top: false,
      bottom: false,
      child: Scaffold(
          body: SingleChildScrollView(
            child: Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              ...
            ),
          ),
        ),
      );
    
  7. Accessing MediaQuery Before SafeArea:
  8. 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(
            ...
        );
    }
    
  9. Using MediaQueryData.fromWindow:
  10. import 'dart:ui' as ui;
    
    MediaQueryData.fromWindow(ui.window).size;
    MediaQueryData.fromWindow(ui.window).padding;
    
  11. Using MediaQueryData.fromView (Flutter 3.10.0+):
  12. MediaQueryData.fromView(ui.PlatformDispatcher.instance.implicitView!).padding
    

By utilizing these methods, you can effectively obtain the safe area size or padding in Flutter, ensuring that your app's content is displayed optimally within the available screen area.

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.