The QR Code Scanner package for Appeto SDK.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

125 líneas
3.1 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. }) : assert(key != null),
  11. assert(onQRViewCreated != null),
  12. super(key: key);
  13. final QRViewCreatedCallback onQRViewCreated;
  14. @override
  15. State<StatefulWidget> createState() => _QRViewState();
  16. }
  17. class _QRViewState extends State<QRView> {
  18. @override
  19. Widget build(BuildContext context) {
  20. var androidView = AndroidView(
  21. viewType: 'net.touchcapture.qr.flutterqr/qrview',
  22. onPlatformViewCreated: _onPlatformViewCreated,
  23. );
  24. if (defaultTargetPlatform == TargetPlatform.android) {
  25. return androidView;
  26. }
  27. if (defaultTargetPlatform == TargetPlatform.iOS) {
  28. return UiKitView(
  29. viewType: 'net.touchcapture.qr.flutterqr/qrview',
  30. onPlatformViewCreated: _onPlatformViewCreated,
  31. creationParams: _CreationParams.fromWidget(0, 0).toMap(),
  32. creationParamsCodec: StandardMessageCodec(),
  33. );
  34. }
  35. return Text(
  36. '$defaultTargetPlatform is not yet supported by the text_view plugin');
  37. }
  38. void _onPlatformViewCreated(int id) {
  39. if (widget.onQRViewCreated == null) {
  40. return;
  41. }
  42. widget.onQRViewCreated(QRViewController._(id, widget.key));
  43. }
  44. }
  45. class _CreationParams {
  46. _CreationParams({this.width, this.height});
  47. static _CreationParams fromWidget(double width, double height) {
  48. return _CreationParams(
  49. width: width,
  50. height: height,
  51. );
  52. }
  53. final double width;
  54. final double height;
  55. Map<String, dynamic> toMap() {
  56. return <String, dynamic>{
  57. 'width': width,
  58. 'height': height,
  59. };
  60. }
  61. }
  62. class QRViewController {
  63. static const scanMethodCall = "onRecognizeQR";
  64. final MethodChannel _channel;
  65. StreamController<String> _scanUpdateController = StreamController<String>();
  66. Stream<String> get scannedDataStream => _scanUpdateController.stream;
  67. QRViewController._(int id, GlobalKey qrKey)
  68. : _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') {
  69. if (defaultTargetPlatform == TargetPlatform.iOS) {
  70. final RenderBox renderBox = qrKey.currentContext.findRenderObject();
  71. _channel.invokeMethod("setDimensions",
  72. {"width": renderBox.size.width, "height": renderBox.size.height});
  73. }
  74. _channel.setMethodCallHandler(
  75. (MethodCall call) async {
  76. switch (call.method) {
  77. case scanMethodCall:
  78. if (call.arguments != null) {
  79. _scanUpdateController.sink.add(call.arguments.toString());
  80. }
  81. }
  82. },
  83. );
  84. }
  85. void flipCamera() {
  86. _channel.invokeMethod("flipCamera");
  87. }
  88. void toggleFlash() {
  89. _channel.invokeMethod("toggleFlash");
  90. }
  91. void pauseCamera() {
  92. _channel.invokeMethod("pauseCamera");
  93. }
  94. void resumeCamera() {
  95. _channel.invokeMethod("resumeCamera");
  96. }
  97. void dispose() {
  98. _scanUpdateController.close();
  99. }
  100. }