The QR Code Scanner package for Appeto SDK.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

169 lines
4.3 KiB

  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. class QrScannerOverlayShape extends ShapeBorder {
  4. final Color borderColor;
  5. final double borderWidth;
  6. final Color overlayColor;
  7. final double borderRadius;
  8. final double borderLength;
  9. final double cutOutSize;
  10. QrScannerOverlayShape({
  11. this.borderColor = Colors.red,
  12. this.borderWidth = 3.0,
  13. this.overlayColor = const Color.fromRGBO(0, 0, 0, 80),
  14. this.borderRadius = 0,
  15. this.borderLength = 40,
  16. this.cutOutSize = 250,
  17. }) : assert(
  18. cutOutSize != null
  19. ? cutOutSize != null
  20. ? borderLength <= cutOutSize / 2 + borderWidth * 2
  21. : true
  22. : true,
  23. "Border can't be larger than ${cutOutSize / 2 + borderWidth * 2}");
  24. @override
  25. EdgeInsetsGeometry get dimensions => const EdgeInsets.all(10.0);
  26. @override
  27. Path getInnerPath(Rect rect, {TextDirection textDirection}) {
  28. return Path()
  29. ..fillType = PathFillType.evenOdd
  30. ..addPath(getOuterPath(rect), Offset.zero);
  31. }
  32. @override
  33. Path getOuterPath(Rect rect, {TextDirection textDirection}) {
  34. Path _getLeftTopPath(Rect rect) {
  35. return Path()
  36. ..moveTo(rect.left, rect.bottom)
  37. ..lineTo(rect.left, rect.top)
  38. ..lineTo(rect.right, rect.top);
  39. }
  40. return _getLeftTopPath(rect)
  41. ..lineTo(
  42. rect.right,
  43. rect.bottom,
  44. )
  45. ..lineTo(
  46. rect.left,
  47. rect.bottom,
  48. )
  49. ..lineTo(
  50. rect.left,
  51. rect.top,
  52. );
  53. }
  54. @override
  55. void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
  56. final width = rect.width;
  57. final borderWidthSize = width / 2;
  58. final height = rect.height;
  59. final borderOffset = borderWidth / 2;
  60. final _borderLength = borderLength > cutOutSize / 2 + borderWidth * 2
  61. ? borderWidthSize / 2
  62. : borderLength;
  63. final _cutOutSize = cutOutSize != null && cutOutSize < width
  64. ? cutOutSize
  65. : width - borderOffset;
  66. final backgroundPaint = Paint()
  67. ..color = overlayColor
  68. ..style = PaintingStyle.fill;
  69. final borderPaint = Paint()
  70. ..color = borderColor
  71. ..style = PaintingStyle.stroke
  72. ..strokeWidth = borderWidth;
  73. final boxPaint = Paint()
  74. ..color = borderColor
  75. ..style = PaintingStyle.fill
  76. ..blendMode = BlendMode.dstOut;
  77. final cutOutRect = Rect.fromLTWH(
  78. width / 2 - _cutOutSize / 2 + borderOffset,
  79. height / 2 - _cutOutSize / 2 + borderOffset,
  80. _cutOutSize - borderOffset * 2,
  81. _cutOutSize - borderOffset * 2,
  82. );
  83. canvas.saveLayer(
  84. rect,
  85. backgroundPaint,
  86. );
  87. canvas
  88. ..drawRect(
  89. rect,
  90. backgroundPaint,
  91. )
  92. // Draw top right corner
  93. ..drawRRect(
  94. RRect.fromLTRBAndCorners(
  95. cutOutRect.right - _borderLength,
  96. cutOutRect.top,
  97. cutOutRect.right,
  98. cutOutRect.top + _borderLength,
  99. topRight: Radius.circular(borderRadius),
  100. ),
  101. borderPaint,
  102. )
  103. // Draw top left corner
  104. ..drawRRect(
  105. RRect.fromLTRBAndCorners(
  106. cutOutRect.left,
  107. cutOutRect.top,
  108. cutOutRect.left + _borderLength,
  109. cutOutRect.top + _borderLength,
  110. topLeft: Radius.circular(borderRadius),
  111. ),
  112. borderPaint,
  113. )
  114. // Draw bottom right corner
  115. ..drawRRect(
  116. RRect.fromLTRBAndCorners(
  117. cutOutRect.right - _borderLength,
  118. cutOutRect.bottom - _borderLength,
  119. cutOutRect.right,
  120. cutOutRect.bottom,
  121. bottomRight: Radius.circular(borderRadius),
  122. ),
  123. borderPaint,
  124. )
  125. // Draw bottom left corner
  126. ..drawRRect(
  127. RRect.fromLTRBAndCorners(
  128. cutOutRect.left,
  129. cutOutRect.bottom - _borderLength,
  130. cutOutRect.left + _borderLength,
  131. cutOutRect.bottom,
  132. bottomLeft: Radius.circular(borderRadius),
  133. ),
  134. borderPaint,
  135. )
  136. ..drawRRect(
  137. RRect.fromRectAndRadius(
  138. cutOutRect,
  139. Radius.circular(borderRadius),
  140. ),
  141. boxPaint,
  142. )
  143. ..restore();
  144. }
  145. @override
  146. ShapeBorder scale(double t) {
  147. return QrScannerOverlayShape(
  148. borderColor: borderColor,
  149. borderWidth: borderWidth,
  150. overlayColor: overlayColor,
  151. );
  152. }
  153. }