From baed2d98c6825b2d03cdf4ed0cbee3905e843a43 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 26 May 2020 23:26:05 +1000 Subject: [PATCH] Yay, barcode scanning working again!!! --- lib/barcode.dart | 71 ++++++++++++++++++++++++++++++++++++++++++++++-- lib/main.dart | 2 +- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/lib/barcode.dart b/lib/barcode.dart index e0305eab..fb47348b 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -53,6 +53,63 @@ class _QRViewState extends State { print("Response:"); print(response.body); + + if (response.statusCode != 200) { + showDialog( + context: context, + child: new SimpleDialog( + title: Text("Server Error"), + children: [ + ListTile( + title: Text("Error ${response.statusCode}"), + subtitle: Text("${response.body.toString().split("\n").first}"), + ) + ], + ), + ); + + return; + } + + // Decode the response + final Map body = json.decode(response.body); + + // "Error" contained in response + if (body.containsKey('error')) { + showDialog( + context: context, + child: new SimpleDialog( + title: Text("Barcode Error"), + children: [ + ListTile( + title: Text("${body['error']}"), + subtitle: Text( + "Plugin: ${body['plugin'] ?? ''}"), + ) + ], + ) + ); + + return; + } else if (body.containsKey('success')) { + // Decode the barcode! + // Ideally, the server has returned unto us something sensible... + _handleBarcode(context, body); + } else { + showDialog( + context: context, + child: new SimpleDialog( + title: Text("Unknown response"), + children: [ + ListTile( + title: Text("Response data"), + subtitle: Text("${body.toString()}"), + ) + ], + ) + ); + } + }).timeout( Duration(seconds: 5) ).catchError((error) { @@ -63,10 +120,16 @@ class _QRViewState extends State { } + Future _doProcessBarcode(String barcode) async { + await processBarcode(barcode); + } + void _onViewCreated(QRViewController controller) { _controller = controller; controller.scannedDataStream.listen((scandata) { - processBarcode(scandata); + _controller?.pauseCamera(); + _doProcessBarcode(scandata); + _controller?.resumeCamera(); }); } @@ -207,6 +270,7 @@ void _handleBarcode(BuildContext context, Map data) { if (pk != null) { InvenTreeStockLocation().get(context, pk).then((var loc) { if (loc is InvenTreeStockLocation) { + Navigator.of(context).pop(); Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(loc))); } }); @@ -220,6 +284,7 @@ void _handleBarcode(BuildContext context, Map data) { if (pk != null) { InvenTreeStockItem().get(context, pk).then((var item) { + Navigator.of(context).pop(); Navigator.push(context, MaterialPageRoute(builder: (context) => StockDetailWidget(item))); }); } else { @@ -231,8 +296,8 @@ void _handleBarcode(BuildContext context, Map data) { if (pk != null) { InvenTreePart().get(context, pk).then((var part) { - Navigator.push(context, - MaterialPageRoute(builder: (context) => PartDetailWidget(part))); + Navigator.of(context).pop(); + Navigator.push(context, MaterialPageRoute(builder: (context) => PartDetailWidget(part))); }); } else { // TODO - Show an error here! diff --git a/lib/main.dart b/lib/main.dart index 3609ed6b..16ab5d76 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -382,6 +382,7 @@ class _MyHomePageState extends State { ], ), Spacer(), + */ Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, @@ -404,7 +405,6 @@ class _MyHomePageState extends State { ), ], ), - */ ]), ), ),