Showing
1 changed file
with
34 additions
and
27 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; | ||
| 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; | ||
| 255 | } | 275 | } |
| 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 | + } |
| 271 | } | 277 | } |
| 278 | + | ||
| 272 | return deviceType; | 279 | return deviceType; |
| 273 | } | 280 | } |
| 274 | 281 |
-
Please register or login to post a comment