mobile_scanner_preview.dart
3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/src/objects/preview_details.dart';
import 'package:native_device_orientation/native_device_orientation.dart';
import 'package:sensors_plus/sensors_plus.dart';
class Preview extends StatefulWidget {
final double width, height;
final double targetWidth, targetHeight;
final int? textureId;
final int? sensorOrientation;
final BoxFit fit;
Preview({
Key? key,
required PreviewDetails previewDetails,
required this.targetWidth,
required this.targetHeight,
required this.fit,
}) : textureId = previewDetails.textureId,
width = previewDetails.width!.toDouble(),
height = previewDetails.height!.toDouble(),
sensorOrientation = previewDetails.sensorOrientation as int?,
super(key: key);
@override
State<Preview> createState() => _PreviewState();
}
class _PreviewState extends State<Preview> {
final _streamSubscriptions = <StreamSubscription<dynamic>>[];
bool landscapeLeft = false;
@override
void initState() {
super.initState();
_streamSubscriptions.add(
magnetometerEvents.listen(
(MagnetometerEvent event) {
if (event.x <= 0) {
landscapeLeft = true;
} else {
landscapeLeft = false;
}
},
),
);
}
@override
void dispose() {
super.dispose();
for (final subscription in _streamSubscriptions) {
subscription.cancel();
}
}
int _getRotationCompensation(NativeDeviceOrientation nativeOrientation) {
int nativeRotation = 0;
switch (nativeOrientation) {
case NativeDeviceOrientation.portraitUp:
nativeRotation = 0;
break;
case NativeDeviceOrientation.landscapeRight:
nativeRotation = 90;
break;
case NativeDeviceOrientation.portraitDown:
nativeRotation = 180;
break;
case NativeDeviceOrientation.landscapeLeft:
nativeRotation = 270;
break;
case NativeDeviceOrientation.unknown:
default:
break;
}
return ((nativeRotation - widget.sensorOrientation! + 450) % 360) ~/ 90;
}
@override
Widget build(BuildContext context) {
final orientation = MediaQuery.of(context).orientation;
double frameHeight = widget.width;
double frameWidth = widget.height;
return ClipRect(
child: FittedBox(
fit: widget.fit,
child: RotatedBox(
quarterTurns: orientation == Orientation.landscape ? landscapeLeft ? 1 : 3 : 0,
child: SizedBox(
width: frameWidth,
height: frameHeight,
child: Texture(textureId: widget.textureId!),
),
),
),
);
return NativeDeviceOrientationReader(
builder: (context) {
var nativeOrientation =
NativeDeviceOrientationReader.orientation(context);
double frameHeight = widget.width;
double frameWidth = widget.height;
return ClipRect(
child: FittedBox(
fit: widget.fit,
child: RotatedBox(
quarterTurns: _getRotationCompensation(nativeOrientation),
child: SizedBox(
width: frameWidth,
height: frameHeight,
child: Texture(textureId: widget.textureId!),
),
),
),
);
},
);
}
}