Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width and height of the device in Flutter

I need a reliable way to get the dimensions of the screen.

I know MediaQuery.of(context), but it removes the bottom padding when the bottom UI item is visible.

like image 733
BenSabo Avatar asked Oct 23 '18 17:10

BenSabo


People also ask

How do you get the height of a widget on a flutter?

That's how to get the size and position of a widget rendered on the screen. Basically, you need to get the RenderBox of the widget. Then, you can access the size property to get the size of the widget and call localToGlobal method to get the offset.

How do you find the aspect ratio on a flutter?

Flutter Widget - AspectRatio() Flutter solves this by providing the AspectRatio widget. You give it an AspectRatio, a child, and, well, that's it. Aspect ratio is the ratio between the width and height of a box. It's often written as a fraction, like 3/2, as in three parts of width to two parts of height.

How do I know my screen height?

To evaluate the screen's length, height, and area, we can follow the same equations as for a flat one: height = diagonal / √(AR²+1) ; length = AR * height ; and. area = height * length .


1 Answers

This appears to be impossible from within dart at the moment on Android due to flutter ignoring the bottom system UI (i.e. the buttons).

I thought this might be a bug, but if you look closely at the documentation it never states that window.physicalSize or MediaQueryData.size are the physical dimensions of the screen, but rather the size to which flutter can render. That probably makes sense, or else every single app would have to make sure to take that into account.

So what you're going to have to do is use method channels to communicate with android directly. I took a look already and there doesn't appear to be any plugins doing this, so you could wrap it up into one if you feel ambitious. But what you'll want to do is make a call to native and then get the physical screen size directly in java code. If you do that you'd probably be best off implementing it for iOS as well, although this same problem doesn't exist there (you could even do it directly in flutter with an if/else).

Luckily, someone has done this before so you can use it as an example: https://github.com/magnatronus/flutter-displaymetrics. Assuming that displayMetrics gives you the right size.

Hope that helps and sorry I don't have a simpler answer for you!

like image 117
rmtmckenzie Avatar answered Oct 16 '22 05:10

rmtmckenzie