barcode_scanner_pageview.dart
2.67 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
import 'dart:async';
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_error_widget.dart';
class BarcodeScannerPageView extends StatefulWidget {
const BarcodeScannerPageView({super.key});
@override
State<BarcodeScannerPageView> createState() => _BarcodeScannerPageViewState();
}
class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView> {
final MobileScannerController controller =
MobileScannerController(autoStart: false);
final PageController pageController = PageController();
@override
void initState() {
super.initState();
unawaited(controller.start());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('With PageView')),
backgroundColor: Colors.black,
body: PageView(
controller: pageController,
onPageChanged: (index) async {
// Stop the camera view for the current page,
// and then restart the camera for the new page.
await controller.stop();
// When switching pages, add a delay to the next start call.
// Otherwise the camera will start before the next page is displayed.
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
if (!mounted) {
return;
}
unawaited(controller.start());
},
children: [
_BarcodeScannerPage(controller: controller),
const SizedBox(),
_BarcodeScannerPage(controller: controller),
_BarcodeScannerPage(controller: controller),
],
),
);
}
@override
Future<void> dispose() async {
pageController.dispose();
super.dispose();
await controller.dispose();
}
}
class _BarcodeScannerPage extends StatelessWidget {
const _BarcodeScannerPage({required this.controller});
final MobileScannerController controller;
@override
Widget build(BuildContext context) {
return 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: Center(
child: ScannedBarcodeLabel(barcodes: controller.barcodes),
),
),
),
],
);
}
}