flutter_integrated_whb/lib/tools/platform_utils.dart

34 lines
1.1 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 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';
}
}
}