The QR Code Scanner package for Appeto SDK.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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