Skip to content

Commit 9a5386b

Browse files
committed
Added *ensureInitialized* method, Added *mounted* check, Updated *display_metrics_ios*
1 parent 26ffff8 commit 9a5386b

File tree

10 files changed

+588
-116
lines changed

10 files changed

+588
-116
lines changed

display_metrics/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.1.0
2+
* Added *ensureInitialized* method
3+
* Added *mounted* check
4+
* Updated *display_metrics_ios* dependency (added Spring 2025 iOS devices support: iPhone 16e, iPad Air M3 (7th gen) 11", 13", iPad A16 (11th gen) 11")
5+
16
## 1.0.0
27
* Added **Linux** platform support
38

display_metrics/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
1313

1414
```yaml
1515
dependencies:
16-
display_metrics: ^1.0.0
16+
display_metrics: ^1.1.0
1717
```
1818
1919
In your library add the following import:
@@ -52,7 +52,7 @@ class ExampleApp extends StatelessWidget {
5252

5353
### DisplayMetricsData
5454

55-
To access DisplayMetricsData you can call
55+
To access `DisplayMetricsData` you can call
5656
```dart
5757
final metrics = DisplayMetrics.maybeOf(context);
5858
// or
@@ -80,6 +80,8 @@ metrics.inchesToLogicalPixelRatio => double
8080
metrics.devicePixelRatio => double
8181
```
8282

83+
You can await for `DisplayMetrics.ensureInitialized(context)` to ensure that `DisplayMetricsData` is available.
84+
8385
### Convert units
8486
To convert inches and millimeters into Flutter logical pixels and vice versa you can call one of BuildContext extension methods:
8587
```dart

display_metrics/coverage/lcov.info

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SF:lib\src\data.dart
1+
SF:lib/src/data.dart
22
DA:9,6
33
DA:22,2
44
DA:23,10
@@ -14,9 +14,9 @@ DA:49,1
1414
DA:50,2
1515
DA:51,2
1616
DA:55,3
17-
DA:58,2
18-
DA:59,6
19-
DA:60,6
17+
DA:58,3
18+
DA:59,9
19+
DA:60,9
2020
DA:61,3
2121
DA:62,3
2222
DA:64,1
@@ -29,44 +29,83 @@ DA:71,4
2929
LF:27
3030
LH:27
3131
end_of_record
32-
SF:lib\src\display_metrics.dart
33-
DA:7,3
34-
DA:17,3
35-
DA:18,6
36-
DA:22,3
37-
DA:23,3
38-
DA:24,3
39-
DA:25,3
40-
DA:26,3
41-
DA:30,2
32+
SF:lib/src/display_metrics.dart
33+
DA:9,3
34+
DA:22,2
35+
DA:24,2
36+
DA:25,2
37+
DA:26,2
38+
DA:27,2
39+
DA:31,3
4240
DA:32,6
43-
LF:10
44-
LH:10
41+
DA:36,3
42+
DA:37,3
43+
DA:38,3
44+
DA:39,3
45+
DA:40,3
46+
DA:44,2
47+
DA:46,6
48+
LF:15
49+
LH:15
4550
end_of_record
46-
SF:lib\src\display_metrics_widget.dart
47-
DA:11,1
48-
DA:23,1
49-
DA:24,1
50-
DA:30,1
51-
DA:32,1
51+
SF:lib/src/display_metrics_widget.dart
52+
DA:11,2
53+
DA:23,2
54+
DA:24,2
5255
DA:33,2
53-
DA:35,1
54-
DA:37,2
55-
DA:38,5
56-
DA:42,1
57-
DA:46,2
58-
DA:47,2
56+
DA:35,2
57+
DA:36,4
58+
DA:37,6
59+
DA:40,2
60+
DA:42,2
61+
DA:44,4
62+
DA:46,8
5963
DA:49,1
60-
DA:50,1
61-
DA:51,1
62-
DA:56,1
63-
DA:58,1
64-
DA:59,1
65-
DA:60,2
66-
LF:19
67-
LH:19
64+
DA:53,2
65+
DA:54,2
66+
DA:55,4
67+
DA:59,2
68+
DA:60,3
69+
DA:61,2
70+
DA:65,1
71+
DA:66,1
72+
DA:67,1
73+
DA:68,1
74+
DA:69,1
75+
DA:73,2
76+
DA:78,2
77+
DA:81,1
78+
DA:82,1
79+
DA:83,1
80+
DA:88,2
81+
DA:90,6
82+
DA:92,4
83+
DA:94,4
84+
DA:96,2
85+
DA:100,1
86+
DA:104,1
87+
DA:109,1
88+
DA:111,1
89+
DA:112,3
90+
DA:114,0
91+
DA:118,2
92+
DA:122,4
93+
DA:123,4
94+
DA:125,2
95+
DA:126,2
96+
DA:127,2
97+
DA:132,1
98+
DA:133,2
99+
DA:134,2
100+
DA:137,2
101+
DA:139,2
102+
DA:140,2
103+
DA:141,2
104+
DA:142,4
105+
LF:53
106+
LH:52
68107
end_of_record
69-
SF:lib\src\extension.dart
108+
SF:lib/src/extension.dart
70109
DA:7,1
71110
DA:8,2
72111
DA:9,1
@@ -83,11 +122,11 @@ DA:33,1
83122
DA:34,2
84123
DA:37,1
85124
DA:41,2
86-
DA:48,2
87-
DA:51,1
88-
DA:52,3
89-
DA:53,1
90-
DA:54,3
125+
DA:48,3
126+
DA:51,3
127+
DA:52,6
128+
DA:53,3
129+
DA:54,9
91130
LF:21
92131
LH:21
93132
end_of_record

display_metrics/example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- display_metrics_ios (1.0.0):
2+
- display_metrics_ios (1.0.3):
33
- Flutter
44
- Flutter (1.0.0)
55

@@ -14,9 +14,9 @@ EXTERNAL SOURCES:
1414
:path: Flutter
1515

1616
SPEC CHECKSUMS:
17-
display_metrics_ios: 73e907f9d5c3eaa2452cd01e8df878b0e9f9e79d
17+
display_metrics_ios: 8cb6c613d3cda282e41488a7139398a1675f27da
1818
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
1919

2020
PODFILE CHECKSUM: 10b75cae8b0e4f0acb86c1bf5bf0f50a9a144c71
2121

22-
COCOAPODS: 1.15.2
22+
COCOAPODS: 1.16.2

display_metrics/example/lib/screens/metrics.dart

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
11
import 'package:display_metrics/display_metrics.dart';
22
import '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
),

display_metrics/lib/src/display_metrics.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/widgets.dart';
24
import 'package:display_metrics/display_metrics.dart';
35

@@ -7,18 +9,30 @@ class DisplayMetrics extends InheritedWidget {
79
const DisplayMetrics({
810
required this.data,
911
required super.child,
12+
Future<DisplayMetricsData>? loadFuture,
1013
super.key,
11-
});
14+
}) : _loadFuture = loadFuture;
1215

1316
/// data that contains display metrics
1417
final DisplayMetricsData? data;
1518

16-
/// method to get DisplayMetricsData
19+
final Future<DisplayMetricsData>? _loadFuture;
20+
21+
/// method to ensure [DisplayMetricsData] has been loaded
22+
static Future<DisplayMetricsData>? ensureInitialized(BuildContext context) {
23+
final metrics =
24+
context.dependOnInheritedWidgetOfExactType<DisplayMetrics>();
25+
final data = metrics?.data;
26+
if (data != null) return Future.value(data);
27+
return metrics?._loadFuture;
28+
}
29+
30+
/// method to get [DisplayMetricsData]
1731
static DisplayMetricsData? maybeOf(BuildContext context) {
1832
return context.dependOnInheritedWidgetOfExactType<DisplayMetrics>()?.data;
1933
}
2034

21-
/// method to get DisplayMetricsData
35+
/// method to get [DisplayMetricsData]
2236
static DisplayMetricsData of(BuildContext context) {
2337
final widget = context.dependOnInheritedWidgetOfExactType<DisplayMetrics>();
2438
assert(widget != null, 'No DisplayMetrics found in context');

0 commit comments

Comments
 (0)