platform_detail 是一個輕量級的 Dart 套件,可簡化取得完整平台特定資訊的過程。它提供了一個統一且容易使用的介面,用於識別目前的操作系統、裝置類型、架構以及其他執行時細節,支援 Android、iOS、macOS、Windows 和 Linux 等多個平台。專為需要可靠平台檢測而無需重複程式碼的開發者設計,該套件將底層平台檢查抽象為簡潔、易讀的方法。此套件非常適合基於環境或裝置特性的條件邏輯。
適用情境
基於平台的條件 UI 繪製
設備特定功能開關
跨平台調試工具
執行時期環境探測
優化每個裝置的效能
測試平台特定行為
主要特色
支援 Android、iOS、macOS、Windows 與 Linux
用於平台和設備檢測的簡單 API
回傳詳細的系統資訊
輕量級,無外部依賴
跨平台的一致性介面
透過 pub add 輕鬆整合
適合對象
跨平台 Flutter 應用
需要平台感知邏輯的應用程式
尋求最小化設定的開發者
除錯與診斷工具
針對多個桌面與行動平台的應用
使用建議
不支援網頁平台
僅限執行時平台資訊
無即時平台變更檢測
依賴原生平台 API
某些平台可能需要權限處理
不適用於複雜的裝置指紋識別
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
Platform Detail
About
Platform Detail is a lightweight and developer-friendly Flutter package that makes platform detection easier, more complete, and cross-platform.
It provides a unified and consistent API for checking the platform your app is running on — including web, something that native Flutter classes like TargetPlatform and Platform from dart:io don't currently support.
⚠️ Flutter’s native Platform and TargetPlatform do not provide support for detecting web, which can be a limitation in universal apps.
✅ platform_detail addresses this limitation by introducing a simple and intuitive API that includes web detection out of the box.
✅ Why Use This Package?
✅ Unified API across all platforms (including web).
✅ Clear separation between platform type and platform group.
✅ Static API with simple access from PlatformDetail.
✅ No need to manually check kIsWeb or rely on partial solutions.
If you only rely on TargetPlatform, you’ll miss web support.
voidmain() {
// Native TargetPlatform doesn't detect web:if (defaultTargetPlatform == TargetPlatform.android) {
// OK for Android
} elseif (kIsWeb) {
// Web has to be checked manually
}
}
With PlatformDetail, the check is unified:
voidmain() {
if (PlatformDetail.isWeb) {
print('Web detected ✔️');
}
}
🔎 Group-based Detection
Check whether the current platform belongs to a broader category:
voidmain() {
if (PlatformDetail.isMobile) {
print('Mobile platform');
}
if (PlatformDetail.isDesktop) {
print('Desktop platform');
}
if (PlatformDetail.isDesktopOrWeb) {
print('Desktop or Web platform');
}
}
voidmain() {
if (PlatformDetail.isIOS) print('iOS');
if (PlatformDetail.isAndroid) print('Android');
if (PlatformDetail.isFuchsia) print('Fuchsia');
if (PlatformDetail.isWindows) print('Windows');
if (PlatformDetail.isLinux) print('Linux');
if (PlatformDetail.isMacOS) print('macOS');
if (PlatformDetail.isWeb) print('Web');
}
Or create different cases through the enum with a switch:
ℹ️ No configuration is required for most platforms.
For macOS, you must add the following entry to both DebugProfile.entitlements and ReleaseProfile.entitlements files:
If you need more detailed information about the device and operating system it is running on.
voidmain() async {
final descriptionDevice = awaitPlatformDetail.deviceInfo();
}
Or maybe you need an information string about the device info:
voidmain() async {
final descriptionDevice = awaitPlatformDetail.deviceInfoDetails();
}
This will return something like this:
Android: Android 9 (SDK 28), Xiaomi Redmi Note 7
iOS: iOS 13.1, iPhone 11 Pro Max iPhone
Web: Google Chrome (115.0.5790.170)
Linux: Fedora 17 (Beefy Miracle)
Windows: Windows 10 Home (1903)
MacOS: macOS 13.5, MacBook Pro
Get compact environment details
If you need a typed and lightweight payload with platform, model and locale, use environmentDetails():
voidmain() async {
final details = awaitPlatformDetail.environmentDetails();
print(details.platformType); // PlatformType.androidprint(details.platform); // android 14print(details.deviceModel); // Google Pixel 8print(details.locale); // en-US
}
This returns an EnvironmentDetails object with these fields:
platformType: PlatformType
platform: String
deviceModel: String
locale: String
The example app includes a section that renders these values on screen.
Get app version and build number
If you need app package metadata, you can use versionDetails():
voidmain() async {
final details = awaitPlatformDetail.versionDetails();
print(details.appName); // My Appprint(details.packageName); // com.example.my_appprint(details.version); // 5.4.0print(details.buildNumber); // 12
}
This returns a VersionDetails object with these fields:
appName: String
packageName: String
version: String
buildNumber: String
Light/Dark Mode
You can detect too if the device is configured in light or dark mode:
voidmain() {
if (PlatformDetail.isLightMode) {
print('The current platform is configured with light mode');
}
if (PlatformDetail.isDarkMode) {
print('The current platform is configured with dark mode');
}
}
voidmain() {
if (PlatformDetail.theme == DeviceTheme.light) {
print('The current device is configured in light mode');
}
if (PlatformDetail.theme == DeviceTheme.dark) {
print('The current device is configured in dark mode');
}
}