diff --git a/README.md b/README.md index 09332c5..888c668 100644 --- a/README.md +++ b/README.md @@ -60,31 +60,39 @@ class _QRViewExampleState extends State { Widget build(BuildContext context) { return Scaffold( body: Column( - children: [ - QRView( - key: qrKey, - onQRViewCreated: _onQRViewCreated, - ), - ], - ), - ); + children: [ + 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. diff --git a/example/lib/main.dart b/example/lib/main.dart index 866346b..b1c8bc5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -48,6 +48,7 @@ class _QRViewExampleState extends State { child: FittedBox( fit: BoxFit.contain, child: Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Text("This is the result of scan: $qrText"), Row( @@ -60,14 +61,15 @@ class _QRViewExampleState extends State { onPressed: () { if (controller != null) { controller.toggleFlash(); - if (_isFlashOn(flashState)) + if (_isFlashOn(flashState)) { setState(() { flashState = flash_off; }); - else + } else { setState(() { flashState = flash_on; }); + } } }, child: @@ -80,14 +82,15 @@ class _QRViewExampleState extends State { onPressed: () { if (controller != null) { controller.flipCamera(); - if (_isBackCamera(cameraState)) + if (_isBackCamera(cameraState)) { setState(() { cameraState = front_camera; }); - else + } else { setState(() { cameraState = back_camera; }); + } } }, child: @@ -97,11 +100,11 @@ class _QRViewExampleState extends State { ], ), Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, + mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( - margin: EdgeInsets.only(bottom: 8.0), + margin: EdgeInsets.all(8.0), child: RaisedButton( onPressed: () { controller?.pauseCamera(); @@ -110,10 +113,10 @@ class _QRViewExampleState extends State { ), ), Container( - margin: EdgeInsets.only(bottom: 8.0), + margin: EdgeInsets.all(8.0), child: RaisedButton( onPressed: () { - controller.resumeCamera(); + controller?.resumeCamera(); }, child: Text('resume', style: TextStyle(fontSize: 20)), ), @@ -140,7 +143,7 @@ class _QRViewExampleState extends State { void _onQRViewCreated(QRViewController controller) { this.controller = controller; - controller.scannedData.listen((scanData) { + controller.scannedDataStream.listen((scanData) { setState(() { qrText = scanData; }); diff --git a/lib/qr_code_scanner.dart b/lib/qr_code_scanner.dart index e7b5559..036e27f 100644 --- a/lib/qr_code_scanner.dart +++ b/lib/qr_code_scanner.dart @@ -12,6 +12,7 @@ class QRView extends StatefulWidget { @required this.onQRViewCreated, this.overlay, }) : assert(key != null), + assert(onQRViewCreated != null), super(key: key); final QRViewCreatedCallback onQRViewCreated; @@ -95,14 +96,14 @@ class _CreationParams { class QRViewController { static const scanMethodCall = "onRecognizeQR"; - StreamController _scanUpdateController = StreamController(); + final MethodChannel _channel; - Stream get scannedData => _scanUpdateController.stream; + StreamController _scanUpdateController = StreamController(); - MethodChannel _channel; + Stream get scannedDataStream => _scanUpdateController.stream; - QRViewController._(int id, GlobalKey qrKey) { - _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id'); + 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",