import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; // class MainConstants { // static const double minWebAppRatio = 9.0 / 21.0; // static const double maxWebAppRatio = 9.0 / 18.0; // } /// Utils for dynamic sizes class SizeUtils { static double height = 0.0; static double width = 0.0; static double physicalHeight = 0.0; static double physicalWidth = 0.0; // Real value of aspect ratio height obtained through the display itself and not from calculating using the height and width static double aspectRatioWidth = 9; static double devicePixelRatio = 0.0; static double physicalAspectRatioHeight = 0; static double visibleAspectRatioHeight = 0; // Padding of the screen where we cannot render elements without the SafeArea static EdgeInsets padding = const EdgeInsets.all(0); // static const double kWebDesiredAspectRatio = MainConstants.minWebAppRatio; const SizeUtils(); /// init method which instantiates the class using MediaQuery. If a size is provided, it will use that size instead of the MediaQuery static void init({required BuildContext context, Size? size}) { if (size != null) { padding = EdgeInsets.zero; devicePixelRatio = MediaQuery.of(context).devicePixelRatio; height = size.height; width = size.width; visibleAspectRatioHeight = (height / width) * aspectRatioWidth; physicalWidth = width; physicalHeight = height; physicalAspectRatioHeight = (physicalHeight / physicalWidth) * aspectRatioWidth; } else { height = MediaQuery.sizeOf(context).height; width = MediaQuery.sizeOf(context).width; padding = MediaQuery.of(context).padding; devicePixelRatio = MediaQuery.of(context).devicePixelRatio; visibleAspectRatioHeight = (height / width) * aspectRatioWidth; final FlutterView view = WidgetsBinding.instance.platformDispatcher.views.first; final Size realSize = view.display.size; // Dimensions in physical pixels (px) physicalWidth = realSize.width / devicePixelRatio; physicalHeight = realSize.height / devicePixelRatio; physicalAspectRatioHeight = (physicalHeight / physicalWidth) * aspectRatioWidth; } } /// Calculate percentatge of screen height, for example: '5' would be a '5%' of the total screen size static double heightByPercent(double percent) => height * (percent / 100); static double physicalHeightByPercent(double percent) => physicalHeight * (percent / 100); static double widthByPercent(double percent) => width * (percent / 100); static bool get isSmallerScreen => visibleAspectRatioHeight <= 16; static bool get isMediumScreen => physicalAspectRatioHeight <= 18.0; static bool get isBigScreen => physicalAspectRatioHeight <= 20.0; static bool get isXLScreen => physicalAspectRatioHeight >= 21.5; static Size get screenSize => Size(width, height); static T getByScreen({ required T small, T? medium, required T big, T? xl, }) { if (isSmallerScreen) return small; if (isMediumScreen) return medium ?? small; if (isBigScreen) return big; if (isXLScreen) return xl ?? big; return big; } static double getDefaultTopBarHeight() { return getByScreen(small: 40.0, big: 55.0); } }