搜尋套件、儲存庫與 APP 分類

套件詳情v2.1.6

platform_detect

一個輕量級庫,用於檢測正在執行的瀏覽器和作業系統

Web
近 30 天下載
10.2萬
喜歡數
38
Pub 分數
125

安裝套件

flutter pub add platform_detect

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

platform_detect 是一個輕量級的 Flutter Web 庫,旨在執行時識別目前的操作系統和瀏覽器環境。它提供簡單且可靠的平台資訊檢測功能,例如作業系統(如 Windows、macOS、Linux)和瀏覽器(如 Chrome、Safari、Firefox),讓開發者能根據使用者的環境來調整行為或介面。此套件專為 Web 應用優化,並與 Flutter 的 Web 平台無縫整合。它依賴於客戶端 JavaScript 檢測,不需外部相依性。

適用情境

  • 為不同瀏覽器適配UI佈局
  • 根據作業系統條件性啟用功能
  • 調試平台特定問題
  • 按瀏覽器優化效能
  • 處理瀏覽器相容性差異

主要特色

  • 輕量且快速
  • 無外部依賴
  • 支援主要的瀏覽器與作業系統
  • 簡單 API,結果明確
  • 專為 Flutter Web 設計

適合對象

  • 僅限網路的Flutter應用
  • 跨瀏覽器相容性邏輯
  • 環境感知的UI渲染
  • 開發者工具和診斷

使用建議

  • 僅限網頁平台
  • 依賴瀏覽器/使用者代理字串
  • 可能需要針對新瀏覽器版本進行更新
  • 不適用於行動或桌面應用程式

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

codecov documentation

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.

import 'package:platform_detect/platform_detect.dart';
import 'package:platform_detect/decorator.dart';

main() {
  decorateRootNodeWithPlatformClasses();
}

The <html> node of your application will now look like something like this:

<html lang="en" class="ua-chrome ua-chrome45 ua-lt-chrome46 ua-lt-chrome47 os-mac no-touch no-mstouch">
  <!-- ... -->
</html>
Feature detection

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 = new Feature('canvas', new CanvasElement().context2D != null);

main() {
  // Decorate HTML element
  decorateRootNodeWithPlatformClasses(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:

<html lang="en" class="canvas ...">
  <!-- ... -->
</html>

and something like this for browsers that don't support canvas:

<html lang="en" class="no-canvas ...">
  <!-- ... -->
</html>

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。