Переглянути джерело

Added scanDataStream & simplified Widget creation

- the QrViewController has a getter that returns a Stream of data that gets updated on every scan result
  - theres no need to init the QrViewController through a method, because the GlobalKey is generated inside the QrViewWidget
  - a dispose method was added to correctly dispose the stream
flutter-beta
Luis Thein 7 роки тому
джерело
коміт
4512a3d28b
2 змінених файлів з 37 додано та 18 видалено
  1. +10
    -13
      example/lib/main.dart
  2. +27
    -5
      lib/qr_code_scanner.dart

+ 10
- 13
example/lib/main.dart Переглянути файл

@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

void main() => runApp(MaterialApp(home: QRViewExample()));
@@ -19,7 +18,6 @@ class QRViewExample extends StatefulWidget {
}

class _QRViewExampleState extends State<QRViewExample> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
var qrText = "";
var flashState = flash_on;
var cameraState = front_camera;
@@ -32,7 +30,6 @@ class _QRViewExampleState extends State<QRViewExample> {
children: <Widget>[
Expanded(
child: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
flex: 4,
@@ -128,17 +125,17 @@ class _QRViewExampleState extends State<QRViewExample> {
}

void _onQRViewCreated(QRViewController controller) {
final channel = controller.channel;
controller.init(qrKey);
this.controller = controller;
channel.setMethodCallHandler((MethodCall call) async {
switch (call.method) {
case "onRecognizeQR":
dynamic arguments = call.arguments;
setState(() {
qrText = arguments.toString();
});
}
controller.scannedData.listen((scanData) {
setState(() {
qrText = scanData;
});
});
}

@override
void dispose() {
controller.dispose();
super.dispose();
}
}

+ 27
- 5
lib/qr_code_scanner.dart Переглянути файл

@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -17,6 +19,7 @@ class QRView extends StatefulWidget {
}

class _QRViewState extends State<QRView> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
@override
Widget build(BuildContext context) {
var androidView = AndroidView(
@@ -45,7 +48,7 @@ class _QRViewState extends State<QRView> {
if (widget.onQRViewCreated == null) {
return;
}
widget.onQRViewCreated(new QRViewController._(id));
widget.onQRViewCreated(QRViewController._(id, qrKey));
}
}

@@ -71,18 +74,33 @@ class _CreationParams {
}

class QRViewController {
QRViewController._(int id)
: channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id');
final MethodChannel channel;
static const scanMethodCall = "onRecognizeQR";
StreamController<String> _scanUpdateController = StreamController<String>();

void init(GlobalKey qrKey) {
Stream<String> get scannedData => _scanUpdateController.stream;

QRViewController._(int id, GlobalKey qrKey)
: channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') {
if (defaultTargetPlatform == TargetPlatform.iOS) {
final RenderBox renderBox = qrKey.currentContext.findRenderObject();
channel.invokeMethod("setDimensions",
{"width": renderBox.size.width, "height": renderBox.size.height});
}
channel.setMethodCallHandler(
(MethodCall call) async {
switch (call.method) {
case scanMethodCall:
if (call.arguments != null) {
_scanUpdateController.sink.add(call.arguments.toString());
}
}
},
);
}

final MethodChannel channel;

void flipCamera() {
channel.invokeMethod("flipCamera");
}
@@ -98,4 +116,8 @@ class QRViewController {
void resumeCamera() {
channel.invokeMethod("resumeCamera");
}

void dispose() {
_scanUpdateController.close();
}
}

Завантаження…
Відмінити
Зберегти