The QR Code Scanner package for Appeto SDK.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

143 строки
3.5 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 void QRViewCreatedCallback(QRViewController controller);
  6. class QRView extends StatefulWidget {
  7. const QRView({
  8. @required Key key,
  9. @required this.onQRViewCreated,
  10. this.overlay,
  11. }) : assert(key != null),
  12. super(key: key);
  13. final QRViewCreatedCallback onQRViewCreated;
  14. final ShapeBorder overlay;
  15. @override
  16. State<StatefulWidget> createState() => _QRViewState();
  17. }
  18. class _QRViewState extends State<QRView> {
  19. @override
  20. Widget build(BuildContext context) {
  21. return Stack(
  22. children: [
  23. _getPlatformQrView(),
  24. widget.overlay != null
  25. ? Container(
  26. decoration: ShapeDecoration(
  27. shape: widget.overlay,
  28. ),
  29. )
  30. : Container(),
  31. ],
  32. );
  33. }
  34. Widget _getPlatformQrView() {
  35. Widget _platformQrView;
  36. switch (defaultTargetPlatform) {
  37. case TargetPlatform.android:
  38. _platformQrView = AndroidView(
  39. viewType: 'net.touchcapture.qr.flutterqr/qrview',
  40. onPlatformViewCreated: _onPlatformViewCreated,
  41. );
  42. break;
  43. case TargetPlatform.iOS:
  44. _platformQrView = UiKitView(
  45. viewType: 'net.touchcapture.qr.flutterqr/qrview',
  46. onPlatformViewCreated: _onPlatformViewCreated,
  47. creationParams: _CreationParams.fromWidget(0, 0).toMap(),
  48. creationParamsCodec: StandardMessageCodec(),
  49. );
  50. break;
  51. default:
  52. throw UnsupportedError(
  53. "Trying to use the default webview implementation for $defaultTargetPlatform but there isn't a default one");
  54. }
  55. return _platformQrView;
  56. }
  57. void _onPlatformViewCreated(int id) {
  58. if (widget.onQRViewCreated == null) {
  59. return;
  60. }
  61. widget.onQRViewCreated(QRViewController._(id, widget.key));
  62. }
  63. }
  64. class _CreationParams {
  65. _CreationParams({this.width, this.height});
  66. static _CreationParams fromWidget(double width, double height) {
  67. return _CreationParams(
  68. width: width,
  69. height: height,
  70. );
  71. }
  72. final double width;
  73. final double height;
  74. Map<String, dynamic> toMap() {
  75. return <String, dynamic>{
  76. 'width': width,
  77. 'height': height,
  78. };
  79. }
  80. }
  81. class QRViewController {
  82. static const scanMethodCall = "onRecognizeQR";
  83. StreamController<String> _scanUpdateController = StreamController<String>();
  84. Stream<String> get scannedData => _scanUpdateController.stream;
  85. MethodChannel _channel;
  86. QRViewController._(int id, GlobalKey qrKey) {
  87. _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id');
  88. if (defaultTargetPlatform == TargetPlatform.iOS) {
  89. final RenderBox renderBox = qrKey.currentContext.findRenderObject();
  90. _channel.invokeMethod("setDimensions",
  91. {"width": renderBox.size.width, "height": renderBox.size.height});
  92. }
  93. _channel.setMethodCallHandler(
  94. (MethodCall call) async {
  95. switch (call.method) {
  96. case scanMethodCall:
  97. if (call.arguments != null) {
  98. _scanUpdateController.sink.add(call.arguments.toString());
  99. }
  100. }
  101. },
  102. );
  103. }
  104. void flipCamera() {
  105. _channel.invokeMethod("flipCamera");
  106. }
  107. void toggleFlash() {
  108. _channel.invokeMethod("toggleFlash");
  109. }
  110. void pauseCamera() {
  111. _channel.invokeMethod("pauseCamera");
  112. }
  113. void resumeCamera() {
  114. _channel.invokeMethod("resumeCamera");
  115. }
  116. void dispose() {
  117. _scanUpdateController.close();
  118. }
  119. }