flutter - Flutter 如何计算屏幕宽度
问题描述
我有一个简单的颤振设置来获取分辨率为 2960*1440 的设备上的屏幕宽度。
但是,当我以 2960*1440 分辨率运行应用程序时,颤动返回的屏幕宽度为 411。颤振如何计算设备的宽度?
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"),
);
}
}
解决方案
颤振取决于MediaQuery
计算屏幕尺寸的方法:
MediaQuery.of(context).size.width ;
根据文档 ,它返回屏幕的“逻辑像素”的数量,其中每个像素代表几个物理像素,因设备而异,如果您想要实际的像素数,可以使用:
MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio ;
对于屏幕高度的实际像素:
MediaQuery.of(context).size.height * MediaQuery.of(context).devicePixelRatio ;
推荐阅读
- spring-boot - 无法连接到启用 SSL 的 GCP 中的 Redis
- java - 将打印作业扔到网络后如何使用 PJL 和 Java 取消打印作业?
- python - 网络抓取,我试图以 list 的形式从网站中提取数据,由于某种原因它的空列表 [] 并且没有错误
- mysql - Mysql复杂的Inner join场景
- go - 检查结构是否存在
- c++ - 在clion中,跨项目重命名
- nunit - 等待 Blazor 组件使用 nUnit 在 bUnit 中加载变量
- javascript - 如何在 forEach 循环中使用 Array.prototype.filter()
- mysql - 嵌套在 MySQL 全文搜索查询中的 AND 内
- excel-formula - 为什么这些连接语句不起作用?