platform_is_android_tablet 0.0.1 copy "platform_is_android_tablet: ^0.0.1" to clipboard
platform_is_android_tablet: ^0.0.1 copied to clipboard

PlatformAndroid

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

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:platform_is_android_tablet/platform_is_android_tablet.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  bool _isTablet = false;
  int _screenLayout = 0;
  int _screenLayoutSize = 0;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    bool isTablet;
    int screenLayout;
    int screenLayoutSize;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await PlatformIsAndroidTablet().getPlatformVersion() ?? 'Unknown platform version';
      isTablet = await PlatformIsAndroidTablet.isAndroidTablet();
      screenLayout = await PlatformIsAndroidTablet.getScreenLayout();
      screenLayoutSize = await PlatformIsAndroidTablet.getScreenLayoutSize();
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
      isTablet = false;
      screenLayout = 0;
      screenLayoutSize = 0;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
      _isTablet = isTablet;
      _screenLayout = screenLayout;
      _screenLayoutSize = screenLayoutSize;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Running on: $_platformVersion'),
              Text('Is Android Tablet: $_isTablet'),
              Text('Screen Layout: 0x${_screenLayout.toRadixString(16).padLeft(2, '0')}'),
              Text('Screen Layout Size: 0x${_screenLayoutSize.toRadixString(16).padLeft(2, '0')}'),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
160
points
78
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

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

Repository (GitHub)

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on platform_is_android_tablet

Packages that implement platform_is_android_tablet