Search packages, repositories and app categories

Package detailsv2.1.6

platform_detect

A lightweight library for detecting the running browser and OS

Web
30-day downloads
102K
Likes
38
Pub points
125

Install package

flutter pub add platform_detect

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

platform_detect is a lightweight Flutter Web library designed to identify the current operating system and browser environment at runtime. It provides simple, reliable detection of platform details such as OS (e.g., Windows, macOS, Linux) and browser (e.g., Chrome, Safari, Firefox), enabling developers to tailor behavior or UI based on the user's environment. The package is optimized for web applications and integrates seamlessly with Flutter’s web platform. It relies on client-side JavaScript detection without external dependencies.

Use cases

  • Adapting UI layout for different browsers
  • Conditionally enabling features based on OS
  • Debugging platform-specific issues
  • Optimizing performance per browser
  • Handling browser compatibility quirks

Key features

  • Lightweight and fast
  • No external dependencies
  • Supports major browsers and OSes
  • Simple API with clear results
  • Built specifically for Flutter Web

Suitable for

  • Web-only Flutter apps
  • Cross-browser compatibility logic
  • Environment-aware UI rendering
  • Developer tools and diagnostics

Considerations

  • Limited to web platforms only
  • Relies on browser/user agent strings
  • May require updates for new browser versions
  • Not suitable for mobile or desktop apps

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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>

Categories

Dependencies

Dependency list 2 items

Source repository

Projects using this package

No data available yet.