plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[device_info_plus] Add physical screen information to AndroidDeviceInfo

Open slightfoot opened this issue 2 years ago • 1 comments

Use case

I would like to calculate the diagonal display size. Flutter/Dart currently does not expose the xDpi and yDpi values separately and so we cannot calculate the size accurately.

Proposal

We add another field to the Android response that contains the physical size and density information from Android and add a getter to the AndroidDeviceInfo class to get the displaySize in inches.

Implementation Example

Android:

  val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "screen_size")
    channel.setMethodCallHandler { call, result ->
      if (call.method == "getRealMetrics") {
        val display = (context as Activity).windowManager.defaultDisplay
        val metrics = DisplayMetrics()
        display.getRealMetrics(metrics)
        result.success(
          mapOf(
            "width" to metrics.widthPixels.toDouble(),
            "height" to metrics.heightPixels.toDouble(),
            "xdpi" to metrics.xdpi,
            "ydpi" to metrics.ydpi
          )
        )
      } else {
        result.notImplemented()
      }
    }

Dart:

  Future<Map<String, double>> getRealMetrics() async {
    return (await _channel.invokeMethod('getRealMetrics') as Map).cast<String, double>();
  }

  Future<double> getScreenSize() async {
    final data = await getRealMetrics();
    final width = data['width']! / data['xdpi']!;
    final height = data['height']! / data['ydpi']!;
    return (math.sqrt((width * width) + (height * height)) * 100).roundToDouble() / 100;
  }

slightfoot avatar Apr 09 '22 11:04 slightfoot

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Jun 09 '22 00:06 github-actions[bot]