ensureScreenSize static method
Future<void>
ensureScreenSize([
- dynamic window,
- Duration duration = const Duration(milliseconds: 10)
Manually wait for window size to be initialized
Recommended
to use before you need access window size
or in custom splash/bootstrap screen FutureBuilder
example:
...
ScreenUtil.init(context, ...);
...
FutureBuilder(
future: Future.wait([..., ensureScreenSize(), ...]),
builder: (context, snapshot) {
if (snapshot.hasData) return const HomeScreen();
return Material(
child: LayoutBuilder(
...
),
);
},
)
Implementation
static Future<void> ensureScreenSize([
FlutterWindow? window,
Duration duration = const Duration(milliseconds: 10),
]) async {
final binding = WidgetsFlutterBinding.ensureInitialized();
window ??= binding.window;
if (window.viewConfiguration.geometry.isEmpty) {
return Future.delayed(duration, () async {
binding.deferFirstFrame();
await ensureScreenSize(window, duration);
return binding.allowFirstFrame();
});
}
}