app-starlock/lib/tools/screen_utils.dart
2024-08-19 15:24:14 +08:00

130 lines
4.6 KiB
Dart
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// 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 {
static void init(BuildContext context) => ScreenUtil.init(context, designSize: const Size(750, 1334));
static double setWidth(double width) => ScreenUtil().setWidth(width);
static double setHeight(double height) => ScreenUtil().setHeight(height);
static double setSp(num fontSize) => ScreenUtil().setSp(fontSize);
// 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 {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.size.width;
}
static double get screenHeight {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.size.height;
}
static double get scale {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.devicePixelRatio;
}
static double get textScaleFactor {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.textScaleFactor;
}
static double get navigationBarHeight {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.padding.top + kToolbarHeight;
}
static double get topSafeHeight {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.padding.top;
}
static double get bottomSafeHeight {
final MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
return mediaQuery.padding.bottom;
}
static void updateStatusBarStyle(SystemUiOverlayStyle style) => SystemChrome.setSystemUIOverlayStyle(style);
}
/*
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),
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}');
*/
/*
屏幕宽度高度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;
*/