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

Flutter plugin to retrieve device display metrics (resolution, size, PPI, diagonal). Convert inches & mm to 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 & millimeters into Flutter logical pixels.

Features #

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

Install #

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

dependencies:
  display_metrics: ^0.4.1
copied to clipboard

In your library add the following import:

import 'package:display_metrics/display_metrics.dart';
copied to clipboard

Getting started #

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

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

Usage #

DisplayMetricsData #

To access DisplayMetricsData you can call

final metrics = DisplayMetrics.maybeOf(context);
// or
final metrics = DisplayMetrics.of(context);
copied to clipboard
// 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 
copied to clipboard

Convert units #

To convert inches and millimeters 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 millimeters to Flutter logical pixels
context.mmToPixels(double mm);

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

// Converts Flutter logical pixels into millimeters
context.pixelsToMm(int pixels);
copied to clipboard

DisplayMetricsWidget #

// Add [DisplayMetricsWidget] to Widget tree above MaterialApp to use DisplayMetrics.of(context) and BuildContext extension methods
DisplayMetricsWidget(
  // Set [updateSizeOnRotate] to true if you need to update size when orientation of your device changes
  updateSizeOnRotate: true,
  child: child,
);
copied to clipboard

Supported Platforms #

Platform Support
iOS
Android
Windows
Web 🛠
(to be added)
macOS 🛠
(to be added)
Linux 🛠
(to be added)

Credits #

iOS implementation uses Jens Schwarzer's UIScreenExtension (https://github.com/marchv/UIScreenExtension)

14
likes
0
points
1.04k
downloads

Publisher

verified publishernukeolay.dev

Weekly Downloads

2024.09.20 - 2025.04.04

Flutter plugin to retrieve device display metrics (resolution, size, PPI, diagonal). Convert inches & mm to Flutter logical pixels.

Repository (GitHub)
View/report issues

Topics

#screen #ppi #size #resolution #pixels

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on display_metrics