TechBuzzPro

Check if Device Is Smartphone or Tablet in Flutter

· 6 min read · Phones & Tablets

In this article
  1. Background: Why device type matters in Flutter
  2. How Flutter distinguishes screen size and form factor
  3. Simple approach with MediaQuery
  4. Using the device_info_plus package
  5. Choosing the right method for your app
  6. Common pitfalls and how to avoid them
  7. Comparison of native vs. package solutions
  8. Frequently Asked Questions

Yes, you can reliably tell whether the current device is a smartphone or a tablet in Flutter by examining screen dimensions, pixel density, or using platform‑specific APIs such as the device_info_plus package. The most common technique is to combine MediaQuery values with a size threshold, while a package gives you the actual device model for finer control.

Background: Why device type matters in Flutter

Flutter’s single‑code‑base philosophy means the same widget tree runs on phones, tablets, desktops, and even embedded devices. However, UI expectations differ: a tablet often has more screen real‑estate, allowing multi‑pane layouts, larger touch targets, and optional desktop‑style navigation. Detecting the form factor lets developers adapt layouts, choose appropriate font sizes, and decide whether to show a navigation drawer or a persistent side menu. Ignoring the distinction can lead to cramped interfaces on tablets or wasted space on phones, harming user experience and potentially increasing churn.

How Flutter distinguishes screen size and form factor

Flutter does not expose a built‑in “isTablet” flag because the definition varies across manufacturers and operating systems. Instead, developers rely on three measurable properties:

  • Logical width – the width in device‑independent pixels (dp). A common heuristic treats devices with a logical width ≥ 600 dp as tablets.
  • Physical size – the diagonal size in inches, which can be derived from MediaQuery.size and MediaQuery.devicePixelRatio.
  • Device model – obtained from platform APIs (Android’s android.os.Build or iOS’s UIDevice) via packages such as device_info_plus.

Because Android manufacturers sometimes label large phones as “phablets,” the logical‑width threshold is a pragmatic compromise rather than an absolute rule.

Simple approach with MediaQuery

The quickest way to differentiate a phone from a tablet is to read the logical width from MediaQuery and compare it against a constant. This works for most Android and iOS devices and requires no additional dependencies.

  1. Import Flutter material package:
    import 'package:flutter/material.dart';
  2. Wrap your root widget (or any widget that needs the information) in a Builder or use the BuildContext you already have.
  3. Read the width and decide:
    bool isTablet(BuildContext context) {
      final width = MediaQuery.of(context).size.shortestSide;
      return width >= 600; // 600dp is the common breakpoint
    }
  4. Use the result to choose a layout:
    Widget build(BuildContext context) {
      return isTablet(context) ? TabletHomePage() : PhoneHomePage();
    }

This method automatically adapts when the user rotates the device because shortestSide always reflects the smaller dimension, regardless of orientation.

Using the device_info_plus package

When you need more precision—such as distinguishing a 7‑inch Android tablet from a 6.5‑inch phone with a large display—accessing the native device model is useful. The device_info_plus package provides a cross‑platform API that returns detailed information without writing platform‑specific code.

  1. Add the dependency to pubspec.yaml:
    dependencies:
      device_info_plus: ^10.0.0
  2. Import the package:
    import 'package:device_info_plus/device_info_plus.dart';
  3. Create an async helper that returns a boolean:
    Future isTabletDevice() async {
      final deviceInfo = DeviceInfoPlugin();
      if (Theme.of(context).platform == TargetPlatform.android) {
        final androidInfo = await deviceInfo.androidInfo;
        // Example: many tablets have "tablet" in the model name or a large screen size.
        return androidInfo.systemFeatures.contains('android.hardware.screen.landscape') &&
               androidInfo.displayMetrics?.widthPx != null &&
               (androidInfo.displayMetrics!.widthPx / androidInfo.displayMetrics!.xdpi) >= 7.0;
      } else if (Theme.of(context).platform == TargetPlatform.iOS) {
        final iosInfo = await deviceInfo.iosInfo;
        // iPad models contain "iPad" in the name.
        return iosInfo.name?.toLowerCase().contains('ipad') ?? false;
      }
      return false; // Fallback for other platforms.
    }
  4. Call the function inside a FutureBuilder or during app initialization to set a flag.

