Procházet zdrojové kódy

QRViewController updated

- MethodChannel is now handled inside the controller
- Removed controller init, pass globalKey as required parameter
- Controller now returns a stream of data
- Updated README.md + example
flutter-beta
Luis Thein před 7 roky
rodič
revize
224d1ebb71
3 změnil soubory, kde provedl 132 přidání a 103 odebrání
  1. +40
    -20
      README.md
  2. +79
    -72
      example/lib/main.dart
  3. +13
    -11
      lib/qr_code_scanner.dart

+ 40
- 20
README.md Zobrazit soubor

@@ -60,31 +60,39 @@ class _QRViewExampleState extends State<QRViewExample> {
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
],
),
);
children: <Widget>[
Expanded(
flex: 5,
child: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
),
),
Expanded(
flex: 1,
child: Center(
child: Text('Scan result: $qrText'),
),
)
],
),
);
}

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.scannedDataStream.listen((scanData) {
setState(() {
qrText = scanData;
});
});
}

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

@@ -104,9 +112,21 @@ controller.flipCamera();
## Flash (Off/On)
By default, flash is OFF.
```dart
controller.flipFlash();
controller.toggleFlash();
```

## Resume/Pause
Pause camera stream and scanner.
```dart
controller.pause();
```
Resume camera stream and scanner.
```dart
controller.resume();
```



# TODO'S:
* iOS Native embedding is written to match what is supported in the framework as of the date of publication of this package. It needs to be improved as the framework support improves.
* In future, options will be provided for default states.


+ 79
- 72
example/lib/main.dart Zobrazit soubor

@@ -37,79 +37,86 @@ class _QRViewExampleState extends State<QRViewExample> {
flex: 4,
),
Expanded(
child: Column(
children: <Widget>[
Text("This is the result of scan: $qrText"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
if (controller != null) {
controller.toggleFlash();
if (_isFlashOn(flashState))
setState(() {
flashState = flash_off;
});
else
setState(() {
flashState = flash_on;
});
}
},
child: Text(flashState, style: TextStyle(fontSize: 20)),
child: FittedBox(
fit: BoxFit.contain,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text("This is the result of scan: $qrText"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
if (controller != null) {
controller.toggleFlash();
if (_isFlashOn(flashState)) {
setState(() {
flashState = flash_off;
});
} else {
setState(() {
flashState = flash_on;
});
}
}
},
child:
Text(flashState, style: TextStyle(fontSize: 20)),
),
),
),
Container(
margin: EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
if (controller != null) {
controller.flipCamera();
if (_isBackCamera(cameraState))
setState(() {
cameraState = front_camera;
});
else
setState(() {
cameraState = back_camera;
});
}
},
child:
Text(cameraState, style: TextStyle(fontSize: 20)),
Container(
margin: EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
if (controller != null) {
controller.flipCamera();
if (_isBackCamera(cameraState)) {
setState(() {
cameraState = front_camera;
});
} else {
setState(() {
cameraState = back_camera;
});
}
}
},
child:
Text(cameraState, style: TextStyle(fontSize: 20)),
),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
controller?.pauseCamera();
},
child: Text('pause', style: TextStyle(fontSize: 20)),
),
),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 8.0),
child: RaisedButton(
onPressed: () {
controller?.pauseCamera();
},
child: Text('pause', style: TextStyle(fontSize: 20)),
),
),
Container(
margin: EdgeInsets.only(bottom: 8.0),
child: RaisedButton(
onPressed: () {
controller.resumeCamera();
},
child: Text('resume', style: TextStyle(fontSize: 20)),
),
)
],
),
],
Container(
margin: EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
controller?.resumeCamera();
},
child: Text('resume', style: TextStyle(fontSize: 20)),
),
)
],
),
],
),
),
flex: 1,
)
@@ -128,7 +135,7 @@ class _QRViewExampleState extends State<QRViewExample> {

void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedData.listen((scanData) {
controller.scannedDataStream.listen((scanData) {
setState(() {
qrText = scanData;
});


+ 13
- 11
lib/qr_code_scanner.dart Zobrazit soubor

@@ -10,7 +10,9 @@ class QRView extends StatefulWidget {
const QRView({
@required Key key,
@required this.onQRViewCreated,
}) : super(key: key);
}) : assert(key != null),
assert(onQRViewCreated != null),
super(key: key);

final QRViewCreatedCallback onQRViewCreated;

@@ -75,18 +77,20 @@ class _CreationParams {
class QRViewController {
static const scanMethodCall = "onRecognizeQR";

final MethodChannel _channel;

StreamController<String> _scanUpdateController = StreamController<String>();

Stream<String> get scannedData => _scanUpdateController.stream;
Stream<String> get scannedDataStream => _scanUpdateController.stream;

QRViewController._(int id, GlobalKey qrKey)
: channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') {
: _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id') {
if (defaultTargetPlatform == TargetPlatform.iOS) {
final RenderBox renderBox = qrKey.currentContext.findRenderObject();
channel.invokeMethod("setDimensions",
_channel.invokeMethod("setDimensions",
{"width": renderBox.size.width, "height": renderBox.size.height});
}
channel.setMethodCallHandler(
_channel.setMethodCallHandler(
(MethodCall call) async {
switch (call.method) {
case scanMethodCall:
@@ -98,22 +102,20 @@ class QRViewController {
);
}

final MethodChannel channel;

void flipCamera() {
channel.invokeMethod("flipCamera");
_channel.invokeMethod("flipCamera");
}

void toggleFlash() {
channel.invokeMethod("toggleFlash");
_channel.invokeMethod("toggleFlash");
}

void pauseCamera() {
channel.invokeMethod("pauseCamera");
_channel.invokeMethod("pauseCamera");
}

void resumeCamera() {
channel.invokeMethod("resumeCamera");
_channel.invokeMethod("resumeCamera");
}

void dispose() {


Načítá se…
Zrušit
Uložit