[英]Flutter MediaQuery.of(context).size.width to check device-size always returns same wrong value in the iOS Simulator
I'm working on a Flutter-App that needs to resize elements on small screens (iPhone 5S, SE, 6S, Galaxy 5S, etc).我正在开发一个需要在小屏幕(iPhone 5S、SE、6S、Galaxy 5S 等)上调整元素大小的 Flutter 应用程序。
In order to do that I'm calling double width = MediaQuery.of(context).size.width
, and if that value is below 750
pixels (iPhone 6S Size) the elements get made smaller.为了做到这一点,我调用
double width = MediaQuery.of(context).size.width
,如果该值低于750
像素(iPhone 6S 尺寸),则元素会变小。
This works excellently on devices, but in the iOS-simulator the value is always something small (~375), no matter which simulated device it runs on.这在设备上非常有效,但在 iOS 模拟器中,该值始终很小(~375),无论它在哪个模拟设备上运行。 Which basically make seeing if the app works on the simulator unusable afterwards, as it always shows the "small" version.
这基本上可以查看应用程序是否在模拟器上运行后无法使用,因为它总是显示“小”版本。
Is there a better way to get the device-size which works with the iOS-Simulator?有没有更好的方法来获得适用于 iOS 模拟器的设备大小? Or possibly other ways to adapt to the screen-size?
或者可能有其他方法来适应屏幕尺寸?
You can use您可以使用
import 'dart:ui';
window.physicalSize.width; // This will return the physical pixels.
I figured it out!我想到了! Turns out there are "logical" pixels (which the system is outputting here) and "device" pixels, which are calculated by reading out the ratio between those two.
原来有“逻辑”像素(系统在这里输出)和“设备”像素,它们是通过读出这两者之间的比率来计算的。
This is used when a "pixel" can have sub-pixels, like when the never iDevices greatly increased their actual resolution, but the pixel-counts did not get multiplied by the same factor.当一个“像素”可以有子像素时使用,比如当从来没有 iDevices 大大提高了它们的实际分辨率,但像素数没有乘以相同的因素时。
double logicalPixelWidth = MediaQuery.of(context).size.width;
double devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
double devicePixelWidth = logicalPixelWidth * devicePixelRatio;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.