Because this approach queries the operating system, it works even if the device reports an unconventional logical width (e.g., a foldable phone that reports a large width when unfolded).

Choosing the right method for your app

Consider the following factors when deciding which detection strategy to adopt:

  • Complexity vs. accuracy – MediaQuery is one line of code; the package adds async handling but yields model‑level detail.
  • Performance impact – MediaQuery reads are synchronous and negligible. The device‑info call incurs a small one‑time delay, typically under 50 ms, which is acceptable during splash screen or early initialization.
  • Future‑proofing – New form factors (foldables, dual‑screen devices) may break simple width thresholds. A model‑based check can be extended with a whitelist of known tablet identifiers.
  • Platform coverage – If you target web or desktop, MediaQuery works everywhere, while device_info_plus currently supports Android, iOS, macOS, Linux, and Windows but not browsers.

Most apps start with the MediaQuery heuristic and fall back to a package‑based check only for edge cases where layout glitches are observed on specific devices.

Common pitfalls and how to avoid them

Even a correct implementation can produce surprising results if you overlook these details:

  • Using physical pixels instead of logical pixels – Querying MediaQuery.of(context).size.width returns logical pixels. Multiplying by devicePixelRatio to get physical pixels before applying the 600 dp rule will give wrong answers on high‑density screens.
  • Hard‑coding orientation – Checking only size.width fails when the device is in portrait mode. Use shortestSide to stay orientation‑agnostic.
  • Neglecting safe‑area insets – On iOS devices with a notch, the usable width may be reduced. However, shortestSide already excludes system UI, so you generally do not need extra adjustments.
  • Running detection before WidgetsBinding is initialized – Calling MediaQuery.of(context) in initState of a widget that is not yet attached will throw. Perform the check inside build or after WidgetsBinding.instance.addPostFrameCallback.
  • Assuming the package works on the web – device_info_plus does not provide device model on browsers. Guard the call with a platform check to avoid runtime errors.

Addressing these issues early prevents layout flicker, crashes, and the need for costly hot‑fixes after release.

Comparison of native vs. package solutions

AspectMediaQuery (native)device_info_plus (package)
Implementation effortVery low – one line of codeModerate – async setup and platform checks
AccuracyGood for most phones/tablets, fails on unusual form factorsHigh – uses actual model identifiers and screen metrics
PerformanceNegligible runtime costOne‑time async call, minimal impact
Platform coverageAll Flutter platforms (including web)Android, iOS, macOS, Linux, Windows (not web)
MaintenanceZero external dependencyDepends on package version, may need updates for new OS releases

For most cross‑platform apps, start with MediaQuery. If you encounter devices that break the 600 dp rule—such as foldables or custom Android skins—add a supplemental check with device_info_plus.

Frequently Asked Questions

Can I detect a tablet on the web version of a Flutter app?

On the web, there is no native device model API, so you must rely on screen size. Using MediaQuery.of(context).size.shortestSide with a 600 dp breakpoint is the recommended approach.

What if a user changes the system font size or display scaling?

Logical dimensions already factor in user scaling, so the 600 dp rule remains valid. However, extremely large scaling can push a phone’s shortest side over the threshold; consider adding a secondary check on devicePixelRatio if you need stricter control.

Do foldable devices count as tablets?

Foldables can report a large width when unfolded, but their shortest side may still be below the tablet threshold. For precise handling, query the native screen size via device_info_plus or listen to the window.onMetricsChanged event and adjust layout dynamically.

Is it safe to store the result of a device check in a global variable?

Yes, because the device type does not change at runtime (except for foldables that physically change size). Store the boolean after the first calculation and reuse it to avoid repeated async calls.

Will using device_info_plus increase my app size significantly?

The package adds a few kilobytes for each platform’s native bridge. In practice, the impact on the final APK or IPA is under 1 MB, which is negligible for most production apps.

How do I handle orientation changes after the initial detection?

Because the MediaQuery check uses shortestSide, the result stays correct after rotation. If you rely on a cached value, recompute it in didChangeMetrics or inside build to stay responsive.