flutter_screenutil.dart
2.1 KB
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
/*
* Created by 李卓原 on 2018/9/29.
* email: zhuoyuan93@gmail.com
*/
import 'package:flutter/material.dart';
class ScreenUtil {
static ScreenUtil instance = new ScreenUtil();
//设计稿的设备尺寸修改
int _designWidth;
int _designHeight;
static MediaQueryData _mediaQueryData;
static double _screenWidth;
static double _screenHeight;
static double _pixelRatio;
static double _statusBarHeight;
static double _bottomBarHeight;
ScreenUtil({int width, int height}) {
_designWidth = width;
_designHeight = height;
}
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;
}
static MediaQueryData get mediaQueryData => _mediaQueryData;
///设备的像素密度
static double get pixelRatio => _pixelRatio;
///当前设备宽度 px
static double get screenWidth => _screenWidth * _pixelRatio;
///当前设备高度 px
static double get screenHeight => _screenHeight * _pixelRatio;
///状态栏高度 刘海屏会更高
static double get statusBarHeight => _statusBarHeight * _pixelRatio;
///底部安全区距离
static double get bottomBarHeight => _bottomBarHeight * _pixelRatio;
///相对于设计稿放大的倍数
get scaleWidth => _screenWidth / instance._designWidth;
get scaleHeight => _screenHeight / instance._designHeight;
///根据设计稿的设备宽度适配
///高度也根据这个来做适配可以保证不变形
setWidth(int width) => width * scaleWidth;
/// 根据设计稿的设备高度适配
/// 当发现设计稿中的一屏显示的与当前样式效果不符合时,
/// 或者形状有差异时,高度适配建议使用此方法
/// 高度适配主要针对想根据设计稿的一屏展示一样的效果
setHeight(int height) => height * scaleHeight;
}