platform_is_android_tablet

A Flutter plugin for Android that provides a reliable way to detect whether the current device is a tablet, even without any UI context or before the first frame is rendered.

Features

  • No UI Dependency: Works early in the app lifecycle (at app startup) by using ApplicationContext.
  • Accurate Detection: Uses Android's Configuration.SCREENLAYOUT_SIZE_MASK to identify tablets based on physical screen size.
  • Raw Access: Provides methods to get raw screenLayout and screenLayoutSize values for custom logic.
  • Lightweight: Pure native implementation with minimal overhead.

Getting Started

Installation

Add the dependency to your pubspec.yaml:

dependencies:
  platform_is_android_tablet: ^0.0.1

Usage

1. Simple Check

Check if the current device is a tablet:

import 'package:platform_is_android_tablet/platform_is_android_tablet.dart';

bool isTablet = await PlatformIsAndroidTablet.isAndroidTablet();
print("Is Tablet: $isTablet");

2. Raw Configuration Access

If you need custom logic (e.g., checking for XLARGE devices specifically), you can get the raw values:

import 'package:platform_is_android_tablet/platform_is_android_tablet.dart';

// Get (screenLayout & SCREENLAYOUT_SIZE_MASK)
int layoutSize = await PlatformIsAndroidTablet.getScreenLayoutSize();

if (layoutSize == PlatformIsAndroidTablet.SCREENLAYOUT_SIZE_XLARGE) {
  print("This is an extra-large screen device.");
}

Constants Map

The plugin provides constants matching Android's Configuration to help with manual checks:

Constant Value Description
SCREENLAYOUT_SIZE_SMALL 0x01 Small screens
SCREENLAYOUT_SIZE_NORMAL 0x02 Normal screens (phones)
SCREENLAYOUT_SIZE_LARGE 0x03 Large screens (tablets)
SCREENLAYOUT_SIZE_XLARGE 0x04 Extra-large screens
SCREENLAYOUT_SIZE_MASK 0x0F Bitmask for size bits

Technical Implementation

On the Android side, the detection is implemented as:

val screenLayout = context.resources.configuration.screenLayout
val size = screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK
return size >= Configuration.SCREENLAYOUT_SIZE_LARGE

This is the standard Android recommendation for identifying tablet-class devices.


中文说明 (Chinese)

一个用于 Android 端的 Flutter 插件,提供了一种可靠的方法来判断当前设备是否为平板。

特性

  • 不依赖 UI:使用 ApplicationContext,在应用启动阶段(无需 Activity 或 View)即可获取。
  • 准确判断:基于 Android 原生的 Configuration.SCREENLAYOUT_SIZE_MASK 进行检测。
  • 原始数据:支持获取原始的 screenLayout 整型值,满足自定义逻辑。

使用

直接调用静态方法:

bool isTablet = await PlatformIsAndroidTablet.isAndroidTablet();