Showing
2 changed files
with
43 additions
and
28 deletions
@@ -25,25 +25,7 @@ class ScreenUtil { | @@ -25,25 +25,7 @@ class ScreenUtil { | ||
25 | 25 | ||
26 | ScreenUtil._(); | 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 | /// Manually wait for window size to be initialized | 30 | /// Manually wait for window size to be initialized |
49 | /// | 31 | /// |
@@ -79,7 +61,7 @@ class ScreenUtil { | @@ -79,7 +61,7 @@ class ScreenUtil { | ||
79 | window = binding.platformDispatcher.implicitView; | 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 | return Future.delayed(duration, () => true); | 65 | return Future.delayed(duration, () => true); |
84 | } | 66 | } |
85 | 67 | ||
@@ -107,19 +89,32 @@ class ScreenUtil { | @@ -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 | MediaQueryData? data, | 93 | MediaQueryData? data, |
113 | Size? designSize, | 94 | Size? designSize, |
114 | bool? splitScreenMode, | 95 | bool? splitScreenMode, |
115 | bool? minTextAdapt, | 96 | bool? minTextAdapt, |
116 | - }) { | ||
117 | - if (data != null) _instance._data = data; | 97 | + bool? ensureScreenHasSize, |
98 | + }) async { | ||
99 | + if (ensureScreenHasSize ?? false) await ScreenUtil.ensureScreenSize(); | ||
118 | 100 | ||
119 | - final deviceData = _instance._data.nonEmptySizeOrNull(); | ||
120 | - final deviceSize = deviceData?.size ?? designSize ?? _instance._uiSize; | 101 | + try { |
102 | + if (data != null) | ||
103 | + _instance._data = data; | ||
104 | + else | ||
105 | + data = _instance._data; | ||
121 | 106 | ||
122 | - if (designSize != null) _instance._uiSize = designSize; | 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 | + } | ||
115 | + | ||
116 | + final MediaQueryData? deviceData = data.nonEmptySizeOrNull(); | ||
117 | + final Size deviceSize = deviceData?.size ?? designSize; | ||
123 | 118 | ||
124 | final orientation = deviceData?.orientation ?? | 119 | final orientation = deviceData?.orientation ?? |
125 | (deviceSize.width > deviceSize.height | 120 | (deviceSize.width > deviceSize.height |
@@ -134,6 +129,23 @@ class ScreenUtil { | @@ -134,6 +129,23 @@ class ScreenUtil { | ||
134 | _instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild()); | 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 | ///Get screen orientation | 150 | ///Get screen orientation |
139 | Orientation get orientation => _orientation; | 151 | Orientation get orientation => _orientation; |
@@ -166,7 +178,7 @@ class ScreenUtil { | @@ -166,7 +178,7 @@ class ScreenUtil { | ||
166 | /// The ratio of actual width to UI design | 178 | /// The ratio of actual width to UI design |
167 | double get scaleWidth => screenWidth / _uiSize.width; | 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 | double get scaleHeight => | 182 | double get scaleHeight => |
171 | (_splitScreenMode ? max(screenHeight, 700) : screenHeight) / | 183 | (_splitScreenMode ? max(screenHeight, 700) : screenHeight) / |
172 | _uiSize.height; | 184 | _uiSize.height; |
@@ -50,6 +50,7 @@ class ScreenUtilInit extends StatefulWidget { | @@ -50,6 +50,7 @@ class ScreenUtilInit extends StatefulWidget { | ||
50 | this.splitScreenMode = false, | 50 | this.splitScreenMode = false, |
51 | this.minTextAdapt = false, | 51 | this.minTextAdapt = false, |
52 | this.useInheritedMediaQuery = false, | 52 | this.useInheritedMediaQuery = false, |
53 | + this.ensureScreenSize, | ||
53 | this.responsiveWidgets, | 54 | this.responsiveWidgets, |
54 | }) : super(key: key); | 55 | }) : super(key: key); |
55 | 56 | ||
@@ -58,6 +59,7 @@ class ScreenUtilInit extends StatefulWidget { | @@ -58,6 +59,7 @@ class ScreenUtilInit extends StatefulWidget { | ||
58 | final bool splitScreenMode; | 59 | final bool splitScreenMode; |
59 | final bool minTextAdapt; | 60 | final bool minTextAdapt; |
60 | final bool useInheritedMediaQuery; | 61 | final bool useInheritedMediaQuery; |
62 | + final bool? ensureScreenSize; | ||
61 | final RebuildFactor rebuildFactor; | 63 | final RebuildFactor rebuildFactor; |
62 | 64 | ||
63 | /// The [Size] of the device in the design draft, in dp | 65 | /// The [Size] of the device in the design draft, in dp |
@@ -143,6 +145,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -143,6 +145,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
143 | designSize: widget.designSize, | 145 | designSize: widget.designSize, |
144 | splitScreenMode: widget.splitScreenMode, | 146 | splitScreenMode: widget.splitScreenMode, |
145 | minTextAdapt: widget.minTextAdapt, | 147 | minTextAdapt: widget.minTextAdapt, |
148 | + ensureScreenHasSize: widget.ensureScreenSize, | ||
146 | ); | 149 | ); |
147 | 150 | ||
148 | return widget.builder?.call(context, widget.child) ?? widget.child!; | 151 | return widget.builder?.call(context, widget.child) ?? widget.child!; |
-
Please register or login to post a comment