app-starlock/lib/tools/screen_utils.dart

130 lines
4.6 KiB
Dart
Raw Permalink Normal View History

2023-07-10 17:50:31 +08:00
/// screen_utils.dart
///
/// Created by iotjin on 2020/07/04.
/// description: 屏幕信息
import 'dart:ui' as ui show window;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class JhScreenUtils {
2024-08-19 15:24:14 +08:00
static void init(BuildContext context) => ScreenUtil.init(context, designSize: const Size(750, 1334));
2023-07-10 17:50:31 +08:00
2024-08-19 15:24:14 +08:00
static double setWidth(double width) => ScreenUtil().setWidth(width);
2023-07-10 17:50:31 +08:00
2024-08-19 15:24:14 +08:00
static double setHeight(double height) => ScreenUtil().setHeight(height);
2023-07-10 17:50:31 +08:00
2024-08-19 15:24:14 +08:00
static double setSp(num fontSize) => ScreenUtil().setSp(fontSize);
2023-07-10 17:50:31 +08:00
// static double get screenWidth => ScreenUtil.screenWidth;
//
// static double get screenHeight => ScreenUtil.screenHeight;
// static double get screenWidthPx => ScreenUtil.screenWidthPx;
//
// static double get screenHeightPx => ScreenUtil.screenHeightPx;
//
// static double get statusBarHeight => ScreenUtil.statusBarHeight;
//
// static double get bottomBarHeight => ScreenUtil.bottomBarHeight;
// 系统方法获取
static double get screenWidth {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.size.width;
}
static double get screenHeight {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.size.height;
}
static double get scale {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.devicePixelRatio;
}
static double get textScaleFactor {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.textScaleFactor;
}
static double get navigationBarHeight {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.padding.top + kToolbarHeight;
}
static double get topSafeHeight {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.padding.top;
}
static double get bottomSafeHeight {
2024-08-19 15:24:14 +08:00
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
2023-07-10 17:50:31 +08:00
return mediaQuery.padding.bottom;
}
2024-08-19 15:24:14 +08:00
static void updateStatusBarStyle(SystemUiOverlayStyle style) => SystemChrome.setSystemUIOverlayStyle(style);
2023-07-10 17:50:31 +08:00
}
/*
ScreenUtil.pixelRatio // 设备的像素密度
ScreenUtil.screenWidth // 设备宽度
ScreenUtil.screenHeight // 设备高度
ScreenUtil.bottomBarHeight // 底部安全区距离,适用于全面屏下面有按键的
ScreenUtil.statusBarHeight // 状态栏高度 刘海屏会更高 单位px
ScreenUtil.textScaleFactory // 系统字体缩放比例
ScreenUtil.getInstance().scaleWidth // 实际宽度的dp与设计稿px的比例
ScreenUtil.getInstance().scaleHeight // 实际高度的dp与设计稿px的比例
width:ScreenUtil().setWidth(100)
height:ScreenUtil().setHeight(80)
Container(
width: 50.w,
height:200.h
)
fontSize: ScreenUtil().setSp(28)
fontSize: ScreenUtil.getInstance().setSp(24),
fontSize: ScreenUtil(allowFontScaling: true).setSp(24),
2024-04-26 15:38:59 +08:00
AppLog.log('设备宽度:${ScreenUtil.screenWidth}'); //Device width
AppLog.log('设备高度:${ScreenUtil.screenHeight}'); //Device height
AppLog.log('设备的像素密度:${ScreenUtil.pixelRatio}'); //Device pixel density
AppLog.log('底部安全区距离:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distancesuitable for buttons with full screen
AppLog.log('状态栏高度:${ScreenUtil.statusBarHeight}px');
AppLog.log('实际宽度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleWidth}');
AppLog.log('实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}');
AppLog.log( '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}');
AppLog.log( '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}');
AppLog.log('系统的字体缩放比例:${ScreenUtil.textScaleFactor}');
2023-07-10 17:50:31 +08:00
*/
/*
MediaQuery.of(context).size.width
MediaQuery.of(context).size.height
MediaQueryData.fromWindow(WidgetBinding.instance.window).padding.top
MediaQueryData mq = MediaQuery.of(context);
// 屏幕密度
pixelRatio = mq.devicePixelRatio;
// 屏幕宽(注意是dp, 转换px 需要 screenWidth * pixelRatio)
screenWidth = mq.size.width;
// 屏幕高(注意是dp)
screenHeight = mq.size.height;
// 顶部状态栏, 随着刘海屏会增高
statusBarHeight = mq padding.top;
// 底部功能栏, 类似于iPhone XR 底部安全区域
bottomBarHeight = mq.padding.bottom;
*/