12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import 'package:flutter/material.dart';
- class SizeConfig {
- SizeConfig._();
- static SizeConfig _instance = SizeConfig._();
- factory SizeConfig() => _instance;
- late MediaQueryData _mediaQueryData;
- late double screenWidth;
- late double screenHeight;
- late double blockSizeHorizontal;
- late double blockSizeVertical;
- late double _safeAreaHorizontal;
- late double _safeAreaVertical;
- late double safeBlockHorizontal;
- late double safeBlockVertical;
- double? profileDrawerWidth;
- late double refHeight;
- late double refWidth;
- void init(BuildContext context) {
- _mediaQueryData = MediaQuery.of(context);
- screenWidth = _mediaQueryData.size.width;
- screenHeight = _mediaQueryData.size.height;
- refHeight = 1450;
- refWidth = 670;
- if (screenHeight < 1200) {
- blockSizeHorizontal = screenWidth / 100;
- blockSizeVertical = screenHeight / 100;
- _safeAreaHorizontal =
- _mediaQueryData.padding.left + _mediaQueryData.padding.right;
- _safeAreaVertical =
- _mediaQueryData.padding.top + _mediaQueryData.padding.bottom;
- safeBlockHorizontal = (screenWidth - _safeAreaHorizontal) / 100;
- safeBlockVertical = (screenHeight - _safeAreaVertical) / 100;
- } else {
- blockSizeHorizontal = screenWidth / 120;
- blockSizeVertical = screenHeight / 120;
- _safeAreaHorizontal =
- _mediaQueryData.padding.left + _mediaQueryData.padding.right;
- _safeAreaVertical =
- _mediaQueryData.padding.top + _mediaQueryData.padding.bottom;
- safeBlockHorizontal = (screenWidth - _safeAreaHorizontal) / 120;
- safeBlockVertical = (screenHeight - _safeAreaVertical) / 120;
- }
- }
- double getWidthRatio(double val) {
- double res = (val / refWidth) * 100;
- double temp = res * blockSizeHorizontal;
- return temp;
- }
- double getHeightRatio(double val) {
- double res = (val / refHeight) * 100;
- double temp = res * blockSizeVertical;
- return temp;
- }
- double getFontRatio(double val) {
- double res = (val / refWidth) * 100;
- double temp = 0.0;
- if (screenWidth < screenHeight) {
- temp = res * safeBlockHorizontal;
- } else {
- temp = res * safeBlockVertical;
- }
- return temp;
- }
- }
- extension SizeUtils on num {
- double get toWidth => SizeConfig().getWidthRatio(this.toDouble());
- double get toHeight => SizeConfig().getHeightRatio(this.toDouble());
- double get toFont => SizeConfig().getFontRatio(this.toDouble());
- }
|