- 通用工具类封装
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
网友评论