Showing
2 changed files
with
54 additions
and
14 deletions
| @@ -94,6 +94,7 @@ class ScreenUtil { | @@ -94,6 +94,7 @@ class ScreenUtil { | ||
| 94 | Size designSize = defaultSize, | 94 | Size designSize = defaultSize, |
| 95 | bool splitScreenMode = false, | 95 | bool splitScreenMode = false, |
| 96 | bool minTextAdapt = false, | 96 | bool minTextAdapt = false, |
| 97 | + bool scaleByHeight = false | ||
| 97 | }) async { | 98 | }) async { |
| 98 | final navigatorContext = Navigator.maybeOf(context)?.context as Element?; | 99 | final navigatorContext = Navigator.maybeOf(context)?.context as Element?; |
| 99 | final mediaQueryContext = | 100 | final mediaQueryContext = |
| @@ -115,12 +116,13 @@ class ScreenUtil { | @@ -115,12 +116,13 @@ class ScreenUtil { | ||
| 115 | : Orientation.portrait); | 116 | : Orientation.portrait); |
| 116 | 117 | ||
| 117 | _instance | 118 | _instance |
| 118 | - .._context = context | 119 | + .._context = scaleByHeight ? null : context |
| 119 | .._uiSize = designSize | 120 | .._uiSize = designSize |
| 120 | .._splitScreenMode = splitScreenMode | 121 | .._splitScreenMode = splitScreenMode |
| 121 | .._minTextAdapt = minTextAdapt | 122 | .._minTextAdapt = minTextAdapt |
| 122 | .._orientation = orientation | 123 | .._orientation = orientation |
| 123 | - .._screenWidth = deviceSize.width | 124 | + .._screenWidth = scaleByHeight ? (deviceSize.height * designSize.width) / |
| 125 | + designSize.height : deviceSize.width | ||
| 124 | .._screenHeight = deviceSize.height; | 126 | .._screenHeight = deviceSize.height; |
| 125 | 127 | ||
| 126 | _instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild()); | 128 | _instance._elementsToRebuild?.forEach((el) => el.markNeedsBuild()); |
| 1 | import 'package:flutter/widgets.dart'; | 1 | import 'package:flutter/widgets.dart'; |
| 2 | 2 | ||
| 3 | import 'screen_util.dart'; | 3 | import 'screen_util.dart'; |
| 4 | +import 'dart:ui' show FlutterWindow; | ||
| 4 | 5 | ||
| 5 | typedef RebuildFactor = bool Function(MediaQueryData old, MediaQueryData data); | 6 | typedef RebuildFactor = bool Function(MediaQueryData old, MediaQueryData data); |
| 6 | 7 | ||
| @@ -40,6 +41,7 @@ class ScreenUtilInit extends StatefulWidget { | @@ -40,6 +41,7 @@ class ScreenUtilInit extends StatefulWidget { | ||
| 40 | this.splitScreenMode = false, | 41 | this.splitScreenMode = false, |
| 41 | this.minTextAdapt = false, | 42 | this.minTextAdapt = false, |
| 42 | this.useInheritedMediaQuery = false, | 43 | this.useInheritedMediaQuery = false, |
| 44 | + this.scaleByHeight = false | ||
| 43 | }) : super(key: key); | 45 | }) : super(key: key); |
| 44 | 46 | ||
| 45 | final ScreenUtilInitBuilder builder; | 47 | final ScreenUtilInitBuilder builder; |
| @@ -47,6 +49,7 @@ class ScreenUtilInit extends StatefulWidget { | @@ -47,6 +49,7 @@ class ScreenUtilInit extends StatefulWidget { | ||
| 47 | final bool splitScreenMode; | 49 | final bool splitScreenMode; |
| 48 | final bool minTextAdapt; | 50 | final bool minTextAdapt; |
| 49 | final bool useInheritedMediaQuery; | 51 | final bool useInheritedMediaQuery; |
| 52 | + final bool scaleByHeight; | ||
| 50 | final RebuildFactor rebuildFactor; | 53 | final RebuildFactor rebuildFactor; |
| 51 | 54 | ||
| 52 | /// The [Size] of the device in the design draft, in dp | 55 | /// The [Size] of the device in the design draft, in dp |
| @@ -100,7 +103,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -100,7 +103,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
| 100 | final old = _mediaQueryData!; | 103 | final old = _mediaQueryData!; |
| 101 | final data = newData; | 104 | final data = newData; |
| 102 | 105 | ||
| 103 | - if (widget.rebuildFactor(old, data)) { | 106 | + if (widget.scaleByHeight || widget.rebuildFactor(old, data)) { |
| 104 | _mediaQueryData = data; | 107 | _mediaQueryData = data; |
| 105 | _updateTree(context as Element); | 108 | _updateTree(context as Element); |
| 106 | } | 109 | } |
| @@ -128,25 +131,60 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -128,25 +131,60 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
| 128 | data: mediaQueryData, | 131 | data: mediaQueryData, |
| 129 | child: Builder( | 132 | child: Builder( |
| 130 | builder: (__context) { | 133 | builder: (__context) { |
| 134 | + final deviceData = MediaQuery.maybeOf(__context); | ||
| 135 | + final deviceSize = deviceData?.size ?? widget.designSize; | ||
| 131 | ScreenUtil.init( | 136 | ScreenUtil.init( |
| 132 | - __context, | ||
| 133 | - designSize: widget.designSize, | ||
| 134 | - splitScreenMode: widget.splitScreenMode, | ||
| 135 | - minTextAdapt: widget.minTextAdapt, | 137 | + __context, |
| 138 | + designSize: widget.designSize, | ||
| 139 | + splitScreenMode: widget.splitScreenMode, | ||
| 140 | + minTextAdapt: widget.minTextAdapt, | ||
| 141 | + scaleByHeight: widget.scaleByHeight | ||
| 142 | + ); | ||
| 143 | + return Container( | ||
| 144 | + width: deviceSize.width, | ||
| 145 | + height: deviceSize.height, | ||
| 146 | + child: FittedBox( | ||
| 147 | + fit: BoxFit.none, | ||
| 148 | + alignment: Alignment.center, | ||
| 149 | + child: Container( | ||
| 150 | + width: widget.scaleByHeight | ||
| 151 | + ? (deviceSize.height * widget.designSize.width) / | ||
| 152 | + widget.designSize.height | ||
| 153 | + : deviceSize.width, | ||
| 154 | + height: deviceSize.height, | ||
| 155 | + child: child, | ||
| 156 | + ), | ||
| 157 | + ) | ||
| 136 | ); | 158 | ); |
| 137 | - return child; | ||
| 138 | }, | 159 | }, |
| 139 | ), | 160 | ), |
| 140 | ); | 161 | ); |
| 141 | } | 162 | } |
| 142 | 163 | ||
| 143 | ScreenUtil.init( | 164 | ScreenUtil.init( |
| 144 | - _context, | ||
| 145 | - designSize: widget.designSize, | ||
| 146 | - splitScreenMode: widget.splitScreenMode, | ||
| 147 | - minTextAdapt: widget.minTextAdapt, | 165 | + _context, |
| 166 | + designSize: widget.designSize, | ||
| 167 | + splitScreenMode: widget.splitScreenMode, | ||
| 168 | + minTextAdapt: widget.minTextAdapt, | ||
| 169 | + scaleByHeight: widget.scaleByHeight | ||
| 170 | + ); | ||
| 171 | + final deviceData = MediaQuery.maybeOf(_context); | ||
| 172 | + | ||
| 173 | + final deviceSize = deviceData?.size ?? widget.designSize; | ||
| 174 | + return Container( | ||
| 175 | + width: deviceSize.width, | ||
| 176 | + height: deviceSize.height, | ||
| 177 | + child: FittedBox( | ||
| 178 | + fit: BoxFit.none, | ||
| 179 | + alignment: Alignment.center, | ||
| 180 | + child: Container( | ||
| 181 | + width: widget.scaleByHeight ? (deviceSize.height * | ||
| 182 | + widget.designSize.width) / widget.designSize.height : deviceSize | ||
| 183 | + .width, | ||
| 184 | + height: deviceSize.height, | ||
| 185 | + child: child, | ||
| 186 | + ), | ||
| 187 | + ) | ||
| 148 | ); | 188 | ); |
| 149 | - | ||
| 150 | - return child; | ||
| 151 | } | 189 | } |
| 152 | } | 190 | } |
-
Please register or login to post a comment