Navaron Bracke

fix the pageview sample

  1 +import 'dart:async';
  2 +
1 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
2 import 'package:mobile_scanner/mobile_scanner.dart'; 4 import 'package:mobile_scanner/mobile_scanner.dart';
3 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 5 import 'package:mobile_scanner_example/scanner_error_widget.dart';
@@ -9,27 +11,72 @@ class BarcodeScannerPageView extends StatefulWidget { @@ -9,27 +11,72 @@ class BarcodeScannerPageView extends StatefulWidget {
9 State<BarcodeScannerPageView> createState() => _BarcodeScannerPageViewState(); 11 State<BarcodeScannerPageView> createState() => _BarcodeScannerPageViewState();
10 } 12 }
11 13
12 -class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView>  
13 - with SingleTickerProviderStateMixin {  
14 - BarcodeCapture? capture; 14 +class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView> {
  15 + final MobileScannerController scannerController = MobileScannerController();
  16 +
  17 + final PageController pageController = PageController();
  18 +
  19 + @override
  20 + void initState() {
  21 + super.initState();
  22 + scannerController.start();
  23 + }
  24 +
  25 + @override
  26 + Widget build(BuildContext context) {
  27 + return Scaffold(
  28 + appBar: AppBar(title: const Text('With PageView')),
  29 + backgroundColor: Colors.black,
  30 + body: PageView(
  31 + controller: pageController,
  32 + onPageChanged: (index) async {
  33 + // Stop the camera view for the current page,
  34 + // and then restart the camera for the new page.
  35 + await scannerController.stop();
  36 +
  37 + // When switching pages, add a delay to the next start call.
  38 + // Otherwise the camera will start before the next page is displayed.
  39 + await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
  40 +
  41 + if (!mounted) {
  42 + return;
  43 + }
  44 +
  45 + scannerController.start();
  46 + },
  47 + children: [
  48 + _BarcodeScannerPage(controller: scannerController),
  49 + const SizedBox(),
  50 + _BarcodeScannerPage(controller: scannerController),
  51 + _BarcodeScannerPage(controller: scannerController),
  52 + ],
  53 + ),
  54 + );
  55 + }
  56 +
  57 + @override
  58 + Future<void> dispose() async {
  59 + await scannerController.dispose();
  60 + pageController.dispose();
  61 + super.dispose();
  62 + }
  63 +}
  64 +
  65 +class _BarcodeScannerPage extends StatelessWidget {
  66 + const _BarcodeScannerPage({required this.controller});
  67 +
  68 + final MobileScannerController controller;
15 69
16 - Widget cameraView() {  
17 - return Builder(  
18 - builder: (context) { 70 + @override
  71 + Widget build(BuildContext context) {
19 return Stack( 72 return Stack(
20 children: [ 73 children: [
21 MobileScanner( 74 MobileScanner(
22 - startDelay: true,  
23 - controller: MobileScannerController(torchEnabled: true), 75 + controller: controller,
24 fit: BoxFit.contain, 76 fit: BoxFit.contain,
25 errorBuilder: (context, error, child) { 77 errorBuilder: (context, error, child) {
26 return ScannerErrorWidget(error: error); 78 return ScannerErrorWidget(error: error);
27 }, 79 },
28 - onDetect: (capture) {  
29 - setState(() {  
30 - this.capture = capture;  
31 - });  
32 - },  
33 ), 80 ),
34 Align( 81 Align(
35 alignment: Alignment.bottomCenter, 82 alignment: Alignment.bottomCenter,
@@ -37,49 +84,30 @@ class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView> @@ -37,49 +84,30 @@ class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView>
37 alignment: Alignment.bottomCenter, 84 alignment: Alignment.bottomCenter,
38 height: 100, 85 height: 100,
39 color: Colors.black.withOpacity(0.4), 86 color: Colors.black.withOpacity(0.4),
40 - child: Row(  
41 - mainAxisAlignment: MainAxisAlignment.spaceEvenly,  
42 - children: [  
43 - Center(  
44 - child: SizedBox(  
45 - width: MediaQuery.of(context).size.width - 120,  
46 - height: 50,  
47 - child: FittedBox(  
48 - child: Text(  
49 - capture?.barcodes.first.rawValue ??  
50 - 'Scan something!', 87 + child: Center(
  88 + child: StreamBuilder<BarcodeCapture>(
  89 + stream: controller.barcodes,
  90 + builder: (context, snapshot) {
  91 + final barcodes = snapshot.data?.barcodes;
  92 +
  93 + if (barcodes == null || barcodes.isEmpty) {
  94 + return const Text(
  95 + 'Scan Something!',
  96 + style: TextStyle(color: Colors.white, fontSize: 20),
  97 + );
  98 + }
  99 +
  100 + return Text(
  101 + barcodes.first.rawValue ?? 'No raw value',
51 overflow: TextOverflow.fade, 102 overflow: TextOverflow.fade,
52 - style: Theme.of(context)  
53 - .textTheme  
54 - .headlineMedium!  
55 - .copyWith(color: Colors.white),  
56 - ),  
57 - ),  
58 - ), 103 + style: const TextStyle(color: Colors.white),
  104 + );
  105 + },
59 ), 106 ),
60 - ],  
61 ), 107 ),
62 ), 108 ),
63 ), 109 ),
64 ], 110 ],
65 ); 111 );
66 - },  
67 - );  
68 - }  
69 -  
70 - @override  
71 - Widget build(BuildContext context) {  
72 - return Scaffold(  
73 - appBar: AppBar(title: const Text('With PageView')),  
74 - backgroundColor: Colors.black,  
75 - body: PageView(  
76 - children: [  
77 - cameraView(),  
78 - Container(),  
79 - cameraView(),  
80 - cameraView(),  
81 - ],  
82 - ),  
83 - );  
84 } 112 }
85 } 113 }