li_zy
Committed by GitHub

使用饿汉单例模式

1 -/*  
2 - * Created by 李卓原 on 2018/9/24. 1 +/*
  2 + * Created by 李卓原 on 2018/9/20.
3 * email: zhuoyuan93@gmail.com 3 * email: zhuoyuan93@gmail.com
4 * 设计稿设备尺寸默认为 1080 * 1920 px 4 * 设计稿设备尺寸默认为 1080 * 1920 px
5 */ 5 */
@@ -7,10 +7,12 @@ @@ -7,10 +7,12 @@
7 import 'dart:ui'; 7 import 'dart:ui';
8 8
9 class ScreenUtil { 9 class ScreenUtil {
10 - //设计稿的设备尺寸修改 (请修改成你的设计稿的尺寸) 10 + //请修改成你的设计稿的尺寸
  11 + static ScreenUtil instance = new ScreenUtil(width: 1080, height: 1920);
  12 +
  13 + //设计稿的设备尺寸修改
11 int designWidth; 14 int designWidth;
12 int designHeight; 15 int designHeight;
13 - static ScreenUtil instance;  
14 16
15 ScreenUtil({int width, int height}) { 17 ScreenUtil({int width, int height}) {
16 designWidth = width; 18 designWidth = width;
@@ -18,9 +20,6 @@ class ScreenUtil { @@ -18,9 +20,6 @@ class ScreenUtil {
18 } 20 }
19 21
20 static ScreenUtil getInstance() { 22 static ScreenUtil getInstance() {
21 - if (instance == null) {  
22 - instance = new ScreenUtil(width: 1080, height: 1920);  
23 - }  
24 return instance; 23 return instance;
25 } 24 }
26 25
@@ -40,11 +39,9 @@ class ScreenUtil { @@ -40,11 +39,9 @@ class ScreenUtil {
40 static double bottomBarHeight = window.padding.bottom; 39 static double bottomBarHeight = window.padding.bottom;
41 40
42 //相对于设计稿放大的倍数 41 //相对于设计稿放大的倍数
43 - double get scaleWidth =>  
44 - screenWidth / ScreenUtil.getInstance().designWidth / pixelRatio; 42 + double get scaleWidth => screenWidth / instance.designWidth / pixelRatio;
45 43
46 - double get scaleHeight =>  
47 - screenHeight / ScreenUtil.getInstance().designHeight / pixelRatio; 44 + double get scaleHeight => screenHeight / instance.designHeight / pixelRatio;
48 45
49 /** 46 /**
50 * 根据设计稿的设备宽度适配 47 * 根据设计稿的设备宽度适配