The QR Code Scanner package for Appeto SDK.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

235 Zeilen
5.4 KiB

  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. typedef QRViewCreatedCallback = void Function(QRViewController);
  6. enum BarcodeFormat {
  7. /// Aztec 2D barcode format.
  8. aztec,
  9. /// CODABAR 1D format.
  10. codabar,
  11. /// Code 39 1D format.
  12. code39,
  13. /// Code 93 1D format.
  14. code93,
  15. /// Code 128 1D format.
  16. code128,
  17. /// Data Matrix 2D barcode format.
  18. dataMatrix,
  19. /// EAN-8 1D format.
  20. ean8,
  21. /// EAN-13 1D format.
  22. ean13,
  23. /// ITF (Interleaved Two of Five) 1D format.
  24. itf,
  25. /// MaxiCode 2D barcode format.
  26. maxicode,
  27. /// PDF417 format.
  28. pdf417,
  29. /// QR Code 2D barcode format.
  30. qrcode,
  31. /// RSS 14
  32. rss14,
  33. /// RSS EXPANDED
  34. rssExpanded,
  35. /// UPC-A 1D format.
  36. upcA,
  37. /// UPC-E 1D format.
  38. upcE,
  39. /// UPC/EAN extension format. Not a stand-alone format.
  40. upcEanExtension
  41. }
  42. const _formatNames = <String, BarcodeFormat>{
  43. 'AZTEC': BarcodeFormat.aztec,
  44. 'CODABAR': BarcodeFormat.codabar,
  45. 'CODE_39': BarcodeFormat.code39,
  46. 'CODE_93': BarcodeFormat.code93,
  47. 'CODE_128': BarcodeFormat.code128,
  48. 'DATA_MATRIX': BarcodeFormat.dataMatrix,
  49. 'EAN_8': BarcodeFormat.ean8,
  50. 'EAN_13': BarcodeFormat.ean13,
  51. 'ITF': BarcodeFormat.itf,
  52. 'MAXICODE': BarcodeFormat.maxicode,
  53. 'PDF_417': BarcodeFormat.pdf417,
  54. 'QR_CODE': BarcodeFormat.qrcode,
  55. 'RSS_14': BarcodeFormat.rss14,
  56. 'RSS_EXPANDED': BarcodeFormat.rssExpanded,
  57. 'UPC_A': BarcodeFormat.upcA,
  58. 'UPC_E': BarcodeFormat.upcE,
  59. 'UPC_EAN_EXTENSION': BarcodeFormat.upcEanExtension,
  60. };
  61. class Barcode {
  62. Barcode(this.code, this.format);
  63. final String code;
  64. final BarcodeFormat format;
  65. }
  66. class QRView extends StatefulWidget {
  67. const QRView({
  68. @required Key key,
  69. @required this.onQRViewCreated,
  70. this.overlay,
  71. }) : assert(key != null),
  72. assert(onQRViewCreated != null),
  73. super(key: key);
  74. final QRViewCreatedCallback onQRViewCreated;
  75. final ShapeBorder overlay;
  76. @override
  77. State<StatefulWidget> createState() => _QRViewState();
  78. }
  79. class _QRViewState extends State<QRView> {
  80. @override
  81. Widget build(BuildContext context) {
  82. return Stack(
  83. children: [
  84. _getPlatformQrView(),
  85. if (widget.overlay != null)
  86. Container(
  87. decoration: ShapeDecoration(
  88. shape: widget.overlay,
  89. ),
  90. )
  91. else
  92. Container(),
  93. ],
  94. );
  95. }
  96. Widget _getPlatformQrView() {
  97. Widget _platformQrView;
  98. switch (defaultTargetPlatform) {
  99. case TargetPlatform.android:
  100. _platformQrView = AndroidView(
  101. viewType: 'net.touchcapture.qr.flutterqr/qrview',
  102. onPlatformViewCreated: _onPlatformViewCreated,
  103. );
  104. break;
  105. case TargetPlatform.iOS:
  106. _platformQrView = UiKitView(
  107. viewType: 'net.touchcapture.qr.flutterqr/qrview',
  108. onPlatformViewCreated: _onPlatformViewCreated,
  109. creationParams: _CreationParams.fromWidget(0, 0).toMap(),
  110. creationParamsCodec: StandardMessageCodec(),
  111. );
  112. break;
  113. default:
  114. throw UnsupportedError(
  115. "Trying to use the default webview implementation for $defaultTargetPlatform but there isn't a default one");
  116. }
  117. return _platformQrView;
  118. }
  119. void _onPlatformViewCreated(int id) {
  120. if (widget.onQRViewCreated == null) {
  121. return;
  122. }
  123. widget.onQRViewCreated(QRViewController._(id, widget.key));
  124. }
  125. }
  126. class _CreationParams {
  127. _CreationParams({this.width, this.height});
  128. static _CreationParams fromWidget(double width, double height) {
  129. return _CreationParams(
  130. width: width,
  131. height: height,
  132. );
  133. }
  134. final double width;
  135. final double height;
  136. Map<String, dynamic> toMap() {
  137. return <String, dynamic>{
  138. 'width': width,
  139. 'height': height,
  140. };
  141. }
  142. }
  143. class QRViewController {
  144. QRViewController._(int id, GlobalKey qrKey)
  145. : _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') {
  146. if (defaultTargetPlatform == TargetPlatform.iOS) {
  147. final RenderBox renderBox = qrKey.currentContext.findRenderObject();
  148. _channel.invokeMethod('setDimensions',
  149. {'width': renderBox.size.width, 'height': renderBox.size.height});
  150. }
  151. _channel.setMethodCallHandler(
  152. (call) async {
  153. switch (call.method) {
  154. case scanMethodCall:
  155. if (call.arguments != null) {
  156. final args = call.arguments as Map;
  157. final code = args['code'] as String;
  158. final rawType = args['type'] as String;
  159. final format = _formatNames[rawType];
  160. if (format != null) {
  161. final barcode = Barcode(code, format);
  162. _scanUpdateController.sink.add(barcode);
  163. } else {
  164. throw Exception('Unexpected barcode type $rawType');
  165. }
  166. }
  167. }
  168. },
  169. );
  170. }
  171. static const scanMethodCall = 'onRecognizeQR';
  172. final MethodChannel _channel;
  173. final StreamController<Barcode> _scanUpdateController =
  174. StreamController<Barcode>();
  175. Stream<Barcode> get scannedDataStream => _scanUpdateController.stream;
  176. void flipCamera() {
  177. _channel.invokeMethod('flipCamera');
  178. }
  179. void toggleFlash() {
  180. _channel.invokeMethod('toggleFlash');
  181. }
  182. void pauseCamera() {
  183. _channel.invokeMethod('pauseCamera');
  184. }
  185. void resumeCamera() {
  186. _channel.invokeMethod('resumeCamera');
  187. }
  188. void dispose() {
  189. _scanUpdateController.close();
  190. }
  191. }