情景推荐
This commit is contained in:
228
lib/controllers/settings/is_platform.dart
Normal file
228
lib/controllers/settings/is_platform.dart
Normal file
@@ -0,0 +1,228 @@
|
||||
import 'dart:io' as io show Platform;
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
|
||||
/// 时间: 2026-04-04
|
||||
/// 功能: 平台判断工具类
|
||||
/// 介绍: 统一管理平台判断逻辑,支持 Web、Android、iOS、HarmonyOS、Windows、macOS、Linux 等平台
|
||||
/// 最新变化: 新建文件,统一平台判断逻辑
|
||||
|
||||
class PlatformUtils {
|
||||
PlatformUtils._();
|
||||
|
||||
/// 是否是 Web 平台
|
||||
static bool get isWeb => kIsWeb;
|
||||
|
||||
/// 是否是移动平台(Android、iOS、HarmonyOS)
|
||||
static bool get isMobile => isAndroid || isIOS || isHarmonyOS;
|
||||
|
||||
/// 是否是桌面平台(Windows、macOS、Linux)
|
||||
static bool get isDesktop => isWindows || isMacOS || isLinux;
|
||||
|
||||
/// 是否是 Android 平台
|
||||
static bool get isAndroid {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
return io.Platform.isAndroid;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否是 iOS 平台
|
||||
static bool get isIOS {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
return io.Platform.isIOS;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否是鸿蒙平台
|
||||
static bool get isHarmonyOS {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
final osName = io.Platform.operatingSystem.toLowerCase();
|
||||
return osName == 'ohos' ||
|
||||
osName == 'harmonyos' ||
|
||||
osName == 'openharmony';
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否是 Windows 平台
|
||||
static bool get isWindows {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
return io.Platform.isWindows;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否是 macOS 平台
|
||||
static bool get isMacOS {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
return io.Platform.isMacOS;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否是 Linux 平台
|
||||
static bool get isLinux {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
return io.Platform.isLinux;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否是 Fuchsia 平台
|
||||
static bool get isFuchsia {
|
||||
if (kIsWeb) return false;
|
||||
try {
|
||||
return io.Platform.isFuchsia;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取操作系统名称
|
||||
static String get operatingSystem {
|
||||
if (kIsWeb) return 'web';
|
||||
try {
|
||||
return io.Platform.operatingSystem;
|
||||
} catch (e) {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取操作系统版本
|
||||
static String get operatingSystemVersion {
|
||||
if (kIsWeb) return 'web browser';
|
||||
try {
|
||||
return io.Platform.operatingSystemVersion;
|
||||
} catch (e) {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取平台显示名称(带 Flutter 后缀)
|
||||
static String get platformDisplayName {
|
||||
if (kIsWeb) return 'Web Flutter';
|
||||
|
||||
try {
|
||||
final osName = io.Platform.operatingSystem.toLowerCase();
|
||||
|
||||
if (osName == 'ohos' ||
|
||||
osName == 'harmonyos' ||
|
||||
osName == 'openharmony') {
|
||||
return 'HarmonyOS Flutter';
|
||||
} else if (io.Platform.isAndroid) {
|
||||
return 'Android Flutter';
|
||||
} else if (io.Platform.isIOS) {
|
||||
return 'iOS Flutter';
|
||||
} else if (io.Platform.isWindows) {
|
||||
return 'Windows Flutter';
|
||||
} else if (io.Platform.isMacOS) {
|
||||
return 'macOS Flutter';
|
||||
} else if (io.Platform.isLinux) {
|
||||
return 'Linux Flutter';
|
||||
} else if (io.Platform.isFuchsia) {
|
||||
return 'Fuchsia Flutter';
|
||||
} else {
|
||||
return 'Flutter';
|
||||
}
|
||||
} catch (e) {
|
||||
return 'Flutter';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取简短的平台名称
|
||||
static String get platformShortName {
|
||||
if (kIsWeb) return 'Web';
|
||||
|
||||
try {
|
||||
final osName = io.Platform.operatingSystem.toLowerCase();
|
||||
|
||||
if (osName == 'ohos' ||
|
||||
osName == 'harmonyos' ||
|
||||
osName == 'openharmony') {
|
||||
return 'HarmonyOS';
|
||||
} else if (io.Platform.isAndroid) {
|
||||
return 'Android';
|
||||
} else if (io.Platform.isIOS) {
|
||||
return 'iOS';
|
||||
} else if (io.Platform.isWindows) {
|
||||
return 'Windows';
|
||||
} else if (io.Platform.isMacOS) {
|
||||
return 'macOS';
|
||||
} else if (io.Platform.isLinux) {
|
||||
return 'Linux';
|
||||
} else if (io.Platform.isFuchsia) {
|
||||
return 'Fuchsia';
|
||||
} else {
|
||||
return 'Unknown';
|
||||
}
|
||||
} catch (e) {
|
||||
return 'Unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取路径分隔符
|
||||
static String get pathSeparator {
|
||||
if (kIsWeb) return '/';
|
||||
try {
|
||||
return io.Platform.pathSeparator;
|
||||
} catch (e) {
|
||||
return '/';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取本地主机名
|
||||
static String get localHostname {
|
||||
if (kIsWeb) return 'web';
|
||||
try {
|
||||
return io.Platform.localHostname;
|
||||
} catch (e) {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取环境变量
|
||||
static Map<String, String> get environment {
|
||||
if (kIsWeb) return {};
|
||||
try {
|
||||
return io.Platform.environment;
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取 Dart 版本
|
||||
static String get dartVersion {
|
||||
if (kIsWeb) return 'web';
|
||||
try {
|
||||
return io.Platform.version;
|
||||
} catch (e) {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取平台信息摘要
|
||||
static String get platformSummary {
|
||||
if (kIsWeb) return 'Web Browser';
|
||||
|
||||
try {
|
||||
final osName = operatingSystem;
|
||||
final osVersion = operatingSystemVersion;
|
||||
return '$osName ($osVersion)';
|
||||
} catch (e) {
|
||||
return 'Unknown Platform';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user