The QR Code Scanner package for Appeto SDK.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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