【问题标题】:How flutter calculates screen widthFlutter 如何计算屏幕宽度
【发布时间】:2019-02-16 10:43:01
【问题描述】:

我有一个简单的颤振设置来获取分辨率为 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"),

    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    flutter 依赖于MediaQuery 方法来计算屏幕尺寸:

      MediaQuery.of(context).size.width ;
    

    根据documentation,它返回屏幕的“逻辑像素”的数量,每个像素代表几个物理像素,不同的设备不同,如果您想要实际数量您可以使用的像素:

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

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    相关资源
    最近更新 更多