简体   繁体   English

Flutter MediaQuery.of(context).size.width 检查设备大小总是在 iOS 模拟器中返回相同的错误值

[英]Flutter MediaQuery.of(context).size.width to check device-size always returns same wrong value in the iOS Simulator

I'm working on a Flutter-App that needs to resize elements on small screens (iPhone 5S, SE, 6S, Galaxy 5S, etc).我正在开发一个需要在小屏幕(iPhone 5S、SE、6S、Galaxy 5S 等)上调整元素大小的 Flutter 应用程序。

In order to do that I'm calling double width = MediaQuery.of(context).size.width , and if that value is below 750 pixels (iPhone 6S Size) the elements get made smaller.为了做到这一点,我调用double width = MediaQuery.of(context).size.width ,如果该值低于750像素(iPhone 6S 尺寸),则元素会变小。

This works excellently on devices, but in the iOS-simulator the value is always something small (~375), no matter which simulated device it runs on.这在设备上非常有效,但在 iOS 模拟器中,该值始终很小(~375),无论它在哪个模拟设备上运行。 Which basically make seeing if the app works on the simulator unusable afterwards, as it always shows the "small" version.这基本上可以查看应用程序是否在模拟器上运行后无法使用,因为它总是显示“小”版本。

Is there a better way to get the device-size which works with the iOS-Simulator?有没有更好的方法来获得适用于 iOS 模拟器的设备大小? Or possibly other ways to adapt to the screen-size?或者可能有其他方法来适应屏幕尺寸?

You can use您可以使用

import 'dart:ui';
window.physicalSize.width; // This will return the physical pixels.

I figured it out!我想到了! Turns out there are "logical" pixels (which the system is outputting here) and "device" pixels, which are calculated by reading out the ratio between those two.原来有“逻辑”像素(系统在这里输出)和“设备”像素,它们是通过读出这两者之间的比率来计算的。

This is used when a "pixel" can have sub-pixels, like when the never iDevices greatly increased their actual resolution, but the pixel-counts did not get multiplied by the same factor.当一个“像素”可以有子像素时使用,比如当从来没有 iDevices 大大提高了它们的实际分辨率,但像素数没有乘以相同的因素时。

double logicalPixelWidth = MediaQuery.of(context).size.width;
double devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
double devicePixelWidth = logicalPixelWidth * devicePixelRatio;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Get.width 或初始化 MediaQuery.of(context).size 在 Release 模式下不起作用 - Flutter Get.width or initialize MediaQuery.of(context).size not working in Release mode 未检测到键盘。 MediaQuery.of(context).viewInsets.bottom 总是返回 0.0 - Keyboard not being detected. MediaQuery.of(context).viewInsets.bottom always returns 0.0 在 flutter 中使用 MediaQuery 的高度和宽度大小时出错 - Error while using MediaQuery for size of height and width in flutter 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error EditText在设备上始终具有相同的大小,但在模拟器上却没有? - EditText always has same size on device but not on emulator? 压缩位图始终返回相同的字节大小 - Compressing bitmap returns always the same byte size flutter 相同大小的值小部件在 Android 和 IOS 上具有不同的实际大小 - flutter same size value widget has different actual size on Android and IOS 音频文件返回错误的大小值 - Audio file returns wrong size value 当我将 MediaQuery.of(context).padding.top 与 appBar 一起使用时,它返回 0.0。 这是为什么? - MediaQuery.of(context).padding.top is returning 0.0 when I'm using it with appBar. Why is that? getFromLocationName()。size()始终返回0-Android - getFromLocationName().size() returns always 0 - Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM