platform_detect は、実行時に現在のオペレーティングシステムおよびブラウザ環境を識別するための軽量な Flutter Web ライブラリです。OS(例:Windows、macOS、Linux)やブラウザ(例:Chrome、Safari、Firefox)などのプラットフォーム詳細をシンプルかつ信頼性の高い方法で検出でき、ユーザーの環境に応じて動作やUIをカスタマイズできます。このパッケージはWebアプリケーション向けに最適化されており、FlutterのWebプラットフォームとシームレスに統合されています。外部依存なしにクライアントサイドJavaScriptによる検出に依存しています。
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: