Showing
4 changed files
with
27 additions
and
101 deletions
| @@ -9,7 +9,6 @@ class MyApp extends StatelessWidget { | @@ -9,7 +9,6 @@ class MyApp extends StatelessWidget { | ||
| 9 | Widget build(BuildContext context) { | 9 | Widget build(BuildContext context) { |
| 10 | // In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it | 10 | // In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it |
| 11 | return ScreenUtilInit( | 11 | return ScreenUtilInit( |
| 12 | - useInheritedMediaQuery: true, | ||
| 13 | builder: (_, child) { | 12 | builder: (_, child) { |
| 14 | return MaterialApp( | 13 | return MaterialApp( |
| 15 | debugShowCheckedModeBanner: false, | 14 | debugShowCheckedModeBanner: false, |
| @@ -129,6 +129,7 @@ class HomePageScaffold extends StatelessWidget { | @@ -129,6 +129,7 @@ class HomePageScaffold extends StatelessWidget { | ||
| 129 | showModalBottomSheet<void>( | 129 | showModalBottomSheet<void>( |
| 130 | context: context, | 130 | context: context, |
| 131 | builder: (BuildContext context) { | 131 | builder: (BuildContext context) { |
| 132 | + print('BottomSheet build'); | ||
| 132 | return Container( | 133 | return Container( |
| 133 | height: 200.w + | 134 | height: 200.w + |
| 134 | MediaQuery.of(context).viewInsets.bottom, | 135 | MediaQuery.of(context).viewInsets.bottom, |
| @@ -42,7 +42,6 @@ void main() { | @@ -42,7 +42,6 @@ void main() { | ||
| 42 | return MediaQuery( | 42 | return MediaQuery( |
| 43 | data: currentData, | 43 | data: currentData, |
| 44 | child: ScreenUtilInit( | 44 | child: ScreenUtilInit( |
| 45 | - useInheritedMediaQuery: true, | ||
| 46 | designSize: designSize, | 45 | designSize: designSize, |
| 47 | builder: (context, child) => MaterialApp( | 46 | builder: (context, child) => MaterialApp( |
| 48 | home: Material( | 47 | home: Material( |
| @@ -39,7 +39,6 @@ class ScreenUtilInit extends StatefulWidget { | @@ -39,7 +39,6 @@ class ScreenUtilInit extends StatefulWidget { | ||
| 39 | this.designSize = ScreenUtil.defaultSize, | 39 | this.designSize = ScreenUtil.defaultSize, |
| 40 | this.splitScreenMode = false, | 40 | this.splitScreenMode = false, |
| 41 | this.minTextAdapt = false, | 41 | this.minTextAdapt = false, |
| 42 | - this.useInheritedMediaQuery = false, | ||
| 43 | this.scaleByHeight = false}) | 42 | this.scaleByHeight = false}) |
| 44 | : super(key: key); | 43 | : super(key: key); |
| 45 | 44 | ||
| @@ -47,7 +46,6 @@ class ScreenUtilInit extends StatefulWidget { | @@ -47,7 +46,6 @@ class ScreenUtilInit extends StatefulWidget { | ||
| 47 | final Widget? child; | 46 | final Widget? child; |
| 48 | final bool splitScreenMode; | 47 | final bool splitScreenMode; |
| 49 | final bool minTextAdapt; | 48 | final bool minTextAdapt; |
| 50 | - final bool useInheritedMediaQuery; | ||
| 51 | final bool scaleByHeight; | 49 | final bool scaleByHeight; |
| 52 | final RebuildFactor rebuildFactor; | 50 | final RebuildFactor rebuildFactor; |
| 53 | 51 | ||
| @@ -62,60 +60,19 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -62,60 +60,19 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
| 62 | with WidgetsBindingObserver { | 60 | with WidgetsBindingObserver { |
| 63 | MediaQueryData? _mediaQueryData; | 61 | MediaQueryData? _mediaQueryData; |
| 64 | 62 | ||
| 65 | - bool wrappedInMediaQuery = false; | ||
| 66 | - | ||
| 67 | WidgetsBinding get binding => WidgetsFlutterBinding.ensureInitialized(); | 63 | WidgetsBinding get binding => WidgetsFlutterBinding.ensureInitialized(); |
| 68 | 64 | ||
| 69 | - MediaQueryData get mediaQueryData => _mediaQueryData!; | ||
| 70 | - | ||
| 71 | - MediaQueryData get newData { | ||
| 72 | - if (widget.useInheritedMediaQuery) { | ||
| 73 | - final data = MediaQuery.maybeOf(context); | ||
| 74 | - | ||
| 75 | - if (data != null) { | ||
| 76 | - wrappedInMediaQuery = true; | ||
| 77 | - return data; | ||
| 78 | - } | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - return MediaQueryData.fromView(View.of(context)); | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | Widget get child { | 65 | Widget get child { |
| 85 | return widget.builder.call(context, widget.child); | 66 | return widget.builder.call(context, widget.child); |
| 86 | } | 67 | } |
| 87 | 68 | ||
| 88 | - _updateTree(Element el) { | ||
| 89 | - el.markNeedsBuild(); | ||
| 90 | - el.visitChildren(_updateTree); | ||
| 91 | - } | ||
| 92 | - | ||
| 93 | @override | 69 | @override |
| 94 | void initState() { | 70 | void initState() { |
| 95 | super.initState(); | 71 | super.initState(); |
| 96 | - // mediaQueryData = newData; | ||
| 97 | binding.addObserver(this); | 72 | binding.addObserver(this); |
| 98 | } | 73 | } |
| 99 | 74 | ||
| 100 | @override | 75 | @override |
| 101 | - void didChangeMetrics() { | ||
| 102 | - final old = _mediaQueryData!; | ||
| 103 | - final data = newData; | ||
| 104 | - | ||
| 105 | - if (widget.scaleByHeight || widget.rebuildFactor(old, data)) { | ||
| 106 | - _mediaQueryData = data; | ||
| 107 | - _updateTree(context as Element); | ||
| 108 | - } | ||
| 109 | - } | ||
| 110 | - | ||
| 111 | - @override | ||
| 112 | - void didChangeDependencies() { | ||
| 113 | - super.didChangeDependencies(); | ||
| 114 | - if (_mediaQueryData == null) _mediaQueryData = newData; | ||
| 115 | - didChangeMetrics(); | ||
| 116 | - } | ||
| 117 | - | ||
| 118 | - @override | ||
| 119 | void dispose() { | 76 | void dispose() { |
| 120 | binding.removeObserver(this); | 77 | binding.removeObserver(this); |
| 121 | super.dispose(); | 78 | super.dispose(); |
| @@ -123,62 +80,32 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -123,62 +80,32 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
| 123 | 80 | ||
| 124 | @override | 81 | @override |
| 125 | Widget build(BuildContext _context) { | 82 | Widget build(BuildContext _context) { |
| 126 | - if (mediaQueryData.size == Size.zero) return const SizedBox.shrink(); | ||
| 127 | - if (!wrappedInMediaQuery) { | ||
| 128 | - return MediaQuery( | ||
| 129 | - // key: GlobalObjectKey('mediaQuery'), | ||
| 130 | - data: mediaQueryData, | ||
| 131 | - child: Builder( | ||
| 132 | - builder: (__context) { | ||
| 133 | - final deviceData = MediaQuery.maybeOf(__context); | ||
| 134 | - final deviceSize = deviceData?.size ?? widget.designSize; | ||
| 135 | - ScreenUtil.init(__context, | ||
| 136 | - designSize: widget.designSize, | ||
| 137 | - splitScreenMode: widget.splitScreenMode, | ||
| 138 | - minTextAdapt: widget.minTextAdapt, | ||
| 139 | - scaleByHeight: widget.scaleByHeight); | ||
| 140 | - return Container( | ||
| 141 | - width: deviceSize.width, | ||
| 142 | - height: deviceSize.height, | ||
| 143 | - child: FittedBox( | ||
| 144 | - fit: BoxFit.none, | ||
| 145 | - alignment: Alignment.center, | ||
| 146 | - child: Container( | ||
| 147 | - width: widget.scaleByHeight | ||
| 148 | - ? (deviceSize.height * widget.designSize.width) / | ||
| 149 | - widget.designSize.height | ||
| 150 | - : deviceSize.width, | ||
| 151 | - height: deviceSize.height, | ||
| 152 | - child: child, | ||
| 153 | - ), | ||
| 154 | - )); | ||
| 155 | - }, | ||
| 156 | - ), | ||
| 157 | - ); | ||
| 158 | - } | ||
| 159 | - | ||
| 160 | - ScreenUtil.init(_context, | ||
| 161 | - designSize: widget.designSize, | ||
| 162 | - splitScreenMode: widget.splitScreenMode, | ||
| 163 | - minTextAdapt: widget.minTextAdapt, | ||
| 164 | - scaleByHeight: widget.scaleByHeight); | ||
| 165 | - final deviceData = MediaQuery.maybeOf(_context); | ||
| 166 | - | ||
| 167 | - final deviceSize = deviceData?.size ?? widget.designSize; | ||
| 168 | - return Container( | ||
| 169 | - width: deviceSize.width, | ||
| 170 | - height: deviceSize.height, | ||
| 171 | - child: FittedBox( | ||
| 172 | - fit: BoxFit.none, | ||
| 173 | - alignment: Alignment.center, | ||
| 174 | - child: Container( | ||
| 175 | - width: widget.scaleByHeight | ||
| 176 | - ? (deviceSize.height * widget.designSize.width) / | ||
| 177 | - widget.designSize.height | ||
| 178 | - : deviceSize.width, | 83 | + if (_mediaQueryData?.size == Size.zero) return const SizedBox.shrink(); |
| 84 | + return Builder( | ||
| 85 | + builder: (__context) { | ||
| 86 | + final deviceData = MediaQuery.maybeOf(__context); | ||
| 87 | + final deviceSize = deviceData?.size ?? widget.designSize; | ||
| 88 | + ScreenUtil.init(__context, | ||
| 89 | + designSize: widget.designSize, | ||
| 90 | + splitScreenMode: widget.splitScreenMode, | ||
| 91 | + minTextAdapt: widget.minTextAdapt, | ||
| 92 | + scaleByHeight: widget.scaleByHeight); | ||
| 93 | + return Container( | ||
| 94 | + width: deviceSize.width, | ||
| 179 | height: deviceSize.height, | 95 | height: deviceSize.height, |
| 180 | - child: child, | ||
| 181 | - ), | ||
| 182 | - )); | 96 | + child: FittedBox( |
| 97 | + fit: BoxFit.none, | ||
| 98 | + alignment: Alignment.center, | ||
| 99 | + child: Container( | ||
| 100 | + width: widget.scaleByHeight | ||
| 101 | + ? (deviceSize.height * widget.designSize.width) / | ||
| 102 | + widget.designSize.height | ||
| 103 | + : deviceSize.width, | ||
| 104 | + height: deviceSize.height, | ||
| 105 | + child: child, | ||
| 106 | + ), | ||
| 107 | + )); | ||
| 108 | + }, | ||
| 109 | + ); | ||
| 183 | } | 110 | } |
| 184 | } | 111 | } |
-
Please register or login to post a comment