size_config.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:flutter/material.dart';
  2. class SizeConfig {
  3. SizeConfig._();
  4. static final SizeConfig _instance = SizeConfig._();
  5. factory SizeConfig() => _instance;
  6. late MediaQueryData _mediaQueryData;
  7. late Size size;
  8. late double screenWidth;
  9. late double screenHeight;
  10. late double blockSizeHorizontal;
  11. late double blockSizeVertical;
  12. late double _safeAreaHorizontal;
  13. late double _safeAreaVertical;
  14. late double safeBlockHorizontal;
  15. late double safeBlockVertical;
  16. double? profileDrawerWidth;
  17. late double refHeight;
  18. late double refWidth;
  19. void init(BuildContext context) {
  20. _mediaQueryData = MediaQuery.of(context);
  21. size = _mediaQueryData.size;
  22. screenWidth = _mediaQueryData.size.width;
  23. screenHeight = _mediaQueryData.size.height;
  24. refHeight = 1450;
  25. refWidth = 670;
  26. if (screenHeight < 1200) {
  27. blockSizeHorizontal = screenWidth / 100;
  28. blockSizeVertical = screenHeight / 100;
  29. _safeAreaHorizontal =
  30. _mediaQueryData.padding.left + _mediaQueryData.padding.right;
  31. _safeAreaVertical =
  32. _mediaQueryData.padding.top + _mediaQueryData.padding.bottom;
  33. safeBlockHorizontal = (screenWidth - _safeAreaHorizontal) / 100;
  34. safeBlockVertical = (screenHeight - _safeAreaVertical) / 100;
  35. } else {
  36. blockSizeHorizontal = screenWidth / 120;
  37. blockSizeVertical = screenHeight / 120;
  38. _safeAreaHorizontal =
  39. _mediaQueryData.padding.left + _mediaQueryData.padding.right;
  40. _safeAreaVertical =
  41. _mediaQueryData.padding.top + _mediaQueryData.padding.bottom;
  42. safeBlockHorizontal = (screenWidth - _safeAreaHorizontal) / 120;
  43. safeBlockVertical = (screenHeight - _safeAreaVertical) / 120;
  44. }
  45. }
  46. Size getSize() {
  47. return size;
  48. }
  49. double getWidthRatio(double val) {
  50. double res = (val / refWidth) * 100;
  51. double temp = res * blockSizeHorizontal;
  52. return temp;
  53. }
  54. double getHeightRatio(double val) {
  55. double res = (val / refHeight) * 100;
  56. double temp = res * blockSizeVertical;
  57. return temp;
  58. }
  59. double getFontRatio(double val) {
  60. double res = (val / refWidth) * 100;
  61. double temp = 0.0;
  62. if (screenWidth < screenHeight) {
  63. temp = res * safeBlockHorizontal;
  64. } else {
  65. temp = res * safeBlockVertical;
  66. }
  67. return temp;
  68. }
  69. }
  70. extension SizeUtils on num {
  71. double get toWidth => SizeConfig().getWidthRatio(toDouble());
  72. double get toHeight => SizeConfig().getHeightRatio(toDouble());
  73. double get toFont => SizeConfig().getFontRatio(toDouble());
  74. }