Showing
2 changed files
with
27 additions
and
11 deletions
@@ -12,6 +12,7 @@ class MyApp extends StatelessWidget { | @@ -12,6 +12,7 @@ class MyApp extends StatelessWidget { | ||
12 | // In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it | 12 | // In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it |
13 | return ScreenUtilInit( | 13 | return ScreenUtilInit( |
14 | responsiveWidgets: responsiveWidgets, | 14 | responsiveWidgets: responsiveWidgets, |
15 | + ensureScreenSize: true, | ||
15 | child: MaterialApp( | 16 | child: MaterialApp( |
16 | debugShowCheckedModeBanner: false, | 17 | debugShowCheckedModeBanner: false, |
17 | title: 'First Method', | 18 | title: 'First Method', |
1 | +import 'dart:async'; | ||
1 | import 'dart:collection'; | 2 | import 'dart:collection'; |
2 | 3 | ||
3 | import 'package:flutter/widgets.dart'; | 4 | import 'package:flutter/widgets.dart'; |
@@ -99,12 +100,14 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -99,12 +100,14 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
99 | final _canMarkedToBuild = HashSet<String>(); | 100 | final _canMarkedToBuild = HashSet<String>(); |
100 | MediaQueryData? _mediaQueryData; | 101 | MediaQueryData? _mediaQueryData; |
101 | final _binding = WidgetsBinding.instance; | 102 | final _binding = WidgetsBinding.instance; |
103 | + final _screenSizeCompleter = Completer<void>(); | ||
102 | 104 | ||
103 | @override | 105 | @override |
104 | void initState() { | 106 | void initState() { |
105 | if (widget.responsiveWidgets != null) { | 107 | if (widget.responsiveWidgets != null) { |
106 | _canMarkedToBuild.addAll(widget.responsiveWidgets!); | 108 | _canMarkedToBuild.addAll(widget.responsiveWidgets!); |
107 | } | 109 | } |
110 | + _validateSize().then(_screenSizeCompleter.complete); | ||
108 | 111 | ||
109 | super.initState(); | 112 | super.initState(); |
110 | _binding.addObserver(this); | 113 | _binding.addObserver(this); |
@@ -129,6 +132,10 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -129,6 +132,10 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
129 | return mq; | 132 | return mq; |
130 | } | 133 | } |
131 | 134 | ||
135 | + Future<void> _validateSize() async { | ||
136 | + if (widget.ensureScreenSize ?? false) return ScreenUtil.ensureScreenSize(); | ||
137 | + } | ||
138 | + | ||
132 | void _markNeedsBuildIfAllowed(Element el) { | 139 | void _markNeedsBuildIfAllowed(Element el) { |
133 | final widgetName = el.widget.runtimeType.toString(); | 140 | final widgetName = el.widget.runtimeType.toString(); |
134 | final allowed = widget is SU || | 141 | final allowed = widget is SU || |
@@ -162,18 +169,26 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | @@ -162,18 +169,26 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> | ||
162 | Widget build(BuildContext context) { | 169 | Widget build(BuildContext context) { |
163 | final mq = _mediaQueryData; | 170 | final mq = _mediaQueryData; |
164 | 171 | ||
165 | - if (mq == null) return SizedBox.shrink(); | ||
166 | - | ||
167 | - ScreenUtil.configure( | ||
168 | - data: mq, | ||
169 | - designSize: widget.designSize, | ||
170 | - splitScreenMode: widget.splitScreenMode, | ||
171 | - minTextAdapt: widget.minTextAdapt, | ||
172 | - ensureScreenHasSize: widget.ensureScreenSize, | ||
173 | - fontSizeResolver: widget.fontSizeResolver, | 172 | + if (mq == null) return const SizedBox.shrink(); |
173 | + | ||
174 | + return FutureBuilder<void>( | ||
175 | + future: _screenSizeCompleter.future, | ||
176 | + builder: (c, snapshot) { | ||
177 | + ScreenUtil.configure( | ||
178 | + data: mq, | ||
179 | + designSize: widget.designSize, | ||
180 | + splitScreenMode: widget.splitScreenMode, | ||
181 | + minTextAdapt: widget.minTextAdapt, | ||
182 | + fontSizeResolver: widget.fontSizeResolver, | ||
183 | + ); | ||
184 | + | ||
185 | + if (snapshot.connectionState == ConnectionState.done) { | ||
186 | + return widget.builder?.call(context, widget.child) ?? widget.child!; | ||
187 | + } | ||
188 | + | ||
189 | + return const SizedBox.shrink(); | ||
190 | + }, | ||
174 | ); | 191 | ); |
175 | - | ||
176 | - return widget.builder?.call(context, widget.child) ?? widget.child!; | ||
177 | } | 192 | } |
178 | 193 | ||
179 | @override | 194 | @override |
-
Please register or login to post a comment