diff --git a/lib/barcode.dart b/lib/barcode.dart new file mode 100644 index 00000000..0fa38c63 --- /dev/null +++ b/lib/barcode.dart @@ -0,0 +1,52 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:qr_utils/qr_utils.dart'; + +import 'package:InvenTree/inventree/stock.dart'; +import 'package:InvenTree/widget/location_display.dart'; + +import 'dart:convert'; + +void scanQrCode(BuildContext context) async { + + QrUtils.scanQR.then((String result) { + // Look for JSON data in the result... + final data = json.decode(result); + + final String tool = (data['tool'] ?? '').toString().toLowerCase(); + + // 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 { + showDialog( + context: context, + child: new SimpleDialog( + title: new Text("Unknown barcode"), + children: [ + Text("Data: $result"), + ] + ) + ); + } + + print("Scanned: $result"); + }); +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 22e3a299..daccd3dd 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,10 +1,13 @@ +import 'package:InvenTree/inventree/stock.dart'; import 'package:InvenTree/widget/category_display.dart'; import 'package:InvenTree/widget/location_display.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:qr_utils/qr_utils.dart'; +import 'barcode.dart'; + +import 'dart:convert'; import 'settings.dart'; import 'api.dart'; @@ -115,9 +118,7 @@ class _MyHomePageState extends State { } void _scanCode() async { - QrUtils.scanQR.then((String result) { - print("Scanned: $result"); - }); + scanQrCode(context); } void _showStock() {