33 * email: zhuoyuan93@gmail.com
44 */
55
6- import 'dart:ui' as ui;
7-
86import 'dart:math' ;
7+ import 'dart:ui' as ui;
98
109import 'package:flutter/material.dart' ;
1110
@@ -21,6 +20,9 @@ class ScreenUtil {
2120 /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
2221 late bool allowFontScaling;
2322
23+ ///屏幕方向
24+ static late Orientation _orientation;
25+
2426 static late double _pixelRatio;
2527 static late double _textScaleFactor;
2628 static late double _screenWidth;
@@ -35,22 +37,23 @@ class ScreenUtil {
3537 }
3638
3739 static void init (
38- BoxConstraints constraints,
39- Orientation orientation,
40- {
40+ BoxConstraints constraints,
41+ Orientation orientation, {
4142 Size designSize = defaultSize,
4243 bool allowFontScaling = false ,
4344 }) {
4445 _instance = ScreenUtil ._();
4546 _instance
4647 ..uiSize = designSize
4748 ..allowFontScaling = allowFontScaling;
48- if (orientation == Orientation .portrait){
49+
50+ _orientation = orientation;
51+ if (orientation == Orientation .portrait) {
4952 _screenWidth = constraints.maxWidth;
50- _screenHeight = constraints.maxHeight;
51- }else {
53+ _screenHeight = constraints.maxHeight;
54+ } else {
5255 _screenWidth = constraints.maxHeight;
53- _screenHeight = constraints.maxWidth;
56+ _screenHeight = constraints.maxWidth;
5457 }
5558
5659 var window = WidgetsBinding .instance? .window ?? ui.window;
@@ -60,6 +63,10 @@ class ScreenUtil {
6063 _textScaleFactor = window.textScaleFactor;
6164 }
6265
66+ ///获取屏幕方向
67+ ///Get screen orientation
68+ Orientation get orientation => _orientation;
69+
6370 /// 每个逻辑像素的字体像素数,字体的缩放比例
6471 /// The number of font pixels for each logical pixel.
6572 double get textScaleFactor => _textScaleFactor;
@@ -119,12 +126,7 @@ class ScreenUtil {
119126 ///Font size adaptation method
120127 ///- [fontSize] The size of the font on the UI design, in dp.
121128 ///- [allowFontScaling]
122- double setSp (num fontSize, {bool ? allowFontScalingSelf}) =>
123- allowFontScalingSelf == null
124- ? (allowFontScaling
125- ? (fontSize * scaleText) * _textScaleFactor
126- : (fontSize * scaleText))
127- : (allowFontScalingSelf
128- ? (fontSize * scaleText) * _textScaleFactor
129- : (fontSize * scaleText));
129+ double setSp (num fontSize, {bool ? allowFontScalingSelf}) => allowFontScalingSelf == null
130+ ? (allowFontScaling ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText))
131+ : (allowFontScalingSelf ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText));
130132}
0 commit comments