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
,但我注意到返回的值与实际屏幕分辨率不匹配。
例如,
在模拟器上 iPhone 11 Pro Max(分辨率为 2688 x 1242)我得到 MediaQuery.of(context).size.width
= 414
在模拟器 Nexus XL(分辨率为 1440 x 2560)上,我得到 MediaQuery.of(context).size.width
= 411.42857142857144
在真实设备上 iPhone 7(分辨率为 1,334 x 750)我得到 MediaQuery.of(context).size.width
= 375
有谁知道为什么 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
获得以物理像素为单位的宽度。
在我的 Flutter 应用程序中,我试图获取真实的屏幕宽度(在每个设备上自然会有所不同)。
我正在使用 MediaQuery.of(context).size.width
,但我注意到返回的值与实际屏幕分辨率不匹配。
例如,
在模拟器上 iPhone 11 Pro Max(分辨率为 2688 x 1242)我得到
MediaQuery.of(context).size.width
= 414在模拟器 Nexus XL(分辨率为 1440 x 2560)上,我得到
MediaQuery.of(context).size.width
= 411.42857142857144在真实设备上 iPhone 7(分辨率为 1,334 x 750)我得到
MediaQuery.of(context).size.width
= 375
有谁知道为什么 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
获得以物理像素为单位的宽度。