The QR Code Scanner package for Appeto SDK.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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