barcode_scanner_zoom.dart
3.68 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
129
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanned_barcode_label.dart';
import 'package:mobile_scanner_example/scanner_button_widgets.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
class BarcodeScannerWithZoom extends StatefulWidget {
const BarcodeScannerWithZoom({super.key});
@override
State<BarcodeScannerWithZoom> createState() => _BarcodeScannerWithZoomState();
}
class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
final MobileScannerController controller = MobileScannerController(
torchEnabled: true,
);
double _zoomFactor = 0.0;
@override
void initState() {
super.initState();
controller.start();
}
Widget _buildZoomScaleSlider() {
return ValueListenableBuilder(
valueListenable: controller,
builder: (context, state, child) {
if (!state.isInitialized || !state.isRunning) {
return const SizedBox.shrink();
}
final TextStyle labelStyle = Theme.of(context)
.textTheme
.headlineMedium!
.copyWith(color: Colors.white);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
children: [
Text(
'0%',
overflow: TextOverflow.fade,
style: labelStyle,
),
Expanded(
child: Slider(
value: _zoomFactor,
onChanged: (value) {
setState(() {
_zoomFactor = value;
controller.setZoomScale(value);
});
},
),
),
Text(
'100%',
overflow: TextOverflow.fade,
style: labelStyle,
),
],
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('With zoom slider')),
backgroundColor: Colors.black,
body: Stack(
children: [
MobileScanner(
controller: controller,
fit: BoxFit.contain,
errorBuilder: (context, error, child) {
return ScannerErrorWidget(error: error);
},
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
alignment: Alignment.bottomCenter,
height: 100,
color: Colors.black.withOpacity(0.4),
child: Column(
children: [
if (!kIsWeb) _buildZoomScaleSlider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ToggleFlashlightButton(controller: controller),
StartStopMobileScannerButton(controller: controller),
Expanded(
child: Center(
child: ScannedBarcodeLabel(
barcodes: controller.barcodes,
),
),
),
SwitchCameraButton(controller: controller),
AnalyzeImageFromGalleryButton(controller: controller),
],
),
],
),
),
),
],
),
);
}
@override
Future<void> dispose() async {
super.dispose();
await controller.dispose();
}
}