Flutter MediaQuery.of(context).size.width 值与实际屏幕分辨率不同

Flutter MediaQuery.of(context).size.width values are different than real screen resolution

在我的 Flutter 应用程序中,我试图获取真实的屏幕宽度(在每个设备上自然会有所不同)。

我正在使用 MediaQuery.of(context).size.width,但我注意到返回的值与实际屏幕分辨率不匹配。

例如,

有谁知道为什么 MediaQuery 返回的值与以像素为单位的实际屏幕分辨率不同?

谢谢

根据the size property's documentation

The size of the media in logical pixels (e.g, the size of the screen).

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.

所以你会MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio获得以物理像素为单位的宽度。