如何在Flutter中确定屏幕的高度和宽度 [英] How to Determine the Screen Height and Width in Flutter

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

问题描述

我已经在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?

推荐答案

您可以使用:

  • 双倍宽度= MediaQuery.of(context).size.width;
  • 双倍高度= 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;

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

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