From d57e2033dc353c2c754225d87af543a6bedb81ad Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 3 Apr 2020 22:47:03 +1100 Subject: [PATCH] Further support for QR decoding - StockItem - Part --- lib/barcode.dart | 64 +++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/lib/barcode.dart b/lib/barcode.dart index 0fa38c63..7716d7de 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -3,39 +3,31 @@ import 'package:flutter/material.dart'; import 'package:qr_utils/qr_utils.dart'; import 'package:InvenTree/inventree/stock.dart'; +import 'package:InvenTree/inventree/part.dart'; + import 'package:InvenTree/widget/location_display.dart'; +import 'package:InvenTree/widget/part_display.dart'; +import 'package:InvenTree/widget/category_display.dart'; +import 'package:InvenTree/widget/stock_display.dart'; import 'dart:convert'; void scanQrCode(BuildContext context) async { QrUtils.scanQR.then((String result) { + + print("Scanned: $result"); + // Look for JSON data in the result... final data = json.decode(result); - final String tool = (data['tool'] ?? '').toString().toLowerCase(); + // Look for an 'InvenTree' style barcode + if ((data['tool'] ?? '').toString().toLowerCase() == 'inventree') { + _handleInvenTreeBarcode(context, data); + } - // This looks like an InvenTree QR code! - if (tool == 'inventree') { - final String codeType = (data['type'] ?? '').toString().toLowerCase(); - - final int pk = (data['id'] ?? -1) as int; - - if (codeType == 'stocklocation') { - - // Try to open a stock location... - InvenTreeStockLocation().get(pk).then((var loc) { - if (loc is InvenTreeStockLocation) { - Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(loc))); - } - }); - - } else if (codeType == 'stockitem') { - - } - - - } else { + // Unknown barcode style! + else { showDialog( context: context, child: new SimpleDialog( @@ -47,6 +39,32 @@ void scanQrCode(BuildContext context) async { ); } - print("Scanned: $result"); }); +} + +void _handleInvenTreeBarcode(BuildContext context, Map data) { + + final String codeType = (data['type'] ?? '').toString().toLowerCase(); + + final int pk = (data['id'] ?? -1) as int; + + if (codeType == 'stocklocation') { + + // Try to open a stock location... + InvenTreeStockLocation().get(pk).then((var loc) { + if (loc is InvenTreeStockLocation) { + Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(loc))); + } + }); + + } else if (codeType == 'stockitem') { + InvenTreeStockItem().get(pk).then((var item) { + Navigator.push(context, MaterialPageRoute(builder: (context) => StockItemDisplayWidget(item))); + }); + } else if (codeType == 'part') { + InvenTreePart().get(pk).then((var part) { + Navigator.push(context, + MaterialPageRoute(builder: (context) => PartDisplayWidget(part))); + }); + } } \ No newline at end of file