Skip to content

Commit 5365100

Browse files
author
Mounir Bouaiche
committed
Little code solidity
1 parent 6fed40a commit 5365100

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

lib/src/screen_util.dart

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,7 @@ class ScreenUtil {
2525

2626
ScreenUtil._();
2727

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;
4729

4830
/// Manually wait for window size to be initialized
4931
///
@@ -79,7 +61,7 @@ class ScreenUtil {
7961
window = binding.platformDispatcher.implicitView;
8062
}
8163

82-
if (window == null || window!.physicalGeometry.isEmpty == true) {
64+
if (window == null || window!.physicalGeometry.isEmpty) {
8365
return Future.delayed(duration, () => true);
8466
}
8567

@@ -107,19 +89,32 @@ class ScreenUtil {
10789
}
10890
}
10991

110-
/// Initializing the library.
111-
static void configure({
92+
static Future<void> configure({
11293
MediaQueryData? data,
11394
Size? designSize,
11495
bool? splitScreenMode,
11596
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+
}
121115

122-
if (designSize != null) _instance._uiSize = designSize;
116+
final MediaQueryData? deviceData = data.nonEmptySizeOrNull();
117+
final Size deviceSize = deviceData?.size ?? designSize;
123118

124119
final orientation = deviceData?.orientation ??
125120
(deviceSize.width > deviceSize.height
@@ -134,6 +129,23 @@ class ScreenUtil {
134129
_instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild());
135130
}
136131

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+
137149
///获取屏幕方向
138150
///Get screen orientation
139151
Orientation get orientation => _orientation;
@@ -166,7 +178,7 @@ class ScreenUtil {
166178
/// The ratio of actual width to UI design
167179
double get scaleWidth => screenWidth / _uiSize.width;
168180

169-
/// /// The ratio of actual height to UI design
181+
/// The ratio of actual height to UI design
170182
double get scaleHeight =>
171183
(_splitScreenMode ? max(screenHeight, 700) : screenHeight) /
172184
_uiSize.height;

lib/src/screenutil_init.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ScreenUtilInit extends StatefulWidget {
5050
this.splitScreenMode = false,
5151
this.minTextAdapt = false,
5252
this.useInheritedMediaQuery = false,
53+
this.ensureScreenSize,
5354
this.responsiveWidgets,
5455
}) : super(key: key);
5556

@@ -58,6 +59,7 @@ class ScreenUtilInit extends StatefulWidget {
5859
final bool splitScreenMode;
5960
final bool minTextAdapt;
6061
final bool useInheritedMediaQuery;
62+
final bool? ensureScreenSize;
6163
final RebuildFactor rebuildFactor;
6264

6365
/// The [Size] of the device in the design draft, in dp
@@ -143,6 +145,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit>
143145
designSize: widget.designSize,
144146
splitScreenMode: widget.splitScreenMode,
145147
minTextAdapt: widget.minTextAdapt,
148+
ensureScreenHasSize: widget.ensureScreenSize,
146149
);
147150

148151
return widget.builder?.call(context, widget.child) ?? widget.child!;

0 commit comments

Comments
 (0)