如何确定屏幕高度和宽度 [英] How to Determine Screen Height and Width

查看:32
本文介绍了如何确定屏幕高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Flutter 上创建了一个新应用程序,但在不同设备之间切换时遇到了屏幕尺寸问题.

I've created a new application on flutter, and I've had problems with the screen sizes when switching between different devices.

我使用 Pixel 2XL 屏幕尺寸创建了应用程序,并且因为我有一个带有 ListView 子元素的容器,它要求我包含容器的高度和宽度.

I created the application using the Pixel 2XL screen size, and because I've had containers with a child of ListView it's asked me to include a height and width for the container.

因此,当我将设备切换到新设备时,容器太长并引发错误.

So when I switch the device to a new device the container is too long and throws an error.

我如何才能使应用程序针对所有屏幕进行优化?

How can I go about making it so the application is optimized for all screens?

推荐答案

您可以使用:

  • double width = MediaQuery.of(context).size.width;
  • double height = MediaQuery.of(context).size.height;

要获得 SafeArea 的高度(适用于 iOS 11 及更高版本):

To get height just of SafeArea (for iOS 11 and above):

  • var padding = MediaQuery.of(context).padding;
  • double newheight = height - padding.top - padding.bottom;

这篇关于如何确定屏幕高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