@@ -25,25 +25,7 @@ class ScreenUtil {
25
25
26
26
ScreenUtil ._();
27
27
28
- factory ScreenUtil () {
29
- return _instance;
30
- }
31
-
32
- factory ScreenUtil .init (
33
- BuildContext context, {
34
- Size designSize = defaultSize,
35
- bool splitScreenMode = false ,
36
- bool minTextAdapt = false ,
37
- }) {
38
- configure (
39
- data: MediaQuery .maybeOf (context),
40
- designSize: designSize,
41
- minTextAdapt: minTextAdapt,
42
- splitScreenMode: splitScreenMode,
43
- );
44
-
45
- return _instance;
46
- }
28
+ factory ScreenUtil () => _instance;
47
29
48
30
/// Manually wait for window size to be initialized
49
31
///
@@ -79,7 +61,7 @@ class ScreenUtil {
79
61
window = binding.platformDispatcher.implicitView;
80
62
}
81
63
82
- if (window == null || window! .physicalGeometry.isEmpty == true ) {
64
+ if (window == null || window! .physicalGeometry.isEmpty) {
83
65
return Future .delayed (duration, () => true );
84
66
}
85
67
@@ -107,19 +89,32 @@ class ScreenUtil {
107
89
}
108
90
}
109
91
110
- /// Initializing the library.
111
- static void configure ({
92
+ static Future <void > configure ({
112
93
MediaQueryData ? data,
113
94
Size ? designSize,
114
95
bool ? splitScreenMode,
115
96
bool ? minTextAdapt,
116
- }) {
117
- if (data != null ) _instance._data = data;
118
-
119
- final deviceData = _instance._data.nonEmptySizeOrNull ();
120
- final deviceSize = deviceData? .size ?? designSize ?? _instance._uiSize;
97
+ bool ? ensureScreenHasSize,
98
+ }) async {
99
+ if (ensureScreenHasSize ?? false ) await ScreenUtil .ensureScreenSize ();
100
+
101
+ try {
102
+ if (data != null )
103
+ _instance._data = data;
104
+ else
105
+ data = _instance._data;
106
+
107
+ if (designSize != null )
108
+ _instance._uiSize = designSize;
109
+ else
110
+ designSize = _instance._uiSize;
111
+ } catch (_) {
112
+ throw Exception (
113
+ 'You must either use ScreenUtil.init or ScreenUtilInit first' );
114
+ }
121
115
122
- if (designSize != null ) _instance._uiSize = designSize;
116
+ final MediaQueryData ? deviceData = data.nonEmptySizeOrNull ();
117
+ final Size deviceSize = deviceData? .size ?? designSize;
123
118
124
119
final orientation = deviceData? .orientation ??
125
120
(deviceSize.width > deviceSize.height
@@ -134,6 +129,23 @@ class ScreenUtil {
134
129
_instance._elementsToRebuild? .forEach ((el) => el.markNeedsBuild ());
135
130
}
136
131
132
+ /// Initializing the library.
133
+ static Future <void > init (
134
+ BuildContext context, {
135
+ Size designSize = defaultSize,
136
+ bool splitScreenMode = false ,
137
+ bool minTextAdapt = false ,
138
+ bool ensureScreenSize = false ,
139
+ }) {
140
+ return configure (
141
+ data: MediaQuery .maybeOf (context),
142
+ designSize: designSize,
143
+ minTextAdapt: minTextAdapt,
144
+ splitScreenMode: splitScreenMode,
145
+ ensureScreenHasSize: ensureScreenSize,
146
+ );
147
+ }
148
+
137
149
///获取屏幕方向
138
150
///Get screen orientation
139
151
Orientation get orientation => _orientation;
@@ -166,7 +178,7 @@ class ScreenUtil {
166
178
/// The ratio of actual width to UI design
167
179
double get scaleWidth => screenWidth / _uiSize.width;
168
180
169
- /// /// The ratio of actual height to UI design
181
+ /// The ratio of actual height to UI design
170
182
double get scaleHeight =>
171
183
(_splitScreenMode ? max (screenHeight, 700 ) : screenHeight) /
172
184
_uiSize.height;
0 commit comments