flutter_integrated_whb/lib/tools/platform_utils.dart

34 lines
1.1 KiB
Dart
Raw Normal View History

2025-09-23 10:31:41 +08:00
// 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';
}
}
}