Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect values from MediaQuery.of(context).size in Flutter

Tags:

flutter

please help with a problem I have in flutter. I put them in context:

I must create a responsive application. I will use MediaQuery.of(context).size, so in my universe I consider:

  • size.width > 1280px => desktop device
  • size.width < 768px => mobile device
  • size.width between 769px and 1279px => tablet device
  1. I create a device in android studio (ADV Manager) with a resolution of 2560px x 1800px (desktop device).
  • device image in ADV Manager
  1. I code a simple instruction to know the value of size.width:
  • code image
  1. I hope that the console will print the value 2560, but it prints 1280:
  • console results image

Note: I did a lot of tests on all the different types of devices that ADV Manager offers (TV, phone, tablet, automotive and wear OS) and I realized that the value of size.width is always the real value divided in two, some examples :

  • If the device has a width of 1000, size.width takes the value of 500.

  • If the device has a width of 2560, size.width takes the value of 1280.

  • etc., always the same result.

I have considered multiplying the size.width value by two (to get the actual value), but in the web browser the size.witdh value is correct so the problem is only on emulated android devices.

Can anyone have any idea what is wrong? Thanks in advance.


1 Answers

MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio to get the width in physical pixels.

Documentation : Logical pixels are roughly the same visual size across devices. Physical pixels are the size of the actual hardware pixels on the device. The number of physical pixels per logical pixel is described by the devicePixelRatio.

like image 99
Simon Sot Avatar answered Nov 07 '25 15:11

Simon Sot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!