mobile_scanner icon indicating copy to clipboard operation
mobile_scanner copied to clipboard

Scan window doesn't work when using BoxFit.cover

Open enricocaliolo opened this issue 10 months ago • 1 comments

Hi, I can't seem to make scan window work when I use BoxFit.cover instead of BoxFit.contain. I think it is related to how I am painting the overlay scan, but dunno what to do.

Scaffold(
      appBar: AppBar(
        title: const Text('Escaneie o QR Code!'),
        backgroundColor: strongBlueColor(),
      ),
      backgroundColor: Colors.black,
      body: LayoutBuilder(builder: (context, constraints) {
        final scanWindow = Rect.fromCenter(
            center: Offset(constraints.maxWidth / 2, constraints.maxHeight / 2),
            width: 300,
            height: 300);

        return Stack(
          fit: StackFit.expand,
          children: [
            MobileScanner(
              onDetect: (capture) => _onDetected(capture, data_controller),
              controller: controller,
              fit: BoxFit.cover,
              scanWindow: scanWindow,
            ),
            if (started)
              CustomPaint(
                painter: ScannerOverlay(scanWindow),
              )
          ],
        );
      }),
    );

class ScannerOverlay extends CustomPainter {
  ScannerOverlay(this.scanWindow);

  final Rect scanWindow;

  @override
  void paint(Canvas canvas, Size size) {
    final backgroundPath = Path()..addRect(Rect.largest);
    final cutoutPath = Path()..addRect(scanWindow);

    final backgroundPaint = Paint()
      ..color = Colors.black.withOpacity(0.5)
      ..style = PaintingStyle.fill
      ..blendMode = BlendMode.dstOut;

    final backgroundWithCutout = Path.combine(
      PathOperation.difference,
      backgroundPath,
      cutoutPath,
    );
    canvas.drawPath(backgroundWithCutout, backgroundPaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

enricocaliolo avatar Apr 11 '24 21:04 enricocaliolo