streamBarcode static method

Stream<String> streamBarcode(
  1. BuildContext context, {
  2. String lineColor = "#ff6666",
  3. String cancelButtonText = "Cancel",
  4. bool isShowFlashIcon = false,
  5. ScanType scanType = ScanType.barcode,
  6. CameraFace cameraFace = CameraFace.back,
  7. BarcodeAppBar? barcodeAppBar,
  8. int? delayMillis,
  9. ScanFormat scanFormat = ScanFormat.ALL_FORMATS,
  10. Widget? child,
})

Continuously scans barcodes and emits results through a stream.

This method will continue scanning and emitting barcode values until the scanner is closed or manually stopped.

Parameters:

  • context: The BuildContext required for navigation.
  • lineColor: The color of the scanning line (default: "#ff6666").
  • cancelButtonText: Text for the cancel button (default: "Cancel").
  • isShowFlashIcon: Whether to show the flash toggle icon (default: false).
  • scanType: The type of barcode to scan (default: ScanType.barcode).
  • cameraFace: Which camera to use (default: CameraFace.back).
  • barcodeAppBar: Custom app bar configuration.
  • delayMillis: Delay in milliseconds between scans.
  • child: Optional widget to display in the scanner interface.

Returns a Stream<String> that emits scanned barcode values. The stream is closed when the scanner is closed.

Implementation

static Stream<String> streamBarcode(
  BuildContext context, {
  String lineColor = "#ff6666",
  String cancelButtonText = "Cancel",
  bool isShowFlashIcon = false,
  ScanType scanType = ScanType.barcode,
  CameraFace cameraFace = CameraFace.back,
  BarcodeAppBar? barcodeAppBar,
  int? delayMillis,
  ScanFormat scanFormat = ScanFormat.ALL_FORMATS,
  Widget? child,
}) {
  final streamController = StreamController<String>();
  bool isPopped = false;

  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => BarcodeScanner(
        lineColor: lineColor,
        cancelButtonText: cancelButtonText,
        isShowFlashIcon: isShowFlashIcon,
        scanType: scanType,
        cameraFace: cameraFace,
        barcodeAppBar: barcodeAppBar,
        delayMillis: delayMillis,
        scanFormat: scanFormat,
        onScanned: (res) {
          streamController.add(res);
          // Don't pop the navigator - keep scanning
        },
        onClose: () {
          if (!isPopped) {
            isPopped = true;
            streamController.close();
            Navigator.pop(context);
          }
        },
        child: child,
      ),
    ),
  );

  return streamController.stream;
}