The QR Code Scanner package for Appeto SDK.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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