import 'package:InvenTree/widget/dialogs.dart'; 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/inventree/part.dart'; import 'package:InvenTree/api.dart'; import 'package:InvenTree/widget/location_display.dart'; import 'package:InvenTree/widget/part_detail.dart'; import 'package:InvenTree/widget/category_display.dart'; import 'package:InvenTree/widget/stock_detail.dart'; import 'dart:convert'; Future scanQrCode(BuildContext context) async { QrUtils.scanQR.then((String result) { print("Scanned: $result"); // Look for JSON data in the result... final data = json.decode(result); showProgressDialog(context, "Querying Server", "Sending barcode data to server"); InvenTreeAPI().post("barcode/", body: data).then((var response) { // Close the progress dialog Navigator.pop(context); 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; } final Map body = json.decode(response.body); 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'] ?? ''}"), ) ], ) ); } 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()}"), ) ], ) ); } print("body: ${body.toString()}"); }); }); } void _handleBarcode(BuildContext context, Map data) { int id; // A stocklocation has been passed? if (data.containsKey('stocklocation')) { id = data['stocklocation']['id'] ?? null; if (id != null) { // Try to open a stock location... showProgressDialog(context, "Loading data", "Requesting stock location information from server"); InvenTreeStockLocation().get(id).then((var loc) { hideProgressDialog(context); if (loc is InvenTreeStockLocation) { Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(loc))); } }); } } else if (data.containsKey('stockitem')) { id = data['stockitem']['id'] ?? null; if (id != null) { showProgressDialog(context, "Loading data", "Requesting stock item information from server"); InvenTreeStockItem().get(id).then((var item) { hideProgressDialog(context); Navigator.push(context, MaterialPageRoute(builder: (context) => StockDetailWidget(item))); }); } } else if (data.containsKey('part')) { id = data['part']['id'] ?? null; if (id != null) { showProgressDialog(context, "Loading data", "Requesting part information from server"); InvenTreePart().get(id).then((var part) { hideProgressDialog(context); Navigator.push(context, MaterialPageRoute(builder: (context) => PartDetailWidget(part))); }); } } else { showDialog( context: context, child: SimpleDialog( title: Text("Unknown response"), children: [ ListTile( title: Text("Response data"), subtitle: Text(data.toString()), ) ], ) ); } }