gpt4 book ai didi

flutter 检测装置类型

转载 作者:行者123 更新时间:2023-12-03 03:22:55 24 4
gpt4 key购买 nike

据我所知,我们可以很容易地检测到 flutter 中的操作系统。现在我只是想知道是否有任何方法可以确定应用程序运行在哪个设备上。例如,如果应用程序在移动设备或平板设备上运行。他们都可以有相同的操作系统。原因是由于屏幕尺寸,我想在我的一个小部件中显示,例如移动设备上的图标和平板设备上带有图标的文本。我知道我们可以使用 MediaQuery.of(context).size.width 来找到屏幕的宽度,但是我不确定这是否是正确的方法,如果是的话,那是什么每个设备的可靠断点

最佳答案

您可以使用 flutter_device_type 中的以下代码片段包裹:

import 'dart:ui' as ui;

bool isTablet;
bool isPhone;

final double devicePixelRatio = ui.window.devicePixelRatio;
final ui.Size size = ui.window.physicalSize;
final double width = size.width;
final double height = size.height;


if(devicePixelRatio < 2 && (width >= 1000 || height >= 1000)) {
isTablet = true;
isPhone = false;
}
else if(devicePixelRatio == 2 && (width >= 1920 || height >= 1920)) {
isTablet = true;
isPhone = false;
}
else {
isTablet = false;
isPhone = true;
}

或者要获得更全面的信息,您可以使用 device_info包。

关于 flutter 检测装置类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com