34 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
		
		
			
		
	
	
			34 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
|  | // lib/utils/platform_utils.dart
 | |||
|  | import 'package:flutter/foundation.dart' show kIsWeb, defaultTargetPlatform, TargetPlatform; | |||
|  | 
 | |||
|  | /// 简单的跨平台判断工具,避免在 web 上引用 dart:io
 | |||
|  | class PlatformUtils { | |||
|  |   /// 是否在浏览器
 | |||
|  |   static bool get isWeb => kIsWeb; | |||
|  | 
 | |||
|  |   /// 是否为 iOS(注意:在 web 上会返回 false)
 | |||
|  |   static bool get isIOS => !kIsWeb && defaultTargetPlatform == TargetPlatform.iOS; | |||
|  | 
 | |||
|  |   /// 是否为 Android(在 web 上会返回 false)
 | |||
|  |   static bool get isAndroid => !kIsWeb && defaultTargetPlatform == TargetPlatform.android; | |||
|  | 
 | |||
|  |   /// 返回一个简单的操作系统标识字符串(web 会返回 'web')
 | |||
|  |   static String get operatingSystem { | |||
|  |     if (kIsWeb) return 'web'; | |||
|  |     switch (defaultTargetPlatform) { | |||
|  |       case TargetPlatform.android: | |||
|  |         return 'android'; | |||
|  |       case TargetPlatform.iOS: | |||
|  |         return 'ios'; | |||
|  |       case TargetPlatform.macOS: | |||
|  |         return 'macos'; | |||
|  |       case TargetPlatform.linux: | |||
|  |         return 'linux'; | |||
|  |       case TargetPlatform.windows: | |||
|  |         return 'windows'; | |||
|  |       case TargetPlatform.fuchsia: | |||
|  |         return 'fuchsia'; | |||
|  |     } | |||
|  |   } | |||
|  | } |