11import 'package:display_metrics/display_metrics.dart' ;
22import 'package:flutter/material.dart' ;
33
4- class MetricsScreen extends StatelessWidget {
4+ class MetricsScreen extends StatefulWidget {
55 const MetricsScreen ({super .key});
66
7+ @override
8+ State <MetricsScreen > createState () => _MetricsScreenState ();
9+ }
10+
11+ class _MetricsScreenState extends State <MetricsScreen > {
12+ bool _isInitializing = true ;
13+
14+ @override
15+ void didChangeDependencies () {
16+ super .didChangeDependencies ();
17+ // call DisplayMetrics.ensureInitialized(context) to ensure
18+ // DisplayMetricsData has been loaded
19+ DisplayMetrics .ensureInitialized (context)? .then ((data) {
20+ if (_isInitializing) {
21+ debugPrint ('DisplayMetrics initialized with data [$data ]' );
22+ setState (() {
23+ _isInitializing = false ;
24+ });
25+ }
26+ });
27+ }
28+
729 @override
830 Widget build (BuildContext context) {
9- // call DisplayMetrics.maybeOf(context) or DisplayMetrics.of(context)
10- // to get DisplayMetricsData
11- final metrics = DisplayMetrics .maybeOf (context);
12- if (metrics == null ) {
31+ // if you want, you can show loading widget
32+ // while DisplayMetrics is initializing
33+ if (_isInitializing) {
1334 return const Center (
1435 child: CircularProgressIndicator (),
1536 );
1637 }
38+
39+ // call DisplayMetrics.maybeOf(context)
40+ // or DisplayMetrics.of(context)
41+ // to get DisplayMetricsData
42+ final metrics = DisplayMetrics .of (context);
43+
1744 return Scaffold (
1845 appBar: AppBar (
1946 title: const Text ('Display metrics example app' ),
@@ -53,7 +80,7 @@ class DisplayInfoWidget extends StatelessWidget {
5380 super .key,
5481 });
5582
56- final DisplayMetricsData ? metrics;
83+ final DisplayMetricsData metrics;
5784
5885 @override
5986 Widget build (BuildContext context) {
@@ -66,29 +93,29 @@ class DisplayInfoWidget extends StatelessWidget {
6693 children: [
6794 MetricsLabel (
6895 title: 'ppi' ,
69- value: '${ metrics ? .ppi .toStringAsFixed (0 )}' ,
96+ value: metrics.ppi.toStringAsFixed (0 ),
7097 ),
7198 MetricsLabel (
7299 title: 'devicePixelRatio' ,
73- value: '${ metrics ? .devicePixelRatio .toStringAsFixed (0 )}' ,
100+ value: metrics.devicePixelRatio.toStringAsFixed (0 ),
74101 ),
75102 MetricsLabel (
76103 title: 'inchesToLogicalPixelRatio' ,
77- value: '${ metrics ? .inchesToLogicalPixelRatio .toStringAsFixed (0 )}' ,
104+ value: metrics.inchesToLogicalPixelRatio.toStringAsFixed (0 ),
78105 ),
79106 MetricsLabel (
80107 title: 'diagonal (inches)' ,
81- value: '${ metrics ? .diagonal .toStringAsFixed (2 )}' ,
108+ value: metrics.diagonal.toStringAsFixed (2 ),
82109 ),
83110 MetricsLabel (
84111 title: 'physicalSize (inches)' ,
85- value: '${metrics ? .physicalSize .width .toStringAsFixed (2 )} (w) x '
86- '${metrics ? .physicalSize .height .toStringAsFixed (2 )} (h)' ,
112+ value: '${metrics .physicalSize .width .toStringAsFixed (2 )} (w) x '
113+ '${metrics .physicalSize .height .toStringAsFixed (2 )} (h)' ,
87114 ),
88115 MetricsLabel (
89116 title: 'resolution (pixels)' ,
90- value: '${metrics ? .resolution .width .toStringAsFixed (0 )} (w) x '
91- '${metrics ? .resolution .height .toStringAsFixed (0 )} (h)' ,
117+ value: '${metrics .resolution .width .toStringAsFixed (0 )} (w) x '
118+ '${metrics .resolution .height .toStringAsFixed (0 )} (h)' ,
92119 ),
93120 ],
94121 ),
0 commit comments