Showing
2 changed files
with
28 additions
and
17 deletions
| @@ -23,6 +23,10 @@ class HomePageScaffold extends StatelessWidget { | @@ -23,6 +23,10 @@ class HomePageScaffold extends StatelessWidget { | ||
| 23 | Widget build(BuildContext context) { | 23 | Widget build(BuildContext context) { |
| 24 | printScreenInformation(); | 24 | printScreenInformation(); |
| 25 | 25 | ||
| 26 | + /// Uncomment if you wanna force current widget to be rebuilt with updated values | ||
| 27 | + /// Use it only if you use the second method, or if you use ScreenUtilInit's child. | ||
| 28 | + /// Note: don't use it along with ScreenUtil.init() | ||
| 29 | + // ScreenUtil.registerToBuild(context); | ||
| 26 | return Scaffold( | 30 | return Scaffold( |
| 27 | appBar: AppBar( | 31 | appBar: AppBar( |
| 28 | title: Text(title), | 32 | title: Text(title), |
| @@ -10,11 +10,11 @@ import 'package:flutter/widgets.dart'; | @@ -10,11 +10,11 @@ import 'package:flutter/widgets.dart'; | ||
| 10 | 10 | ||
| 11 | class ScreenUtil { | 11 | class ScreenUtil { |
| 12 | static const Size defaultSize = Size(360, 690); | 12 | static const Size defaultSize = Size(360, 690); |
| 13 | - static late ScreenUtil _instance; | 13 | + static ScreenUtil _instance = ScreenUtil._(); |
| 14 | 14 | ||
| 15 | /// UI设计中手机尺寸 , dp | 15 | /// UI设计中手机尺寸 , dp |
| 16 | /// Size of the phone in UI Design , dp | 16 | /// Size of the phone in UI Design , dp |
| 17 | - late Size uiSize; | 17 | + late Size _uiSize; |
| 18 | 18 | ||
| 19 | ///屏幕方向 | 19 | ///屏幕方向 |
| 20 | late Orientation _orientation; | 20 | late Orientation _orientation; |
| @@ -22,7 +22,7 @@ class ScreenUtil { | @@ -22,7 +22,7 @@ class ScreenUtil { | ||
| 22 | late double _screenWidth; | 22 | late double _screenWidth; |
| 23 | late double _screenHeight; | 23 | late double _screenHeight; |
| 24 | late bool _minTextAdapt; | 24 | late bool _minTextAdapt; |
| 25 | - BuildContext? context; | 25 | + BuildContext? _context; |
| 26 | late bool _splitScreenMode; | 26 | late bool _splitScreenMode; |
| 27 | 27 | ||
| 28 | ScreenUtil._(); | 28 | ScreenUtil._(); |
| @@ -69,16 +69,21 @@ class ScreenUtil { | @@ -69,16 +69,21 @@ class ScreenUtil { | ||
| 69 | } | 69 | } |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | + Set<Element>? _elementsToRebuild; | ||
| 73 | + | ||
| 72 | /// ### Experimental | 74 | /// ### Experimental |
| 73 | - /// Register current page and all its descendants to rebuild | 75 | + /// Register current page and all its descendants to rebuild. |
| 74 | /// Helpful when building for web and desktop | 76 | /// Helpful when building for web and desktop |
| 75 | static void registerToBuild( | 77 | static void registerToBuild( |
| 76 | BuildContext context, [ | 78 | BuildContext context, [ |
| 77 | bool withDescendants = false, | 79 | bool withDescendants = false, |
| 78 | ]) { | 80 | ]) { |
| 79 | - MediaQuery.maybeOf(context); | 81 | + final instance = ScreenUtil(); |
| 82 | + (instance._elementsToRebuild ??= {}).add(context as Element); | ||
| 83 | + | ||
| 84 | + // MediaQuery.maybeOf(context); | ||
| 80 | if (withDescendants) { | 85 | if (withDescendants) { |
| 81 | - (context as Element).visitChildren((element) { | 86 | + context.visitChildren((element) { |
| 82 | registerToBuild(element, true); | 87 | registerToBuild(element, true); |
| 83 | }); | 88 | }); |
| 84 | } | 89 | } |
| @@ -108,14 +113,16 @@ class ScreenUtil { | @@ -108,14 +113,16 @@ class ScreenUtil { | ||
| 108 | ? Orientation.landscape | 113 | ? Orientation.landscape |
| 109 | : Orientation.portrait); | 114 | : Orientation.portrait); |
| 110 | 115 | ||
| 111 | - _instance = ScreenUtil._() | ||
| 112 | - ..uiSize = designSize | 116 | + _instance |
| 117 | + .._uiSize = designSize | ||
| 113 | .._splitScreenMode = splitScreenMode | 118 | .._splitScreenMode = splitScreenMode |
| 114 | .._minTextAdapt = minTextAdapt | 119 | .._minTextAdapt = minTextAdapt |
| 115 | .._orientation = orientation | 120 | .._orientation = orientation |
| 116 | .._screenWidth = deviceSize.width | 121 | .._screenWidth = deviceSize.width |
| 117 | .._screenHeight = deviceSize.height | 122 | .._screenHeight = deviceSize.height |
| 118 | - ..context = mediaQueryContext != null ? context : null; | 123 | + .._context = context; |
| 124 | + | ||
| 125 | + _instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild()); | ||
| 119 | } | 126 | } |
| 120 | 127 | ||
| 121 | ///获取屏幕方向 | 128 | ///获取屏幕方向 |
| @@ -125,41 +132,41 @@ class ScreenUtil { | @@ -125,41 +132,41 @@ class ScreenUtil { | ||
| 125 | /// 每个逻辑像素的字体像素数,字体的缩放比例 | 132 | /// 每个逻辑像素的字体像素数,字体的缩放比例 |
| 126 | /// The number of font pixels for each logical pixel. | 133 | /// The number of font pixels for each logical pixel. |
| 127 | double get textScaleFactor => | 134 | double get textScaleFactor => |
| 128 | - context != null ? MediaQuery.of(context!).textScaleFactor : 1; | 135 | + _context != null ? MediaQuery.of(_context!).textScaleFactor : 1; |
| 129 | 136 | ||
| 130 | /// 设备的像素密度 | 137 | /// 设备的像素密度 |
| 131 | /// The size of the media in logical pixels (e.g, the size of the screen). | 138 | /// The size of the media in logical pixels (e.g, the size of the screen). |
| 132 | double? get pixelRatio => | 139 | double? get pixelRatio => |
| 133 | - context != null ? MediaQuery.of(context!).devicePixelRatio : 1; | 140 | + _context != null ? MediaQuery.of(_context!).devicePixelRatio : 1; |
| 134 | 141 | ||
| 135 | /// 当前设备宽度 dp | 142 | /// 当前设备宽度 dp |
| 136 | /// The horizontal extent of this size. | 143 | /// The horizontal extent of this size. |
| 137 | double get screenWidth => | 144 | double get screenWidth => |
| 138 | - context != null ? MediaQuery.of(context!).size.width : _screenWidth; | 145 | + _context != null ? MediaQuery.of(_context!).size.width : _screenWidth; |
| 139 | 146 | ||
| 140 | ///当前设备高度 dp | 147 | ///当前设备高度 dp |
| 141 | ///The vertical extent of this size. dp | 148 | ///The vertical extent of this size. dp |
| 142 | double get screenHeight => | 149 | double get screenHeight => |
| 143 | - context != null ? MediaQuery.of(context!).size.height : _screenHeight; | 150 | + _context != null ? MediaQuery.of(_context!).size.height : _screenHeight; |
| 144 | 151 | ||
| 145 | /// 状态栏高度 dp 刘海屏会更高 | 152 | /// 状态栏高度 dp 刘海屏会更高 |
| 146 | /// The offset from the top, in dp | 153 | /// The offset from the top, in dp |
| 147 | double get statusBarHeight => | 154 | double get statusBarHeight => |
| 148 | - context == null ? 0 : MediaQuery.of(context!).padding.top; | 155 | + _context == null ? 0 : MediaQuery.of(_context!).padding.top; |
| 149 | 156 | ||
| 150 | /// 底部安全区距离 dp | 157 | /// 底部安全区距离 dp |
| 151 | /// The offset from the bottom, in dp | 158 | /// The offset from the bottom, in dp |
| 152 | double get bottomBarHeight => | 159 | double get bottomBarHeight => |
| 153 | - context == null ? 0 : MediaQuery.of(context!).padding.bottom; | 160 | + _context == null ? 0 : MediaQuery.of(_context!).padding.bottom; |
| 154 | 161 | ||
| 155 | /// 实际尺寸与UI设计的比例 | 162 | /// 实际尺寸与UI设计的比例 |
| 156 | /// The ratio of actual width to UI design | 163 | /// The ratio of actual width to UI design |
| 157 | - double get scaleWidth => screenWidth / uiSize.width; | 164 | + double get scaleWidth => screenWidth / _uiSize.width; |
| 158 | 165 | ||
| 159 | /// /// The ratio of actual height to UI design | 166 | /// /// The ratio of actual height to UI design |
| 160 | double get scaleHeight => | 167 | double get scaleHeight => |
| 161 | (_splitScreenMode ? max(screenHeight, 700) : screenHeight) / | 168 | (_splitScreenMode ? max(screenHeight, 700) : screenHeight) / |
| 162 | - uiSize.height; | 169 | + _uiSize.height; |
| 163 | 170 | ||
| 164 | double get scaleText => | 171 | double get scaleText => |
| 165 | _minTextAdapt ? min(scaleWidth, scaleHeight) : scaleWidth; | 172 | _minTextAdapt ? min(scaleWidth, scaleHeight) : scaleWidth; |
-
Please register or login to post a comment