Skip to content

Commit 987c462

Browse files
remove dart:io for support web (#550)
1 parent 6bafe6f commit 987c462

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

lib/src/screen_util.dart

+34-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
44
*/
55

6-
import 'dart:io';
6+
import 'package:flutter/foundation.dart' show kIsWeb, defaultTargetPlatform;
7+
78
import 'dart:math' show min, max;
89
import 'dart:ui' as ui show FlutterView;
910

@@ -241,34 +242,40 @@ class ScreenUtil {
241242
double setSp(num fontSize) =>
242243
fontSizeResolver?.call(fontSize, _instance) ?? fontSize * scaleText;
243244

244-
DeviceType deviceType() {
245-
DeviceType deviceType;
246-
switch (Platform.operatingSystem) {
247-
case 'android':
248-
case 'ios':
249-
deviceType = DeviceType.mobile;
250-
if ((orientation == Orientation.portrait && screenWidth < 600) ||
251-
(orientation == Orientation.landscape && screenHeight < 600)) {
252-
deviceType = DeviceType.mobile;
253-
} else {
254-
deviceType = DeviceType.tablet;
245+
DeviceType deviceType(BuildContext context) {
246+
var deviceType = DeviceType.web;
247+
final screenWidth = MediaQuery.of(context).size.width;
248+
final screenHeight = MediaQuery.of(context).size.height;
249+
final orientation = MediaQuery.of(context).orientation;
250+
251+
if (kIsWeb) {
252+
deviceType = DeviceType.web;
253+
} else {
254+
bool isMobile = defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.android;
255+
bool isTablet = (orientation == Orientation.portrait && screenWidth >= 600) || (orientation == Orientation.landscape && screenHeight >= 600);
256+
257+
if (isMobile) {
258+
deviceType = isTablet ? DeviceType.tablet : DeviceType.mobile;
259+
} else {
260+
switch (defaultTargetPlatform) {
261+
case TargetPlatform.linux:
262+
deviceType = DeviceType.linux;
263+
break;
264+
case TargetPlatform.macOS:
265+
deviceType = DeviceType.mac;
266+
break;
267+
case TargetPlatform.windows:
268+
deviceType = DeviceType.windows;
269+
break;
270+
case TargetPlatform.fuchsia:
271+
deviceType = DeviceType.fuchsia;
272+
break;
273+
default:
274+
break;
255275
}
256-
break;
257-
case 'linux':
258-
deviceType = DeviceType.linux;
259-
break;
260-
case 'macos':
261-
deviceType = DeviceType.mac;
262-
break;
263-
case 'windows':
264-
deviceType = DeviceType.windows;
265-
break;
266-
case 'fuchsia':
267-
deviceType = DeviceType.fuchsia;
268-
break;
269-
default:
270-
deviceType = DeviceType.web;
276+
}
271277
}
278+
272279
return deviceType;
273280
}
274281

0 commit comments

Comments
 (0)