Showing
1 changed file
with
25 additions
and
13 deletions
1 | import 'package:flutter/widgets.dart'; | 1 | import 'package:flutter/widgets.dart'; |
2 | -import 'package:flutter/rendering.dart'; | ||
3 | 2 | ||
4 | import 'screen_util.dart'; | 3 | import 'screen_util.dart'; |
5 | 4 | ||
@@ -12,7 +11,11 @@ class ScreenUtilInit extends StatelessWidget { | @@ -12,7 +11,11 @@ class ScreenUtilInit extends StatelessWidget { | ||
12 | this.splitScreenMode = false, | 11 | this.splitScreenMode = false, |
13 | this.minTextAdapt = false, | 12 | this.minTextAdapt = false, |
14 | Key? key, | 13 | Key? key, |
15 | - }) : super(key: key); | 14 | + }) : assert( |
15 | + builder != null || child != null, | ||
16 | + 'You must either pass builder or child or both', | ||
17 | + ), | ||
18 | + super(key: key); | ||
16 | 19 | ||
17 | final Widget Function(Widget? child)? builder; | 20 | final Widget Function(Widget? child)? builder; |
18 | final Widget? child; | 21 | final Widget? child; |
@@ -23,28 +26,37 @@ class ScreenUtilInit extends StatelessWidget { | @@ -23,28 +26,37 @@ class ScreenUtilInit extends StatelessWidget { | ||
23 | final Size designSize; | 26 | final Size designSize; |
24 | 27 | ||
25 | @override | 28 | @override |
26 | - Widget build(BuildContext _) { | 29 | + Widget build(BuildContext context) { |
27 | bool firstFrameAllowed = false; | 30 | bool firstFrameAllowed = false; |
28 | - RendererBinding.instance!.deferFirstFrame(); | 31 | + final binding = WidgetsFlutterBinding.ensureInitialized(); |
32 | + binding.deferFirstFrame(); | ||
33 | + | ||
34 | + final rootMediaQueryData = (context | ||
35 | + .getElementForInheritedWidgetOfExactType<MediaQuery>() | ||
36 | + ?.widget as MediaQuery?) | ||
37 | + ?.data; | ||
29 | 38 | ||
30 | return LayoutBuilder( | 39 | return LayoutBuilder( |
31 | - builder: (context, constraints) { | 40 | + builder: (_, constraints) { |
32 | if (constraints.biggest == Size.zero) return const SizedBox.shrink(); | 41 | if (constraints.biggest == Size.zero) return const SizedBox.shrink(); |
33 | 42 | ||
43 | + if (!firstFrameAllowed) { | ||
44 | + binding.allowFirstFrame(); | ||
45 | + firstFrameAllowed = true; | ||
46 | + } | ||
47 | + | ||
48 | + return MediaQuery( | ||
49 | + data: rootMediaQueryData ?? MediaQueryData.fromWindow(binding.window), | ||
50 | + child: Builder(builder: (_context) { | ||
34 | ScreenUtil.init( | 51 | ScreenUtil.init( |
35 | - null, | ||
36 | - deviceSize: constraints.biggest, | 52 | + _context, |
37 | designSize: designSize, | 53 | designSize: designSize, |
38 | splitScreenMode: splitScreenMode, | 54 | splitScreenMode: splitScreenMode, |
39 | minTextAdapt: minTextAdapt, | 55 | minTextAdapt: minTextAdapt, |
40 | ); | 56 | ); |
41 | - | ||
42 | - if (!firstFrameAllowed) { | ||
43 | - RendererBinding.instance!.allowFirstFrame(); | ||
44 | - firstFrameAllowed = true; | ||
45 | - } | ||
46 | - | ||
47 | return builder?.call(child) ?? child!; | 57 | return builder?.call(child) ?? child!; |
58 | + }), | ||
59 | + ); | ||
48 | }, | 60 | }, |
49 | ); | 61 | ); |
50 | } | 62 | } |
-
Please register or login to post a comment