From 36ed3724dd2a987a3c62bf58b71e5d67e19de58b Mon Sep 17 00:00:00 2001 From: Sager Saif Nabhan Al Shukaili <69807558+TOZXII@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:49:52 +0400 Subject: [PATCH] Fix screenInfo availability issue Fixes #3 Add a `FutureBuilder` to `ScreenHelperWidget` to wait for `screenInfoData` to be available. * Modify `_fetchAndSetScreenInfo` to return a `Future` in `lib/screen_helper_widget.dart`. * Update `build` method in `lib/screen_helper_widget.dart` to use `FutureBuilder` to wait for `screenInfoData`. * Remove the null check for `screenInfo` in `ScreenInfoDisplay` in `example/lib/main.dart`. * Remove the `CircularProgressIndicator` in `ScreenInfoDisplay` in `example/lib/main.dart`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/TOZXII/screen_helper/issues/3?shareId=XXXX-XXXX-XXXX-XXXX). --- example/lib/main.dart | 5 +---- lib/screen_helper_widget.dart | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 8048f98..7c1e931 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -44,10 +44,7 @@ class _ScreenInfoDisplayState extends State { @override Widget build(BuildContext context) { // Access screen information using ScreenInfo.of(context) - final screenInfo = ScreenInfo.maybeOf(context); - if (screenInfo == null) { - return const CircularProgressIndicator(); - } + final screenInfo = ScreenInfo.of(context); // Use the extension method to convert mm to pixels final lineLengthInPixels = context.mmToPx(_lineLengthMm); diff --git a/lib/screen_helper_widget.dart b/lib/screen_helper_widget.dart index 10da7e1..639b219 100644 --- a/lib/screen_helper_widget.dart +++ b/lib/screen_helper_widget.dart @@ -40,7 +40,7 @@ class ScreenHelperWidget extends StatefulWidget { class _ScreenHelperWidgetState extends State with WidgetsBindingObserver { - ScreenInfoData? _screenInfoData; + Future? _screenInfoDataFuture; @override void initState() { @@ -73,30 +73,36 @@ class _ScreenHelperWidgetState extends State void _updateScreenInfoData() { final dpi = MediaQuery.maybeDevicePixelRatioOf(context); if (dpi != null) { - _fetchAndSetScreenInfo(dpi); + setState(() { + _screenInfoDataFuture = _fetchAndSetScreenInfo(dpi); + }); } } - Future _fetchAndSetScreenInfo(double dpi) async { + Future _fetchAndSetScreenInfo(double dpi) async { final sizeInInches = await ScreenHelperPlatform.instance.getScreenSizeInInches(); final resolution = await ScreenHelperPlatform.instance.getScreenResolution(); if (sizeInInches != null && resolution != null) { - setState(() { - _screenInfoData = ScreenInfoData( - dpi: dpi, - screenSizeInInches: sizeInInches, - screenResolution: resolution); - }); + return ScreenInfoData( + dpi: dpi, + screenSizeInInches: sizeInInches, + screenResolution: resolution); } + return null; } @override Widget build(BuildContext context) { - return ScreenInfo( - screenInfoData: _screenInfoData, - child: widget.child, + return FutureBuilder( + future: _screenInfoDataFuture, + builder: (context, snapshot) { + return ScreenInfo( + screenInfoData: snapshot.data, + child: widget.child, + ); + }, ); } }