Showing
1 changed file
with
25 additions
and
18 deletions
@@ -3,7 +3,8 @@ | @@ -3,7 +3,8 @@ | ||
3 | * email: zhuoyuan93@gmail.com | 3 | * email: zhuoyuan93@gmail.com |
4 | */ | 4 | */ |
5 | 5 | ||
6 | -import 'dart:io'; | 6 | +import 'package:flutter/foundation.dart' show kIsWeb, defaultTargetPlatform; |
7 | + | ||
7 | import 'dart:math' show min, max; | 8 | import 'dart:math' show min, max; |
8 | import 'dart:ui' as ui show FlutterView; | 9 | import 'dart:ui' as ui show FlutterView; |
9 | 10 | ||
@@ -241,34 +242,40 @@ class ScreenUtil { | @@ -241,34 +242,40 @@ class ScreenUtil { | ||
241 | double setSp(num fontSize) => | 242 | double setSp(num fontSize) => |
242 | fontSizeResolver?.call(fontSize, _instance) ?? fontSize * scaleText; | 243 | fontSizeResolver?.call(fontSize, _instance) ?? fontSize * scaleText; |
243 | 244 | ||
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; | 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 { | 253 | } else { |
254 | - deviceType = DeviceType.tablet; | ||
255 | - } | ||
256 | - break; | ||
257 | - case 'linux': | 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: | ||
258 | deviceType = DeviceType.linux; | 262 | deviceType = DeviceType.linux; |
259 | break; | 263 | break; |
260 | - case 'macos': | 264 | + case TargetPlatform.macOS: |
261 | deviceType = DeviceType.mac; | 265 | deviceType = DeviceType.mac; |
262 | break; | 266 | break; |
263 | - case 'windows': | 267 | + case TargetPlatform.windows: |
264 | deviceType = DeviceType.windows; | 268 | deviceType = DeviceType.windows; |
265 | break; | 269 | break; |
266 | - case 'fuchsia': | 270 | + case TargetPlatform.fuchsia: |
267 | deviceType = DeviceType.fuchsia; | 271 | deviceType = DeviceType.fuchsia; |
268 | break; | 272 | break; |
269 | default: | 273 | default: |
270 | - deviceType = DeviceType.web; | 274 | + break; |
271 | } | 275 | } |
276 | + } | ||
277 | + } | ||
278 | + | ||
272 | return deviceType; | 279 | return deviceType; |
273 | } | 280 | } |
274 | 281 |
-
Please register or login to post a comment