Jule
Jule

Reputation: 11

Flutter web get screen size

I first time try flutter for web and build a project on top off existing project according to this documentation https://flutter.dev/web. Project run fine but in chrome half off my application is out off screen. I tried to get screen size with MediaQuery.of(context) and SafeArea() but nothing happens. What is right way to get screen size?

Upvotes: 1

Views: 3027

Answers (1)

NITHIN KIRTHICK
NITHIN KIRTHICK

Reputation: 59

In Flutter, I've been attempting to determine the size of the entire context view. You may get width or height by using MediaQuery with the current context of your widget

double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;

Around your widget, you'll need a MaterialApp or a WidgetsApp. They are the ones who give the MediaQuery. Flutter will always look up the widget tree to find the widget when you call.of(context).

You can Improve responsiveness by adding Functions like

static bool isSmallScreen(BuildContext context) {
    return MediaQuery.of(context).size.width < 800;
  }
  static bool isLargeScreen(BuildContext context) {
    return MediaQuery.of(context).size.width > 1200;
  }
  static bool isMediumScreen(BuildContext context) {
    return MediaQuery.of(context).size.width >= 800 &&
        MediaQuery.of(context).size.width <= 1200;
  }

In my opinion, don't use a garbage library that doesn't support multiplatform and don't have above 100 pub points instead of create your own functions to do stuff and also you can use expand which expand the screen according to the screen size

don't give size like

height:100
weight:100

it may lead to screen crashing when we use it on multiple screen

Upvotes: 1

Related Questions