The QR Code Scanner package for Appeto SDK.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

172 rindas
4.4 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(
  13. cutOutSize != null ??
  14. cutOutSize != null ??
  15. borderLength <= cutOutSize / 2 + borderWidth * 2,
  16. "Border can't be larger than ${cutOutSize / 2 + borderWidth * 2}");
  17. final Color borderColor;
  18. final double borderWidth;
  19. final Color overlayColor;
  20. final double borderRadius;
  21. final double borderLength;
  22. final double cutOutSize;
  23. final double cutOutBottomOffset;
  24. @override
  25. EdgeInsetsGeometry get dimensions => const EdgeInsets.all(10);
  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. rect.left + width / 2 - _cutOutSize / 2 + borderOffset,
  79. -cutOutBottomOffset +
  80. rect.top +
  81. height / 2 -
  82. _cutOutSize / 2 +
  83. borderOffset,
  84. _cutOutSize - borderOffset * 2,
  85. _cutOutSize - borderOffset * 2,
  86. );
  87. canvas
  88. ..saveLayer(
  89. rect,
  90. backgroundPaint,
  91. )
  92. ..drawRect(
  93. rect,
  94. backgroundPaint,
  95. )
  96. // Draw top right corner
  97. ..drawRRect(
  98. RRect.fromLTRBAndCorners(
  99. cutOutRect.right - _borderLength,
  100. cutOutRect.top,
  101. cutOutRect.right,
  102. cutOutRect.top + _borderLength,
  103. topRight: Radius.circular(borderRadius),
  104. ),
  105. borderPaint,
  106. )
  107. // Draw top left corner
  108. ..drawRRect(
  109. RRect.fromLTRBAndCorners(
  110. cutOutRect.left,
  111. cutOutRect.top,
  112. cutOutRect.left + _borderLength,
  113. cutOutRect.top + _borderLength,
  114. topLeft: Radius.circular(borderRadius),
  115. ),
  116. borderPaint,
  117. )
  118. // Draw bottom right corner
  119. ..drawRRect(
  120. RRect.fromLTRBAndCorners(
  121. cutOutRect.right - _borderLength,
  122. cutOutRect.bottom - _borderLength,
  123. cutOutRect.right,
  124. cutOutRect.bottom,
  125. bottomRight: Radius.circular(borderRadius),
  126. ),
  127. borderPaint,
  128. )
  129. // Draw bottom left corner
  130. ..drawRRect(
  131. RRect.fromLTRBAndCorners(
  132. cutOutRect.left,
  133. cutOutRect.bottom - _borderLength,
  134. cutOutRect.left + _borderLength,
  135. cutOutRect.bottom,
  136. bottomLeft: Radius.circular(borderRadius),
  137. ),
  138. borderPaint,
  139. )
  140. ..drawRRect(
  141. RRect.fromRectAndRadius(
  142. cutOutRect,
  143. Radius.circular(borderRadius),
  144. ),
  145. boxPaint,
  146. )
  147. ..restore();
  148. }
  149. @override
  150. ShapeBorder scale(double t) {
  151. return QrScannerOverlayShape(
  152. borderColor: borderColor,
  153. borderWidth: borderWidth,
  154. overlayColor: overlayColor,
  155. );
  156. }
  157. }