Skip to content

Commit

Permalink
Little code solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mounir Bouaiche committed Jun 11, 2023
1 parent 6fed40a commit 5365100
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
70 changes: 41 additions & 29 deletions lib/src/screen_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,7 @@ class ScreenUtil {

ScreenUtil._();

factory ScreenUtil() {
return _instance;
}

factory ScreenUtil.init(
BuildContext context, {
Size designSize = defaultSize,
bool splitScreenMode = false,
bool minTextAdapt = false,
}) {
configure(
data: MediaQuery.maybeOf(context),
designSize: designSize,
minTextAdapt: minTextAdapt,
splitScreenMode: splitScreenMode,
);

return _instance;
}
factory ScreenUtil() => _instance;

/// Manually wait for window size to be initialized
///
Expand Down Expand Up @@ -79,7 +61,7 @@ class ScreenUtil {
window = binding.platformDispatcher.implicitView;
}

if (window == null || window!.physicalGeometry.isEmpty == true) {
if (window == null || window!.physicalGeometry.isEmpty) {
return Future.delayed(duration, () => true);
}

Expand Down Expand Up @@ -107,19 +89,32 @@ class ScreenUtil {
}
}

/// Initializing the library.
static void configure({
static Future<void> configure({
MediaQueryData? data,
Size? designSize,
bool? splitScreenMode,
bool? minTextAdapt,
}) {
if (data != null) _instance._data = data;

final deviceData = _instance._data.nonEmptySizeOrNull();
final deviceSize = deviceData?.size ?? designSize ?? _instance._uiSize;
bool? ensureScreenHasSize,
}) async {
if (ensureScreenHasSize ?? false) await ScreenUtil.ensureScreenSize();

try {
if (data != null)
_instance._data = data;
else
data = _instance._data;

if (designSize != null)
_instance._uiSize = designSize;
else
designSize = _instance._uiSize;
} catch (_) {
throw Exception(
'You must either use ScreenUtil.init or ScreenUtilInit first');
}

if (designSize != null) _instance._uiSize = designSize;
final MediaQueryData? deviceData = data.nonEmptySizeOrNull();
final Size deviceSize = deviceData?.size ?? designSize;

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

/// Initializing the library.
static Future<void> init(
BuildContext context, {
Size designSize = defaultSize,
bool splitScreenMode = false,
bool minTextAdapt = false,
bool ensureScreenSize = false,
}) {
return configure(
data: MediaQuery.maybeOf(context),
designSize: designSize,
minTextAdapt: minTextAdapt,
splitScreenMode: splitScreenMode,
ensureScreenHasSize: ensureScreenSize,
);
}

///获取屏幕方向
///Get screen orientation
Orientation get orientation => _orientation;
Expand Down Expand Up @@ -166,7 +178,7 @@ class ScreenUtil {
/// The ratio of actual width to UI design
double get scaleWidth => screenWidth / _uiSize.width;

/// /// The ratio of actual height to UI design
/// The ratio of actual height to UI design
double get scaleHeight =>
(_splitScreenMode ? max(screenHeight, 700) : screenHeight) /
_uiSize.height;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/screenutil_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ScreenUtilInit extends StatefulWidget {
this.splitScreenMode = false,
this.minTextAdapt = false,
this.useInheritedMediaQuery = false,
this.ensureScreenSize,
this.responsiveWidgets,
}) : super(key: key);

Expand All @@ -58,6 +59,7 @@ class ScreenUtilInit extends StatefulWidget {
final bool splitScreenMode;
final bool minTextAdapt;
final bool useInheritedMediaQuery;
final bool? ensureScreenSize;
final RebuildFactor rebuildFactor;

/// The [Size] of the device in the design draft, in dp
Expand Down Expand Up @@ -143,6 +145,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit>
designSize: widget.designSize,
splitScreenMode: widget.splitScreenMode,
minTextAdapt: widget.minTextAdapt,
ensureScreenHasSize: widget.ensureScreenSize,
);

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

0 comments on commit 5365100

Please sign in to comment.