我正在构建一个 flutter 应用程序,该应用程序需要以厘米/英寸为单位的屏幕精确尺寸。
根据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.
我已经尝试在应用程序中使用这些比率,但是它们甚至还不接近。
有没有一种方法可以准确计算屏幕尺寸?
最佳答案
Flutter的像素坐标以逻辑像素而非物理像素给出。但是,MediaQuery
将为您提供转换率。
var mediaQuery = MediaQuery.of(context);
var physicalPixelWidth = mediaQuery.size.width * mediaQuery.devicePixelRatio;
var physicalPixelHeight = mediaQuery.size.height * mediaQuery.devicePixelRatio;
(请注意,此代码只能在
BuildContext
对象可用的地方运行,例如build
方法或StatefulWidget
的伴随State
类。)
关于flutter - 获取屏幕抖动的物理尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59707923/