Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ class _ScreenInfoDisplayState extends State<ScreenInfoDisplay> {
@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);
Expand Down
30 changes: 18 additions & 12 deletions lib/screen_helper_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ScreenHelperWidget extends StatefulWidget {

class _ScreenHelperWidgetState extends State<ScreenHelperWidget>
with WidgetsBindingObserver {
ScreenInfoData? _screenInfoData;
Future<ScreenInfoData?>? _screenInfoDataFuture;

@override
void initState() {
Expand Down Expand Up @@ -73,30 +73,36 @@ class _ScreenHelperWidgetState extends State<ScreenHelperWidget>
void _updateScreenInfoData() {
final dpi = MediaQuery.maybeDevicePixelRatioOf(context);
if (dpi != null) {
_fetchAndSetScreenInfo(dpi);
setState(() {
_screenInfoDataFuture = _fetchAndSetScreenInfo(dpi);
});
}
}

Future<void> _fetchAndSetScreenInfo(double dpi) async {
Future<ScreenInfoData?> _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<ScreenInfoData?>(
future: _screenInfoDataFuture,
builder: (context, snapshot) {
return ScreenInfo(
screenInfoData: snapshot.data,
child: widget.child,
);
},
);
}
}