Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Determine Screen Height and Width

I've created a new application on flutter, and I've had problems with the screen sizes when switching between different devices.

I created the application using the Pixel 2XL screen size, and because I've had containers with a child of ListView it's asked me to include a height and width for the container.

So when I switch the device to a new device the container is too long and throws an error.

How can I go about making it so the application is optimized for all screens?

like image 506
Tom O'Sullivan Avatar asked Mar 29 '18 10:03

Tom O'Sullivan


People also ask

How do you measure screen width?

The screen width is equal to the aspect ratio width ARw times the screen diagonal divided by the square root of the aspect ratio width ARw squared plus the aspect ratio height ARh squared.


2 Answers

You can use:

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

To get height just of SafeArea (for iOS 11 and above):

  • var padding = MediaQuery.of(context).padding;
  • double newheight = height - padding.top - padding.bottom;
like image 192
yashthakkar1173 Avatar answered Oct 26 '22 14:10

yashthakkar1173


Getting width is easy but height can be tricky, following are the ways to deal with height

// Full screen width and height double width = MediaQuery.of(context).size.width; double height = MediaQuery.of(context).size.height;  // Height (without SafeArea) var padding = MediaQuery.of(context).viewPadding; double height1 = height - padding.top - padding.bottom;  // Height (without status bar) double height2 = height - padding.top;  // Height (without status and toolbar) double height3 = height - padding.top - kToolbarHeight; 
like image 38
CopsOnRoad Avatar answered Oct 26 '22 16:10

CopsOnRoad