Skip to content

Instantly share code, notes, and snippets.

@antasofa7
Forked from r-yeates/qr_overlay.dart
Created November 2, 2023 06:57
Show Gist options
  • Save antasofa7/a4fc30b492487ddd25a00016fba5569e to your computer and use it in GitHub Desktop.
Save antasofa7/a4fc30b492487ddd25a00016fba5569e to your computer and use it in GitHub Desktop.
Overlay for QR scanner package mobile_scanner
import 'package:flutter/material.dart';
class QRScannerOverlay extends StatelessWidget {
const QRScannerOverlay({Key? key, required this.overlayColour})
: super(key: key);
final Color overlayColour;
@override
Widget build(BuildContext context) {
double scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 200.0
: 330.0;
return Stack(children: [
ColorFiltered(
colorFilter: ColorFilter.mode(
overlayColour, BlendMode.srcOut), // This one will create the magic
child: Stack(
children: [
Container(
decoration: const BoxDecoration(
color: Colors.red,
backgroundBlendMode: BlendMode
.dstOut), // This one will handle background + difference out
),
Align(
alignment: Alignment.center,
child: Container(
height: scanArea,
width: scanArea,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(20),
),
),
),
],
),
),
Align(
alignment: Alignment.center,
child: CustomPaint(
foregroundPainter: BorderPainter(),
child: SizedBox(
width: scanArea + 25,
height: scanArea + 25,
),
),
),
]);
}
}
// Creates the white borders
class BorderPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
const width = 4.0;
const radius = 20.0;
const tRadius = 3 * radius;
final rect = Rect.fromLTWH(
width,
width,
size.width - 2 * width,
size.height - 2 * width,
);
final rrect = RRect.fromRectAndRadius(rect, const Radius.circular(radius));
const clippingRect0 = Rect.fromLTWH(
0,
0,
tRadius,
tRadius,
);
final clippingRect1 = Rect.fromLTWH(
size.width - tRadius,
0,
tRadius,
tRadius,
);
final clippingRect2 = Rect.fromLTWH(
0,
size.height - tRadius,
tRadius,
tRadius,
);
final clippingRect3 = Rect.fromLTWH(
size.width - tRadius,
size.height - tRadius,
tRadius,
tRadius,
);
final path = Path()
..addRect(clippingRect0)
..addRect(clippingRect1)
..addRect(clippingRect2)
..addRect(clippingRect3);
canvas.clipPath(path);
canvas.drawRRect(
rrect,
Paint()
..color = Colors.white
..style = PaintingStyle.stroke
..strokeWidth = width,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
class BarReaderSize {
static double width = 200;
static double height = 200;
}
class OverlayWithHolePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = Colors.black54;
canvas.drawPath(
Path.combine(
PathOperation.difference,
Path()..addRect(Rect.fromLTWH(0, 0, size.width, size.height)),
Path()
..addOval(Rect.fromCircle(
center: Offset(size.width - 44, size.height - 44), radius: 40))
..close(),
),
paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment