-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic ui elements for barcode overlay and scanner overlay, …
…update scanWindow via MobileScanner widget
- Loading branch information
1 parent
be47302
commit b7d6599
Showing
8 changed files
with
219 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:mobile_scanner/mobile_scanner.dart'; | ||
|
||
class BarcodeOverlay extends StatelessWidget { | ||
const BarcodeOverlay({ | ||
super.key, | ||
required this.controller, | ||
required this.boxFit, | ||
}); | ||
|
||
final MobileScannerController controller; | ||
final BoxFit boxFit; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ValueListenableBuilder( | ||
valueListenable: controller, | ||
builder: (context, value, child) { | ||
// Not ready. | ||
if (!value.isInitialized || !value.isRunning || value.error != null) { | ||
return const SizedBox(); | ||
} | ||
|
||
return StreamBuilder<BarcodeCapture>( | ||
stream: controller.barcodes, | ||
builder: (context, snapshot) { | ||
final BarcodeCapture? barcodeCapture = snapshot.data; | ||
|
||
// No barcode. | ||
if (barcodeCapture == null || barcodeCapture.barcodes.isEmpty) { | ||
return const SizedBox(); | ||
} | ||
|
||
final overlays = <Widget>[]; | ||
|
||
for (final scannedBarcode in barcodeCapture.barcodes) { | ||
// No barcode corners, or size, or no camera preview size. | ||
if (value.size.isEmpty || | ||
scannedBarcode.size.isEmpty || | ||
scannedBarcode.corners.isEmpty) { | ||
continue; | ||
} | ||
|
||
overlays.add( | ||
CustomPaint( | ||
painter: BarcodePainter( | ||
barcodeCorners: scannedBarcode.corners, | ||
barcodeSize: scannedBarcode.size, | ||
boxFit: boxFit, | ||
cameraPreviewSize: barcodeCapture.size, | ||
), | ||
), | ||
); | ||
} | ||
|
||
return Stack( | ||
fit: StackFit.expand, | ||
children: overlays, | ||
); | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class BarcodePainter extends CustomPainter { | ||
BarcodePainter({ | ||
required this.barcodeCorners, | ||
required this.barcodeSize, | ||
required this.boxFit, | ||
required this.cameraPreviewSize, | ||
}); | ||
|
||
final List<Offset> barcodeCorners; | ||
final Size barcodeSize; | ||
final BoxFit boxFit; | ||
final Size cameraPreviewSize; | ||
|
||
@override | ||
void paint(Canvas canvas, Size size) { | ||
if (barcodeCorners.isEmpty || | ||
barcodeSize.isEmpty || | ||
cameraPreviewSize.isEmpty) { | ||
return; | ||
} | ||
|
||
final adjustedSize = applyBoxFit(boxFit, cameraPreviewSize, size); | ||
|
||
double verticalPadding = size.height - adjustedSize.destination.height; | ||
double horizontalPadding = size.width - adjustedSize.destination.width; | ||
if (verticalPadding > 0) { | ||
verticalPadding = verticalPadding / 2; | ||
} else { | ||
verticalPadding = 0; | ||
} | ||
|
||
if (horizontalPadding > 0) { | ||
horizontalPadding = horizontalPadding / 2; | ||
} else { | ||
horizontalPadding = 0; | ||
} | ||
|
||
final double ratioWidth; | ||
final double ratioHeight; | ||
|
||
ratioWidth = cameraPreviewSize.width / adjustedSize.destination.width; | ||
ratioHeight = cameraPreviewSize.height / adjustedSize.destination.height; | ||
|
||
final List<Offset> adjustedOffset = [ | ||
for (final offset in barcodeCorners) | ||
Offset( | ||
offset.dx / ratioWidth + horizontalPadding, | ||
offset.dy / ratioHeight + verticalPadding, | ||
), | ||
]; | ||
|
||
final cutoutPath = Path()..addPolygon(adjustedOffset, true); | ||
|
||
final backgroundPaint = Paint() | ||
..color = Colors.red.withOpacity(0.3) | ||
..style = PaintingStyle.fill; | ||
|
||
canvas.drawPath(cutoutPath, backgroundPaint); | ||
} | ||
|
||
@override | ||
bool shouldRepaint(covariant CustomPainter oldDelegate) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; | ||
import 'package:mobile_scanner/src/overlay/scanner_painter.dart'; | ||
|
||
class ScannerOverlay extends StatelessWidget { | ||
final MobileScannerController controller; | ||
final Rect scanWindow; | ||
|
||
const ScannerOverlay({ | ||
super.key, | ||
required this.controller, | ||
required this.scanWindow, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ValueListenableBuilder( | ||
valueListenable: controller, | ||
builder: (context, value, child) { | ||
// Not ready. | ||
if (!value.isInitialized || | ||
!value.isRunning || | ||
value.error != null || | ||
value.size.isEmpty) { | ||
return const SizedBox(); | ||
} | ||
|
||
return CustomPaint( | ||
size: value.size, | ||
painter: ScannerPainter(scanWindow), | ||
); | ||
}, | ||
); | ||
} | ||
} |
Oops, something went wrong.