size_config.dart 2.4 KB

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