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

2020-12-28  本文已影响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;
  }
}
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    CYScreenUtils.init();
    return MaterialApp(...);
}

获取屏幕宽高

CYScreenUtils.screenHeight
上一篇 下一篇

猜你喜欢

热点阅读