Showing
1 changed file
with
88 additions
and
60 deletions
| 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,62 +11,15 @@ class BarcodeScannerPageView extends StatefulWidget { | @@ -9,62 +11,15 @@ 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; | ||
| 15 | - | ||
| 16 | - Widget cameraView() { | ||
| 17 | - return Builder( | ||
| 18 | - builder: (context) { | ||
| 19 | - return Stack( | ||
| 20 | - children: [ | ||
| 21 | - MobileScanner( | ||
| 22 | - startDelay: true, | ||
| 23 | - controller: MobileScannerController(torchEnabled: true), | ||
| 24 | - fit: BoxFit.contain, | ||
| 25 | - errorBuilder: (context, error, child) { | ||
| 26 | - return ScannerErrorWidget(error: error); | ||
| 27 | - }, | ||
| 28 | - onDetect: (capture) { | ||
| 29 | - setState(() { | ||
| 30 | - this.capture = capture; | ||
| 31 | - }); | ||
| 32 | - }, | ||
| 33 | - ), | ||
| 34 | - Align( | ||
| 35 | - alignment: Alignment.bottomCenter, | ||
| 36 | - child: Container( | ||
| 37 | - alignment: Alignment.bottomCenter, | ||
| 38 | - height: 100, | ||
| 39 | - 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!', | ||
| 51 | - overflow: TextOverflow.fade, | ||
| 52 | - style: Theme.of(context) | ||
| 53 | - .textTheme | ||
| 54 | - .headlineMedium! | ||
| 55 | - .copyWith(color: Colors.white), | ||
| 56 | - ), | ||
| 57 | - ), | ||
| 58 | - ), | ||
| 59 | - ), | ||
| 60 | - ], | ||
| 61 | - ), | ||
| 62 | - ), | ||
| 63 | - ), | ||
| 64 | - ], | ||
| 65 | - ); | ||
| 66 | - }, | ||
| 67 | - ); | 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(); | ||
| 68 | } | 23 | } |
| 69 | 24 | ||
| 70 | @override | 25 | @override |
| @@ -73,13 +28,86 @@ class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView> | @@ -73,13 +28,86 @@ class _BarcodeScannerPageViewState extends State<BarcodeScannerPageView> | ||
| 73 | appBar: AppBar(title: const Text('With PageView')), | 28 | appBar: AppBar(title: const Text('With PageView')), |
| 74 | backgroundColor: Colors.black, | 29 | backgroundColor: Colors.black, |
| 75 | body: PageView( | 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 | + }, | ||
| 76 | children: [ | 47 | children: [ |
| 77 | - cameraView(), | ||
| 78 | - Container(), | ||
| 79 | - cameraView(), | ||
| 80 | - cameraView(), | 48 | + _BarcodeScannerPage(controller: scannerController), |
| 49 | + const SizedBox(), | ||
| 50 | + _BarcodeScannerPage(controller: scannerController), | ||
| 51 | + _BarcodeScannerPage(controller: scannerController), | ||
| 81 | ], | 52 | ], |
| 82 | ), | 53 | ), |
| 83 | ); | 54 | ); |
| 84 | } | 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; | ||
| 69 | + | ||
| 70 | + @override | ||
| 71 | + Widget build(BuildContext context) { | ||
| 72 | + return Stack( | ||
| 73 | + children: [ | ||
| 74 | + MobileScanner( | ||
| 75 | + controller: controller, | ||
| 76 | + fit: BoxFit.contain, | ||
| 77 | + errorBuilder: (context, error, child) { | ||
| 78 | + return ScannerErrorWidget(error: error); | ||
| 79 | + }, | ||
| 80 | + ), | ||
| 81 | + Align( | ||
| 82 | + alignment: Alignment.bottomCenter, | ||
| 83 | + child: Container( | ||
| 84 | + alignment: Alignment.bottomCenter, | ||
| 85 | + height: 100, | ||
| 86 | + color: Colors.black.withOpacity(0.4), | ||
| 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', | ||
| 102 | + overflow: TextOverflow.fade, | ||
| 103 | + style: const TextStyle(color: Colors.white), | ||
| 104 | + ); | ||
| 105 | + }, | ||
| 106 | + ), | ||
| 107 | + ), | ||
| 108 | + ), | ||
| 109 | + ), | ||
| 110 | + ], | ||
| 111 | + ); | ||
| 112 | + } | ||
| 85 | } | 113 | } |
-
Please register or login to post a comment