printScreenDiagonalInInches function

double printScreenDiagonalInInches(
  1. BuildContext context
)

Calculates and returns the screen's diagonal size in inches.

This is an approximation based on the device's logical size, pixel ratio, and a base DPI assumption (typically 160 for Android).

Implementation

double printScreenDiagonalInInches(BuildContext context) {
  final logicalWidth = MediaQuery.of(context).size.width;
  final logicalHeight = MediaQuery.of(context).size.height;
  final pixelRatio = MediaQuery.of(context).devicePixelRatio;

  /// Gets the real size of the screen in pixels
  final widthPx = logicalWidth * pixelRatio;
  final heightPx = logicalHeight * pixelRatio;

  /// Estimates the PPI (Pixels per inch) using the base of 160 dpi
  final ppi = 160 * pixelRatio;

  /// Calculates the diagonal
  final diagonalInches = sqrt(widthPx * widthPx + heightPx * heightPx) / ppi;

  debugPrint("la Diagonal aproximada de este dispositivo es: ${diagonalInches.toStringAsFixed(2)} pulgadas");
  return diagonalInches;
}