-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathflutter_screenutil.dart
108 lines (85 loc) · 3.54 KB
/
flutter_screenutil.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import 'package:flutter/material.dart';
///
/// //填入设计稿中设备的屏幕尺寸
///
///默认 width : 1080px , height:1920px , allowFontScaling:false
///ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
///
///假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
///ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
///
///传入设计稿的px尺寸:
///
///根据屏幕宽度适配 width: ScreenUtil.getInstance().setWidth(540),
///
///根据屏幕高度适配 height: ScreenUtil.getInstance().setHeight(200),
///
///传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
///ScreenUtil.getInstance().setSp(28)
class ScreenUtil {
static ScreenUtil instance = new ScreenUtil();
//设计稿的设备尺寸修改
double width;
double height;
bool allowFontScaling;
static MediaQueryData _mediaQueryData;
static double _screenWidth;
static double _screenHeight;
static double _pixelRatio;
static double _statusBarHeight;
static double _bottomBarHeight;
static double _textScaleFactor;
ScreenUtil({
this.width = 1080,
this.height = 1920,
this.allowFontScaling = false,
});
static ScreenUtil getInstance() {
return instance;
}
void init(BuildContext context) {
MediaQueryData mediaQuery = MediaQuery.of(context);
_mediaQueryData = mediaQuery;
_pixelRatio = mediaQuery.devicePixelRatio;
_screenWidth = mediaQuery.size.width;
_screenHeight = mediaQuery.size.height;
_statusBarHeight = mediaQuery.padding.top;
_bottomBarHeight = _mediaQueryData.padding.bottom;
_textScaleFactor = mediaQuery.textScaleFactor;
}
static MediaQueryData get mediaQueryData => _mediaQueryData;
///每个逻辑像素的字体像素数,字体的缩放比例
static double get textScaleFactory => _textScaleFactor;
///设备的像素密度
static double get pixelRatio => _pixelRatio;
///当前设备宽度 dp
static double get screenWidthDp => _screenWidth;
///当前设备高度 dp
static double get screenHeightDp => _screenHeight;
///当前设备宽度 px
static double get screenWidth => _screenWidth * _pixelRatio;
///当前设备高度 px
static double get screenHeight => _screenHeight * _pixelRatio;
///状态栏高度 dp 刘海屏会更高
static double get statusBarHeight => _statusBarHeight;
///底部安全区距离 dp
static double get bottomBarHeight => _bottomBarHeight;
///实际的dp与设计稿px的比例
get scaleWidth => _screenWidth / instance.width;
get scaleHeight => _screenHeight / instance.height;
///根据设计稿的设备宽度适配
///高度也根据这个来做适配可以保证不变形
setWidth(double width) => width * scaleWidth;
/// 根据设计稿的设备高度适配
/// 当发现设计稿中的一屏显示的与当前样式效果不符合时,
/// 或者形状有差异时,高度适配建议使用此方法
/// 高度适配主要针对想根据设计稿的一屏展示一样的效果
setHeight(double height) => height * scaleHeight;
///字体大小适配方法
///@param fontSize 传入设计稿上字体的px ,
///@param allowFontScaling 控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为false。
///@param allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
setSp(double fontSize) => allowFontScaling
? setWidth(fontSize)
: setWidth(fontSize) / _textScaleFactor;
}