display_metrics 0.1.0 copy "display_metrics: ^0.1.0" to clipboard
display_metrics: ^0.1.0 copied to clipboard

Flutter plugin that provides display metrics such as real screen resolution, physical display size, ppi, diagonal. Also it contains BuildContext extension to transform inches & cm into Flutter logical pixels.

display_metrics #

Flutter plugin that provides display metrics such as real screen resolution, physical display size, ppi, diagonal. Also it contains BuildContext extension to transform inches & centimeters into Flutter logical pixels.

*Does not work on iOS versions below 13

Features #

  • Get real screen resolution, display size (inches) and PPI
  • Convert inches and centimeters into Flutter logical pixels

Install #

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  display_metrics: ^0.1.0

In your library add the following import:

import 'package:display_metrics/display_metrics.dart';

Getting started #

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(
        useMaterial3: true,
      ),
      // add DisplayMetricsWidget to Widget tree under MaterialApp to use
      // DisplayMetrics.of(context) and BuildContext extension methods
      home: DisplayMetricsWidget(
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Display metrics example app'),
            centerTitle: true,
          ),
          body: const BodyWidget(),
        ),
      ),
    );
  }
}

Usage #

DisplayMetricsData #

To access DisplayMetricsData you can call

final metrics = DisplayMetrics.maybeOf(context);
// or
final metrics = DisplayMetrics.of(context);
// Device's screen physical size in inches
metrics.physicalSize => Size

// Device's screen resolution in real pixels
metrics.resolution => Size

// Device's screen diagonal in inches
metrics.diagonal => double

// Device's screen pixels per inch (PPI)
metrics.ppi => double 

// The number of logical pixels on the device's screen
// that corresponds to one inch
metrics.inchesToLogicalPixelRatio => double 

// MediaQuery`s devicePixelRatio;
metrics.devicePixelRatio => double 

Convert units #

To convert inches and centimeters into Flutter logical pixels and vice versa you can call one of BuildCOntext extension methods:

  /// Converts inches to Flutter logical pixels
  context.inchesToPixels(double inches);

  /// Converts centimeters to Flutter logical pixels
  context.cmToPixels(double cm);

  /// Converts Flutter logical pixels into inches
  context.pixelsToInches(int pixels);

  /// Converts Flutter logical pixels into centimeters
  context.double pixelsToCm(int pixels);
14
likes
0
points
1.04k
downloads

Publisher

verified publishernukeolay.dev

Weekly Downloads

Flutter plugin that provides display metrics such as real screen resolution, physical display size, ppi, diagonal. Also it contains BuildContext extension to transform inches & cm into Flutter logical pixels.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on display_metrics