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.
 
 
 
 
 
 

167 regels
4.3 KiB

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