Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created December 16, 2025 18:34
Show Gist options
  • Select an option

  • Save fredgrott/9c4130c3b021c4c0ffd3fb4c120ce14c to your computer and use it in GitHub Desktop.

Select an option

Save fredgrott/9c4130c3b021c4c0ffd3fb4c120ce14c to your computer and use it in GitHub Desktop.
MQExt
/// Changed this from GSkinner's SizedContext package as with fold-ables
/// on both Android and iOS want an always valid size. That is accomplushed
/// by grabbing a FlutterView.display Display object.
///
/// @author Fredrick Allan Grott
extension MQExt on BuildContext {
/// Returns pixelsPerInch
double get pixelsPerInch => UniversalPlatform.isAndroid || UniversalPlatform.isIOS? 150 : 96;
/// Returns brightness from MediaQuery as a bool
bool get isLight => MediaQuery.of(this).platformBrightness == Brightness.light;
/// Returns if Orientation is landscape
bool get isLandscape => MediaQuery.orientationOf(this) == Orientation.landscape;
/// Returns a display object of FlutterView
Display get displayObject => View.of(this).display;
/// Returns devicePixelRation from the display object
double get devicePixelRatio => displayObject.devicePixelRatio;
/// Returns same as MediaQuery.of(context).size
/// but will only rebuild if stuff changes.
/// Returns that actual device size rather than the logical size
///
/// It also eliminates having to MediaQuery DeviceFeatures hinge
/// as them canonical layouts can be on size alone.
Size get sizePx => displayObject.size;
/// Returns same as MediaQuery.of(context).size.width
double get widthPx => sizePx.width;
/// Returns same as MediaQuery.of(context).height
double get heightPx => sizePx.height;
/// Returns diagonal screen pixels
double get diagonalPx {
final Size s = sizePx;
return sqrt((s.width * s.width) + (s.height * s.height));
}
/// Returns pixel size in Inches
Size get sizeInches {
final Size pxSize = sizePx;
return Size(pxSize.width / pixelsPerInch, pxSize.height / pixelsPerInch);
}
/// Returns screen width in Inches
double get widthInches => sizeInches.width;
/// Returns screen height in Inches
double get heightInches => sizeInches.height;
/// Returns screen diagonal in Inches
double get diagonalInches => diagonalPx / pixelsPerInch;
/// Returns fraction (0-1) of screen width in pixels
double widthPct(double fraction) => fraction * widthPx;
/// Returns fraction (0-1) of screen height in pixels
double heightPct(double fraction) => fraction * heightPx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment