【问题标题】:Flutter detecting device type颤振检测装置类型
【发布时间】:2020-07-29 10:17:15
【问题描述】:

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用flutter_device_type包中的以下代码sn-p:

    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包获取更全面的信息。

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 2021-05-05
      • 2018-08-18
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2020-09-29
      相关资源
      最近更新 更多