os_type 0.3.0
os_type: ^0.3.0 copied to clipboard
Useful for OS and web environment detection, supports HarmonyOS.
os_type #
🌐en:
Useful for OS and web environment detection, supports HarmonyOS
🌐 中文:
用于检测操作系统类型和 web 环境,支持鸿蒙
包本身是纯 Dart 实现,不依赖 Flutter SDK,可用于 Dart CLI / 服务端项目,也可直接用于 Flutter。
下面的示例界面用 Flutter 只是为了展示。OS.value 是系统名称字符串,如 macos、ohos,web 环境为 web。
example: #
import 'package:flutter/material.dart';
import 'package:os_type/os_type.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// 鸿蒙设备类型(如 2in1、tablet、phone)现在同步获取,无需再初始化
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('os type example')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('is android: ${OS.isAndroid}'),
SizedBox(height: 8),
Text('is fuchsia: ${OS.isFuchsia}'),
SizedBox(height: 8),
Text('is iOS: ${OS.isIOS}'),
SizedBox(height: 8),
Text('is linux: ${OS.isLinux}'),
SizedBox(height: 8),
Text('is macOS: ${OS.isMacOS}'),
SizedBox(height: 8),
Text('is windows: ${OS.isWindows}'),
SizedBox(height: 8),
Text('is ohos: ${OS.isHarmony}'),
SizedBox(height: 8),
Text('harmony device type: ${OS.harmonyDeviceType}'),
SizedBox(height: 8),
// 上面的列举有更简单的写法:
// OS.value 是系统名称字符串,如 'macos'、'ohos'
// Text('os: ${OS.value}'),
// pc和mobile是相反的,而web环境则等于kIsWeb,与操作系统无关
SizedBox(height: 20),
Text('is PC: ${OS.isPCOS}'),
SizedBox(height: 8),
Text('is Mobile: ${OS.isMobileOS}'), // isMobileOS == !isPCOS
SizedBox(height: 8),
Text('is Web environment: ${OS.isWebEnv}'), // isWebEnv == kIsWeb
SizedBox(height: 8),
],
),
),
),
);
}
}
HarmonyOS device type #
🌐en:
OS.harmonyDeviceType returns the HarmonyOS device type synchronously, such as
2in1, tablet or phone. It is read from the NDK OH_GetDeviceType on first
access and cached afterwards, so no initialization is required. On non-HarmonyOS
systems and in web it returns 'N/A'.
🌐中文:
OS.harmonyDeviceType 同步返回鸿蒙设备类型,如 2in1、tablet、phone。
首次访问时通过 NDK 的 OH_GetDeviceType 读取并缓存,无需初始化;
非鸿蒙系统或 web 环境返回 'N/A'。