platform_detect 是一個輕量級的 Flutter Web 庫,旨在執行時識別目前的操作系統和瀏覽器環境。它提供簡單且可靠的平台資訊檢測功能,例如作業系統(如 Windows、macOS、Linux)和瀏覽器(如 Chrome、Safari、Firefox),讓開發者能根據使用者的環境來調整行為或介面。此套件專為 Web 應用優化,並與 Flutter 的 Web 平台無縫整合。它依賴於客戶端 JavaScript 檢測,不需外部相依性。
適用情境
為不同瀏覽器適配UI佈局
根據作業系統條件性啟用功能
調試平台特定問題
按瀏覽器優化效能
處理瀏覽器相容性差異
主要特色
輕量且快速
無外部依賴
支援主要的瀏覽器與作業系統
簡單 API,結果明確
專為 Flutter Web 設計
適合對象
僅限網路的Flutter應用
跨瀏覽器相容性邏輯
環境感知的UI渲染
開發者工具和診斷
使用建議
僅限網頁平台
依賴瀏覽器/使用者代理字串
可能需要針對新瀏覽器版本進行更新
不適用於行動或桌面應用程式
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
platform_detect
A library for detecting browser and platform type and version.
Usage
A simple usage example:
import'package:platform_detect/platform_detect.dart';
main() {
if (browser.isChrome) {
print('thank you for being a friend');
}
if (operatingSystem.isMac) {
print('');
}
}
Decorating the <html> element with CSS classes
This library can also be used to place CSS classes on the <html> element based on the
operating system, browser, or a specific supported feature.
Add the decorator.dart import as shown in the example below, and call decorateRootNodeWithPlatformClasses
within main in your application.
By default, the only browser feature that the library detects for you is touch / mstouch.
You can extend this functionality to bake in a Modernizr-esque implementation by creating your
own Feature instances, and passing a list of them as the features argument to
decorateRootNodeWithPlatformClasses:
import'package:platform_detect/platform_detect.dart';
import'package:platform_detect/decorator.dart';
// For instance, if you need to detect whether a browser supports H5 canvas stuff...var canvasFeature = newFeature('canvas', newCanvasElement().context2D != null);
main() {
// Decorate HTML elementdecorateRootNodeWithPlatformClasses(features: [canvasFeature]);
// Or use the `isSupported` field somewhere else in your app...if (canvasFeature.isSupported) {
// Do something
} else {
// Fall back / warn / etc...
}
}
The <html> node of your application will now look like something like this
for browsers that support canvas: