flutter_device_kit

pub package License: MIT

A lightweight, cross-platform Flutter utility package to seamlessly detect operating system platforms, device form-factors (Mobile, Tablet, TV, Desktop, Web), screen dimension thresholds, and retrieve platform-specific hardware details.

Features

  • 📱 Form-Factor Detection: Detect Mobile phone, Tablet, TV, Desktop, and Desktop-or-Web form factors.
  • 💻 Operating System Detection: Check for Android, iOS, Web, macOS, Windows, Linux, and Fuchsia.
  • 📺 TV Mode Simulation: Toggle debugTvMode for testing Android TV / Leanback environments.
  • 📐 Screen Dimensions: Built-in width constants (mobileWidth, tabletWidth) for responsive UI design.
  • ⚙️ Detailed Device Metadata: Integrated platform device details powered by device_info_plus (androidInfo, iosInfo, webInfo, macOsInfo, windowsInfo, linuxInfo, deviceInfo).
  • 🌐 Cross-Platform: Full support across iOS, Android, Web, macOS, Windows, Linux, and Fuchsia.

Getting Started

Add flutter_device_kit to your pubspec.yaml:

flutter pub add flutter_device_kit

Then import the package:

import 'package:flutter_device_kit/flutter_device_kit.dart';

Usage & API Documentation

1. Initialization

Call Device.ensureInitialized() in your main() method before accessing device metadata. Use the debugTvMode parameter to simulate a TV environment during development/testing:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize device info kit
  // Pass debugTvMode: true to simulate TV environment for testing
  await Device.ensureInitialized(
    // debugTvMode: true,
  );

  runApp(const MyApp());
}

2. Platform Type Enum (Device.value)

Get the platform as a PlatformType enum (web, macOS, windows, linux, android, iOS, fuchsia):

switch (Device.value) {
  case PlatformType.android:
    print('Android platform');
    break;
  case PlatformType.iOS:
    print('iOS platform');
    break;
  case PlatformType.web:
    print('Web platform');
    break;
  case PlatformType.macOS:
    print('macOS platform');
    break;
  case PlatformType.windows:
    print('Windows platform');
    break;
  case PlatformType.linux:
    print('Linux platform');
    break;
  case PlatformType.fuchsia:
    print('Fuchsia platform');
    break;
}

3. Operating System Boolean Flags

Direct boolean getters to check the current underlying operating system:

if (Device.isAndroid) {
  print('Running on Android');
}
if (Device.isIOS) {
  print('Running on iOS');
}
if (Device.isWeb) {
  print('Running in a Web browser');
}
if (Device.isMacOS) {
  print('Running on macOS');
}
if (Device.isWindows) {
  print('Running on Windows');
}
if (Device.isLinux) {
  print('Running on Linux');
}
if (Device.isFuchsia) {
  print('Running on Fuchsia');
}

4. Device Form-Factor Classification

Detect the layout and device category for responsive UI adaptations:

if (Device.isMobile) {
  print('Mobile phone layout');
} else if (Device.isTablet) {
  print('Tablet layout');
} else if (Device.isTv) {
  print('Smart TV layout (Android Leanback or debugTvMode)');
} else if (Device.isDesktop) {
  print('Desktop environment (macOS, Windows, or Linux)');
}

if (Device.isDesktopOrWeb) {
  print('Desktop or Web browser');
}

5. Screen Breakpoint Constants

Access screen width constants for responsive threshold checks:

print('Mobile max width: $mobileWidth px'); // 750.0
print('Tablet max width: $tabletWidth px'); // 962.0

6. Hardware & Platform Details (device_info_plus)

Access cached platform-specific hardware details:

// Generic Device Info (returns info for the current platform)
final BaseDeviceInfo? genericInfo = Device.deviceInfo;

// Android Info
if (Device.isAndroid) {
  final AndroidDeviceInfo? android = Device.androidInfo;
  print('Android Release: ${android?.version.release}');
  print('Manufacturer: ${android?.manufacturer}');
  print('Model: ${android?.model}');
}

// iOS Info
if (Device.isIOS) {
  final IosDeviceInfo? ios = Device.iosInfo;
  print('Device Name: ${ios?.name}');
  print('System Version: ${ios?.systemVersion}');
  print('Model: ${ios?.model}');
}

// Web Browser Info
if (Device.isWeb) {
  final WebBrowserInfo? web = Device.webInfo;
  print('Browser: ${web?.browserName}');
  print('User Agent: ${web?.userAgent}');
}

// macOS Info
if (Device.isMacOS) {
  final MacOsDeviceInfo? mac = Device.macOsInfo;
  print('Computer Name: ${mac?.computerName}');
  print('OS Release: ${mac?.osRelease}');
}

// Windows Info
if (Device.isWindows) {
  final WindowsDeviceInfo? win = Device.windowsInfo;
  print('Computer Name: ${win?.computerName}');
  print('Build Number: ${win?.buildNumber}');
}

// Linux Info
if (Device.isLinux) {
  final LinuxDeviceInfo? linux = Device.linuxInfo;
  print('Linux Name: ${linux?.name}');
  print('Version ID: ${linux?.versionId}');
}

API Summary Table

API Property / Method Return Type Description
Device.ensureInitialized({bool debugTvMode}) Future<void> Initializes and caches device info plugin data.
Device.value PlatformType Current platform enum (web, macOS, windows, linux, android, iOS, fuchsia).
Device.isAndroid bool true if running on Android OS.
Device.isIOS bool true if running on iOS.
Device.isWeb bool true if running in a web browser environment.
Device.isMacOS bool true if running on macOS.
Device.isWindows bool true if running on Windows.
Device.isLinux bool true if running on Linux.
Device.isFuchsia bool true if running on Fuchsia OS.
Device.isMobile bool true if running on a Mobile phone form-factor.
Device.isTablet bool true if running on a Tablet screen form-factor.
Device.isTv bool true if running on a TV (Android Leanback feature or debugTvMode).
Device.isDesktop bool true if running on Desktop (macOS, Windows, or Linux).
Device.isDesktopOrWeb bool true if running on Desktop or Web.
Device.deviceInfo BaseDeviceInfo? Device info for the current platform.
Device.androidInfo AndroidDeviceInfo? Cached Android hardware/system metadata.
Device.iosInfo IosDeviceInfo? Cached iOS hardware/system metadata.
Device.webInfo WebBrowserInfo? Cached Web browser metadata.
Device.macOsInfo MacOsDeviceInfo? Cached macOS hardware/system metadata.
Device.windowsInfo WindowsDeviceInfo? Cached Windows hardware/system metadata.
Device.linuxInfo LinuxDeviceInfo? Cached Linux hardware/system metadata.
mobileWidth double Mobile width breakpoint constant (750.0).
tabletWidth double Tablet width breakpoint constant (962.0).
PlatformType enum Enum representing platform operating systems.

Acknowledgements & Credits

Special thanks to:

For more details on specific data returned by platform getters (androidInfo, iosInfo, webInfo, etc.), please refer to the official device_info_plus documentation.

Note: device_info_plus is exported directly from flutter_device_kit. You can use all device_info_plus classes and types (such as AndroidDeviceInfo, IosDeviceInfo, BaseDeviceInfo) directly in your project without adding it separately as a dependency!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Libraries

flutter_device_kit
A comprehensive Flutter package for device type, platform detection, and device information.