AmolGangadhare / flutter_barcode_scanner

Barcode scanner plugin for flutter. Supports barcode scanning for Android and iOS
https://pub.dev/packages/flutter_barcode_scanner
MIT License
379 stars 451 forks source link

Close barcode scanner whilst streaming. #30

Open mrdavidrees opened 5 years ago

mrdavidrees commented 5 years ago

Is your feature request related to a problem? Please describe. It's not possible to close the barcode scanner once you are happy with the result from the screen.

Describe the solution you'd like A method that will tell the barcode scanner to close

Describe alternatives you've considered FlutterBarcodeScanner.getBarcodeStreamReceiver("#000000", "Cancel", true).listen((d) => print(d)); await Future.delayed(Duration(seconds: 1)); Navigator.pop(context);

Additional context When I call navigator.pop it closed the screen behind the barcode scanner. It would be great if I could call FlutterBarcodeScanner.closeBarcodeScanner() once i have processed an item from the screen and verified it.

devibrahimkarahan commented 4 years ago

+, need that

hazzo commented 4 years ago

+1

john-yick commented 4 years ago

+1

Xgamefactory commented 4 years ago

any updates ?

vglider commented 4 years ago

this would be awesome also because it won't work the normal scan, only streamscan works

fernandocerkal commented 2 years ago

+1

java.lang.ClassCastException: com.myapp.MainActivity cannot be cast to io.flutter.app.FlutterActivity

Dandix3 commented 1 year ago

+1

mohamedali170 commented 1 year ago

+1

HenriqueHartmann commented 1 year ago

Hi guys, I was facing the same problem but I managed using Method Channel to emit an event to my MainActivity (responsible for running Flutter) that executes an Intent with the flag: FLAG_ACTIVITY_CLEAR_TOP, it's a flag that cleans every activity above the MainActivity, aka Base Activity.

Method Channel (Flutter):

Future<void> finishBarcodeReaderActivity() async {
    const platform = MethodChannel('com.example/channel');

    try {
      await platform.invokeMethod('finishBarcodeReaderActivity');
    } on PlatformException catch (e) {
      log('ERROR: exitRunningActivity => Platform Error', error: e);
    } catch (e) {
      log('ERROR: exitRunningActivity => Unknown Error', error: e);
    }
  }

Stream (Flutter):

void startBarcodeScanStream() {
  late Stream<dynamic>? barcodeStream;
  late StreamSubscription? barcodeSubscription;

  barcodeStream = FlutterBarcodeScanner.getBarcodeStreamReceiver(
    '#ff6666',
    'Fechar Leitor',
    false,
    ScanMode.DEFAULT,
  );

  barcodeSubscription = barcodeStream!.listen(
    (value) async {
      try {
        Your logic ...
      } catch (e) {
        barcodeSubscription!.cancel();
        finishBarcodeReaderActivity();
      }
    },
  );
}

MainActivity (Kotlin):

package com.example

import android.content.Context
import android.content.Intent
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    fun finishSpecificRunningActivity(context: Context) {
        val intent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        context.startActivity(intent)
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            "com.example/channel"
        ).setMethodCallHandler { call, result ->
            if (call.method == "finishBarcodeReaderActivity") {
                    finishSpecificRunningActivity(this)
                result.success(null)
            } else {
                result.notImplemented()
            }
        }
    }
}

OBS: My MainActivity is in Kotlin, but yours could be in Java, you just need to translate.

HenriqueHartmann commented 1 year ago

Is your feature request related to a problem? Please describe. It's not possible to close the barcode scanner once you are happy with the result from the screen.

Describe the solution you'd like A method that will tell the barcode scanner to close

Describe alternatives you've considered FlutterBarcodeScanner.getBarcodeStreamReceiver("#000000", "Cancel", true).listen((d) => print(d)); await Future.delayed(Duration(seconds: 1)); Navigator.pop(context);

Additional context When I call navigator.pop it closed the screen behind the barcode scanner. It would be great if I could call FlutterBarcodeScanner.closeBarcodeScanner() once i have processed an item from the screen and verified it.

It happened with me too, but that happen because you're working with different activities, if you check the ActivityManager and check your running tasks in the MainAcitvity, it's possible to see two activities yours and the Barcode Scanner. So when you called the Navigator.pop(context) you're working under the MainActivity and not on the Barcode Scanner.

raneen-erpmax commented 8 months ago

any updates??

basicFlutter commented 2 months ago

any updates??