Skip to content

Instantly share code, notes, and snippets.

@oohyugi
Last active July 6, 2020 03:19
Show Gist options
  • Save oohyugi/86cad86b17fa1a91f693c1c9c691fb4a to your computer and use it in GitHub Desktop.
Save oohyugi/86cad86b17fa1a91f693c1c9c691fb4a to your computer and use it in GitHub Desktop.
textsize by screen flutter
import 'dart:ui';
class TextSizes {
static TextSizes _instance;
static const int defaultWidth = 720;
static const int defaultHeight = 1280;
num uiWidthPx;
num uiHeightPx;
static double _screenWidth;
static double _screenHeight;
static double _pixelRatio;
TextSizes._();
factory TextSizes() {
return _instance;
}
static void init({num width = defaultWidth, num height = defaultHeight}) {
if (_instance == null) {
_instance = TextSizes._();
}
_instance.uiWidthPx = width;
_instance.uiHeightPx = height;
_pixelRatio = window.devicePixelRatio;
_screenWidth = window.physicalSize.width;
_screenHeight = window.physicalSize.height;
print(_pixelRatio);
print(_screenWidth);
}
static double get pixelRatio => _pixelRatio;
static double get screenWidth => _screenWidth / _pixelRatio;
static double get screenHeight => _screenHeight / _pixelRatio;
double get scaleWidth {
print("${screenWidth / uiWidthPx}");
return screenWidth / uiWidthPx;
}
double get scaleHeight => screenHeight / uiHeightPx;
double get scaleText => scaleWidth;
num setHeight(num height) => height * scaleHeight;
num setWidth(num width) => width * scaleWidth;
num setSp(num fontSize) {
print("${fontSize * scaleText}");
return fontSize * scaleText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment