juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
890 stars 515 forks source link

Stream BarcodeCapture #1060

Open minhkwan opened 6 months ago

minhkwan commented 6 months ago

Hello, I'm trying to implement a start / stop function scanning. When I press the button, it will start scanning, and when it found a barcode, read from it, a modal will popup and scanner will be stopped

import 'dart:async';

import 'package:flutter/material.dart'; import 'package:flutter_application/lib.dart'; import 'package:mobile_scanner/mobile_scanner.dart';

class SignUpScanQRPage extends StatefulWidget with WidgetsBindingObserver { static const String routeName = 'LoginPage'; SignUpScanQRPage({super.key});

@override State createState() => _SignUpScanQRPageState(); }

class _SignUpScanQRPageState extends State { StreamSubscription? streamSubscriptionScanner;

final qrScanController = MobileScannerController();

void startListenToScannerCapture() { streamSubscriptionScanner = qrScanController.barcodes.listen(scannerlistener); }

void scannerlistener(BarcodeCapture barcodeCapture) async { print('START TO RUN'); print(barcodeCapture); streamSubscriptionScanner?.cancel(); }

@override Widget build(BuildContext context) { return BackgroundOverlay( child: Scaffold( backgroundColor: Colors.transparent, body: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ AppConstants.kSpacingItem70, const Text('A'), AppConstants.kSpacingItem75, Expanded( child: MobileScanner( placeholderBuilder: (p0, p1) { return Container( height: 20, width: 20, color: Colors.red, ); }, overlayBuilder: (context, constraints) { return Container( constraints: constraints, decoration: BoxDecoration( color: Colors.transparent, border: Border.all(color: Colors.blue)), ); }, errorBuilder: (p0, p1, p2) { return Container(); }, ), ), CommonButton( btnText: 'Scan', onPress: () { startListenToScannerCapture(); }, ) ], ), )); } }

I dont know why the PRINT won't ever run. Please help me, tks!

navaronbracke commented 6 months ago

What version of mobile_scanner are you using? It seems that the controller is not starting, so the StreamSubscription never gets any events.