美文网首页
Flutter设备信息通用工具类封装

Flutter设备信息通用工具类封装

作者: 刘铁崧 | 来源:发表于2020-12-28 20:36 被阅读0次
  • 通用工具类封装
import 'dart:ui';
class CYScreenUtils{
  static double physicalWidth;// 物理屏幕宽
  static double physicalHeight;// 物理屏幕高
  static double dpr; // 屏幕宽高比
  static double screenWidth;// 屏幕宽
  static double screenHeight;// 屏幕高
  static double statusBarHeight;// 状态栏高度
  static void initialize(){
    // 物理分辨率
    physicalWidth = window.physicalSize.width;
    physicalHeight = window.physicalSize.height;
    print("物理分辨率:$physicalWidth x $physicalHeight(宽 x 高)");

    // dpr
    dpr = window.devicePixelRatio;

    // 屏幕宽高
    screenWidth = physicalWidth / dpr;
    screenHeight = physicalHeight / dpr;
    print("屏幕宽高:$screenWidth x $screenHeight(宽 x 高)");

    // 状态栏高度
    statusBarHeight = window.padding.top / dpr;
  }
}
  • 使用
    最外层widget的build方法中初始化
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    CYScreenUtils.init();
    return MaterialApp(...);
}

获取屏幕宽高

CYScreenUtils.screenHeight

相关文章

网友评论

      本文标题:Flutter设备信息通用工具类封装

      本文链接:https://www.haomeiwen.com/subject/jatnnktx.html