@@ -184,7 +184,7 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
184184 /// The camera listener.
185185 CodeScannerCameraListener ? listener;
186186
187- /// Whether to retry.
187+ /// Whether to retry for when camera permission is denied .
188188 bool retry = true ;
189189
190190 /// Whether it's initialized.
@@ -209,21 +209,13 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
209209 @override
210210 void didChangeAppLifecycleState (AppLifecycleState state) {
211211 super .didChangeAppLifecycleState (state);
212- if (! isInternalController) {
212+ if (! isInternalController || controller == null || ! controller ! .value.isInitialized ) {
213213 return ;
214214 }
215215
216216 if (state == AppLifecycleState .inactive) {
217- CameraController ? cameraController = controller;
218- if (cameraController == null || ! cameraController.value.isInitialized) {
219- return ;
220- }
221- cameraController.dispose ();
222- if (mounted) {
223- setState (() => controller = null );
224- }
225- } else if (state == AppLifecycleState .resumed && retry && initialized && controller == null ) {
226- initialized = false ;
217+ controller! .dispose ();
218+ } else if (state == AppLifecycleState .resumed) {
227219 _initCameraController ();
228220 }
229221 }
@@ -267,11 +259,26 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
267259 }
268260 listener = CodeScannerCameraListener (
269261 this .controller! ,
270- onScan: widget.onScan,
271- onScanAll: widget.onScanAll,
262+ onScan: (code, details, listener) async {
263+ widget.onScan? .call (code, details, listener);
264+ if (widget.once) {
265+ setState (() {
266+ this .controller = null ;
267+ listener.stop ();
268+ });
269+ }
270+ },
271+ onScanAll: (barcodes, listener) async {
272+ widget.onScanAll? .call (barcodes, listener);
273+ if (widget.once) {
274+ setState (() {
275+ this .controller = null ;
276+ listener.stop ();
277+ });
278+ }
279+ },
272280 formats: widget.formats,
273281 interval: widget.scanInterval,
274- once: widget.once,
275282 );
276283
277284 initialized = true ;
@@ -360,9 +367,6 @@ class CodeScannerCameraListener {
360367 /// The scanner instance.
361368 final BarcodeScanner scanner;
362369
363- /// Whether there is only one asked scan.
364- final bool once;
365-
366370 /// Called when a scan occurs.
367371 final void Function (String ? code, Barcode details, CodeScannerCameraListener listener)? onScan;
368372
@@ -380,7 +384,6 @@ class CodeScannerCameraListener {
380384 this .controller, {
381385 List <BarcodeFormat > formats = const [BarcodeFormat .all],
382386 Duration interval = const Duration (milliseconds: 500 ),
383- this .once = false ,
384387 this .onScan,
385388 this .onScanAll,
386389 this .onError,
@@ -454,9 +457,6 @@ class CodeScannerCameraListener {
454457 if (! controller.value.isStreamingImages || barcodes.isEmpty) {
455458 return ;
456459 }
457- if (once) {
458- await stop ();
459- }
460460 onScan? .call (barcodes.first.rawValue, barcodes.first, this );
461461 onScanAll? .call (barcodes, this );
462462 }
0 commit comments