顾海波

【需求】跟随设计稿方向

@@ -18,6 +18,7 @@ class ScreenUtil { @@ -18,6 +18,7 @@ class ScreenUtil {
18 18
19 static bool Function() _enableScaleWH = () => true; 19 static bool Function() _enableScaleWH = () => true;
20 static bool Function() _enableScaleText = () => true; 20 static bool Function() _enableScaleText = () => true;
  21 + static bool Function() _useDesignOrientation = () => false;
21 22
22 23
23 /// UI设计中手机尺寸 , dp 24 /// UI设计中手机尺寸 , dp
@@ -27,6 +28,9 @@ class ScreenUtil { @@ -27,6 +28,9 @@ class ScreenUtil {
27 ///屏幕方向 28 ///屏幕方向
28 late Orientation _orientation; 29 late Orientation _orientation;
29 30
  31 + ///设计稿方向
  32 + late Orientation _designOrientation;
  33 +
30 late bool _minTextAdapt; 34 late bool _minTextAdapt;
31 late MediaQueryData _data; 35 late MediaQueryData _data;
32 late bool _splitScreenMode; 36 late bool _splitScreenMode;
@@ -41,9 +45,10 @@ class ScreenUtil { @@ -41,9 +45,10 @@ class ScreenUtil {
41 /// if the enableWH return false, the width and the height scale ratio will be 1 45 /// if the enableWH return false, the width and the height scale ratio will be 1
42 /// if the enableText return false, the text scale ratio will be 1 46 /// if the enableText return false, the text scale ratio will be 1
43 /// 47 ///
44 - static void enableScale({bool Function()? enableWH, bool Function()? enableText}) { 48 + static void enableScale({bool Function()? enableWH, bool Function()? enableText, bool Function()? useDesignOrientation}) {
45 _enableScaleWH = enableWH ?? () => true; 49 _enableScaleWH = enableWH ?? () => true;
46 _enableScaleText = enableText ?? () => true; 50 _enableScaleText = enableText ?? () => true;
  51 + _useDesignOrientation = useDesignOrientation?? () => false;
47 } 52 }
48 53
49 /// Manually wait for window size to be initialized 54 /// Manually wait for window size to be initialized
@@ -138,11 +143,16 @@ class ScreenUtil { @@ -138,11 +143,16 @@ class ScreenUtil {
138 ? Orientation.landscape 143 ? Orientation.landscape
139 : Orientation.portrait); 144 : Orientation.portrait);
140 145
  146 + final designOrientation = designSize.width > designSize.height
  147 + ? Orientation.landscape
  148 + : Orientation.portrait;
  149 +
141 _instance 150 _instance
142 ..fontSizeResolver = fontSizeResolver ?? _instance.fontSizeResolver 151 ..fontSizeResolver = fontSizeResolver ?? _instance.fontSizeResolver
143 .._minTextAdapt = minTextAdapt ?? _instance._minTextAdapt 152 .._minTextAdapt = minTextAdapt ?? _instance._minTextAdapt
144 .._splitScreenMode = splitScreenMode ?? _instance._splitScreenMode 153 .._splitScreenMode = splitScreenMode ?? _instance._splitScreenMode
145 - .._orientation = orientation; 154 + .._orientation = orientation
  155 + .._designOrientation = designOrientation;
146 156
147 _instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild()); 157 _instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild());
148 } 158 }
@@ -213,12 +223,13 @@ class ScreenUtil { @@ -213,12 +223,13 @@ class ScreenUtil {
213 223
214 /// 实际尺寸与UI设计的比例 224 /// 实际尺寸与UI设计的比例
215 /// The ratio of actual width to UI design 225 /// The ratio of actual width to UI design
216 - double get scaleWidth => !_enableScaleWH() ? 1 : screenWidth / _uiSize.width; 226 + double get scaleWidth => !_enableScaleWH() ? 1 : screenWidth /
  227 + ((_useDesignOrientation() && _designOrientation != _orientation)? _uiSize.height : _uiSize.width);
217 228
218 /// The ratio of actual height to UI design 229 /// The ratio of actual height to UI design
219 double get scaleHeight => 230 double get scaleHeight =>
220 !_enableScaleWH() ? 1 : (_splitScreenMode ? max(screenHeight, 700) : screenHeight) / 231 !_enableScaleWH() ? 1 : (_splitScreenMode ? max(screenHeight, 700) : screenHeight) /
221 - _uiSize.height; 232 + ((_useDesignOrientation() && _designOrientation != _orientation)? _uiSize.width : _uiSize.height);
222 233
223 double get scaleText => 234 double get scaleText =>
224 !_enableScaleText() ? 1 : (_minTextAdapt ? min(scaleWidth, scaleHeight) : scaleWidth); 235 !_enableScaleText() ? 1 : (_minTextAdapt ? min(scaleWidth, scaleHeight) : scaleWidth);
@@ -76,9 +76,10 @@ class ScreenUtilInit extends StatefulWidget { @@ -76,9 +76,10 @@ class ScreenUtilInit extends StatefulWidget {
76 this.ensureScreenSize = false, 76 this.ensureScreenSize = false,
77 this.enableScaleWH, 77 this.enableScaleWH,
78 this.enableScaleText, 78 this.enableScaleText,
  79 + this.useDesignOrientation,
79 this.responsiveWidgets, 80 this.responsiveWidgets,
80 this.excludeWidgets, 81 this.excludeWidgets,
81 - this.fontSizeResolver = FontSizeResolvers.width, 82 + this.fontSizeResolver = FontSizeResolvers.width
82 }) : super(key: key); 83 }) : super(key: key);
83 84
84 final ScreenUtilInitBuilder? builder; 85 final ScreenUtilInitBuilder? builder;
@@ -89,6 +90,7 @@ class ScreenUtilInit extends StatefulWidget { @@ -89,6 +90,7 @@ class ScreenUtilInit extends StatefulWidget {
89 final bool ensureScreenSize; 90 final bool ensureScreenSize;
90 final bool Function()? enableScaleWH; 91 final bool Function()? enableScaleWH;
91 final bool Function()? enableScaleText; 92 final bool Function()? enableScaleText;
  93 + final bool Function()? useDesignOrientation;
92 final RebuildFactor rebuildFactor; 94 final RebuildFactor rebuildFactor;
93 final FontSizeResolver fontSizeResolver; 95 final FontSizeResolver fontSizeResolver;
94 96
@@ -114,7 +116,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> with WidgetsBindingObse @@ -114,7 +116,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> with WidgetsBindingObse
114 _canMarkedToBuild.addAll(widget.responsiveWidgets!); 116 _canMarkedToBuild.addAll(widget.responsiveWidgets!);
115 } 117 }
116 118
117 - ScreenUtil.enableScale(enableWH: widget.enableScaleWH, enableText: widget.enableScaleText); 119 + ScreenUtil.enableScale(enableWH: widget.enableScaleWH, enableText: widget.enableScaleText, useDesignOrientation: widget.useDesignOrientation);
118 120
119 _validateSize().then(_screenSizeCompleter.complete); 121 _validateSize().then(_screenSizeCompleter.complete);
120 122