Showing
2 changed files
with
76 additions
and
2 deletions
@@ -9,6 +9,7 @@ class MyApp extends StatelessWidget { | @@ -9,6 +9,7 @@ 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, | ||
12 | builder: (_, child) { | 13 | builder: (_, child) { |
13 | return MaterialApp( | 14 | return MaterialApp( |
14 | debugShowCheckedModeBanner: false, | 15 | debugShowCheckedModeBanner: false, |
@@ -39,6 +39,7 @@ class ScreenUtilInit extends StatefulWidget { | @@ -39,6 +39,7 @@ 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, | ||
42 | this.scaleByHeight = false}) | 43 | this.scaleByHeight = false}) |
43 | : super(key: key); | 44 | : super(key: key); |
44 | 45 | ||
@@ -46,6 +47,7 @@ class ScreenUtilInit extends StatefulWidget { | @@ -46,6 +47,7 @@ class ScreenUtilInit extends StatefulWidget { | ||
46 | final Widget? child; | 47 | final Widget? child; |
47 | final bool splitScreenMode; | 48 | final bool splitScreenMode; |
48 | final bool minTextAdapt; | 49 | final bool minTextAdapt; |
50 | + final bool useInheritedMediaQuery; | ||
49 | final bool scaleByHeight; | 51 | final bool scaleByHeight; |
50 | final RebuildFactor rebuildFactor; | 52 | final RebuildFactor rebuildFactor; |
51 | 53 | ||
@@ -60,19 +62,60 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -60,19 +62,60 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
60 | with WidgetsBindingObserver { | 62 | with WidgetsBindingObserver { |
61 | MediaQueryData? _mediaQueryData; | 63 | MediaQueryData? _mediaQueryData; |
62 | 64 | ||
65 | + bool wrappedInMediaQuery = false; | ||
66 | + | ||
63 | WidgetsBinding get binding => WidgetsFlutterBinding.ensureInitialized(); | 67 | WidgetsBinding get binding => WidgetsFlutterBinding.ensureInitialized(); |
64 | 68 | ||
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 | + | ||
65 | Widget get child { | 84 | Widget get child { |
66 | return widget.builder.call(context, widget.child); | 85 | return widget.builder.call(context, widget.child); |
67 | } | 86 | } |
68 | 87 | ||
88 | + _updateTree(Element el) { | ||
89 | + el.markNeedsBuild(); | ||
90 | + el.visitChildren(_updateTree); | ||
91 | + } | ||
92 | + | ||
69 | @override | 93 | @override |
70 | void initState() { | 94 | void initState() { |
71 | super.initState(); | 95 | super.initState(); |
96 | + // mediaQueryData = newData; | ||
72 | binding.addObserver(this); | 97 | binding.addObserver(this); |
73 | } | 98 | } |
74 | 99 | ||
75 | @override | 100 | @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 | ||
76 | void dispose() { | 119 | void dispose() { |
77 | binding.removeObserver(this); | 120 | binding.removeObserver(this); |
78 | super.dispose(); | 121 | super.dispose(); |
@@ -80,8 +123,12 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -80,8 +123,12 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
80 | 123 | ||
81 | @override | 124 | @override |
82 | Widget build(BuildContext _context) { | 125 | Widget build(BuildContext _context) { |
83 | - if (_mediaQueryData?.size == Size.zero) return const SizedBox.shrink(); | ||
84 | - return Builder( | 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( | ||
85 | builder: (__context) { | 132 | builder: (__context) { |
86 | final deviceData = MediaQuery.maybeOf(__context); | 133 | final deviceData = MediaQuery.maybeOf(__context); |
87 | final deviceSize = deviceData?.size ?? widget.designSize; | 134 | final deviceSize = deviceData?.size ?? widget.designSize; |
@@ -106,6 +153,32 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -106,6 +153,32 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
106 | ), | 153 | ), |
107 | )); | 154 | )); |
108 | }, | 155 | }, |
156 | + ), | ||
109 | ); | 157 | ); |
110 | } | 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, | ||
179 | + height: deviceSize.height, | ||
180 | + child: child, | ||
181 | + ), | ||
182 | + )); | ||
183 | + } | ||
111 | } | 184 | } |
-
Please register or login to post a comment