Flutter iOS - MediaQuery.of(context).size.width 没有给出以像素为单位的宽度
Flutter iOS - MediaQuery.of(context).size.width does not give the width in pixel
我正在编写一个新的 flutter 应用程序,有时我需要获取 iOS 设备的像素数。当我使用 MediaQuery.of(context).size.width
时,它 returns 像素数除以 3 或 2,具体取决于我使用的设备。
例如,在我的 iPhone5s 上,屏幕宽度假定为 640 像素,但媒体查询 returns 我是 320。在我的 iPhoneXs 上,宽度假定为 1125 像素,但它returns我375.
我们如何解决这个问题?我不确定,但我听说苹果不是使用像素而是使用点系统?可能与...
根据 docs for MediaQuery.of(context).size
,
MediaQuery.of(context).size
returns the size of the media in logical pixels
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.
Flutter 向您显示的是您设备的逻辑像素。您可能正在寻找的(或称为 640 像素和 1125 像素)是设备的 物理像素。
要获取设备的物理像素,您应该尝试
var devicePhysicalPixelWidth = MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio
简单来说,
devicePhysicalPixelWidth = logical pixel width * device pixel ratio
我正在编写一个新的 flutter 应用程序,有时我需要获取 iOS 设备的像素数。当我使用 MediaQuery.of(context).size.width
时,它 returns 像素数除以 3 或 2,具体取决于我使用的设备。
例如,在我的 iPhone5s 上,屏幕宽度假定为 640 像素,但媒体查询 returns 我是 320。在我的 iPhoneXs 上,宽度假定为 1125 像素,但它returns我375.
我们如何解决这个问题?我不确定,但我听说苹果不是使用像素而是使用点系统?可能与...
根据 docs for MediaQuery.of(context).size
,
MediaQuery.of(context).size
returns the size of the media in logical pixelsLogical 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.
Flutter 向您显示的是您设备的逻辑像素。您可能正在寻找的(或称为 640 像素和 1125 像素)是设备的 物理像素。
要获取设备的物理像素,您应该尝试
var devicePhysicalPixelWidth = MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio
简单来说,
devicePhysicalPixelWidth = logical pixel width * device pixel ratio