2

I am building a flutter application that needs precise measurements of the screen in cm / inches.

According to the docs,

By definition, there are roughly 38 logical pixels per centimeter, or about 96 logical pixels per inch, of the physical display. The value returned by devicePixelRatio is ultimately obtained either from the hardware itself, the device drivers, or a hard-coded value stored in the operating system or firmware, and may be inaccurate, sometimes by a significant margin.

I have tried using these ratios in my application but they are not even close.

Is there a way to accurately calculate the dimensions of the screen?

Shai Menzin
  • 45
  • 1
  • 8
  • I'm fairly certain that is the only way, given the answers to [this similar question](https://stackoverflow.com/questions/2193457/is-there-a-way-to-determine-android-physical-screen-height-in-cm-or-inches) for Java Android development. But why do you even need to know that information? – Herohtar Jan 12 '20 at 20:45
  • @Herohtar The app has a "test" part that needs to know the actual size of widgets by the end of it. Again, the example you have given does not produce accurate results. Thanks for trying – Shai Menzin Jan 12 '20 at 20:52
  • 2
    I know the results are not accurate, but I am saying that the only way to get an estimate of the screen size is indeed the method you are already aware of. There is nothing that will give you absolutely accurate physical dimensions. The only accurate dimensions you can get are in number of pixels. – Herohtar Jan 12 '20 at 20:54
  • Yeah seems like this is a dead end... thank you for the clarification – Shai Menzin Jan 12 '20 at 21:08

1 Answers1

2

Flutter's pixel coordinates are given in logical pixels rather than physical pixels. However, MediaQuery will give you the conversion ratio.

var mediaQuery = MediaQuery.of(context);
var physicalPixelWidth = mediaQuery.size.width * mediaQuery.devicePixelRatio;
var physicalPixelHeight = mediaQuery.size.height * mediaQuery.devicePixelRatio;

(Note that this code can only be run in a place where a BuildContext object is available, such as a build method or a StatefulWidget's companion State class.)

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • 3
    This will give the amount of pixels, not the actual size. – Shai Menzin Jan 13 '20 at 07:39
  • 1
    @ShaiMenzin There's no reliable way to get the screen's physical dimensions even in native Android, let alone Flutter. [A few methods exist](https://stackoverflow.com/questions/2193457/is-there-a-way-to-determine-android-physical-screen-height-in-cm-or-inches), but they have known issues of returning incorrect sizes for some devices. Otherwise, the best you can hope for is to compare the physical pixel dimensions to a known pixel density of the device. – Abion47 Jan 13 '20 at 18:49
  • (Native iOS is easier to work with because there are comparatively very few types of iOS devices, so rather than try and find a way to calculate it programmatically it would be easier to just hardcode a lookup table of known device sizes.) – Abion47 Jan 13 '20 at 18:50