Flutter如何计算屏幕宽度

3

我有一个简单的Flutter设置,用于获取分辨率为2960 * 1440的设备屏幕宽度。

然而,当我在2960 * 1440分辨率上运行应用程序时,Flutter返回屏幕宽度为411。Flutter如何计算设备的宽度?

class page extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width.toString();

    return Container(
      child: Text("Height : $height and Width : $width"),

    );
  }
}
1个回答

4

Flutter依赖MediaQuery方法来计算屏幕大小:

  MediaQuery.of(context).size.width ;

根据文档,此函数返回屏幕上“逻辑像素”的数量,其中每个逻辑像素由一个因素表示,该因素因设备而异,代表多个物理像素。如果您想要实际像素数,可以使用以下代码:
  MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio ;

而对于你的屏幕高度的实际像素:

  MediaQuery.of(context).size.height * MediaQuery.of(context).devicePixelRatio ;

1
要深入理解此内容,请查看以下网站:https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions - Randal Schwartz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接