import 'dart:convert'; import 'package:bruno/bruno.dart'; import 'package:flutter/material.dart'; import 'package:tdesign_flutter/tdesign_flutter.dart'; import 'package:wms_app/apiclient/stock_in.dart'; import '../../common/colorCom.dart'; import '../../utils/dialogUtils.dart'; import '../../utils/stringUtils.dart'; class StockInNormal extends StatefulWidget { const StockInNormal({super.key}); @override State createState() => _StockInNormalState(); } /// 通用普通码盘界面 class _StockInNormalState extends State { final _vehicleTextController = TextEditingController(); // 载具号输入框 final _goodsCodeController = TextEditingController(text: "5312452695,30601000080,24B7251524,1,0.481,20241128"); // 条码输入框 List packageData = []; // 已经码盘的数据 int packageDataId = 0; // 已经码盘的数据的序号 @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( iconTheme: const IconThemeData( color: Colors.white ), leading: IconButton(onPressed: () { Navigator.of(context).pop(); }, icon: const Icon(Icons.arrow_back)), centerTitle: true, backgroundColor: ColorCommon.colorScheme, title: const Text( "单机码盘入库", style: TextStyle( color: Colors.white ), ), ), body: Center( child: Padding(padding: const EdgeInsets.only(top: 5, left: 10, right: 10), child: ListView( children: [ BrnTextInputFormItem( controller: _vehicleTextController, title: "载具号:", hint: "请扫描或输入", isRequire: true, ), BrnTextInputFormItem( controller: _goodsCodeController, title: "条码:", hint: "请扫描物料二维码", isRequire: true, ), Padding(padding: const EdgeInsets.only( top: 5 ), child: ElevatedButton( onPressed: resolveCode, style: ButtonStyle( backgroundColor: WidgetStateProperty.all(ColorCommon.colorScheme), ), child: const Text( "添加物料", style: TextStyle( color: Colors.white )) )), Padding(padding: const EdgeInsets.only( top: 0 ), child: ElevatedButton( onPressed: wheelComplete, style: ButtonStyle( backgroundColor: WidgetStateProperty.all(ColorCommon.colorScheme), ), child: const Text( "码盘完成", style: TextStyle( color: Colors.white )) )), const Padding(padding: EdgeInsets.only( top: 10, bottom: 10 ), child: Text("添加在载具的物料:"), ), Container( decoration: BoxDecoration( border: Border.all(color: const Color(0x4D0C0C05), width: 0.3),// border borderRadius: BorderRadius.circular((5)), // 圆角 ), child: TDTable( bordered: true, width: MediaQuery.of(context).size.width, backgroundColor: Colors.transparent, columns: [ TDTableCol( title: '*', colKey: 'action', width: 45, align: TDTableColAlign.center, cellBuilder: (BuildContext context) { return const SizedBox( child: Icon(TDIcons.delete, color: Colors.redAccent, size: 10), ); }, ), TDTableCol(title: '序号', colKey: 'id', align: TDTableColAlign.center, width: 80), TDTableCol(title: '采购单号', colKey: 'segment1', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100), TDTableCol(title: '物料号', colKey: 'itemId', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100), TDTableCol(title: '批次号', colKey: 'batch', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100), TDTableCol(title: '数量', colKey: 'quantity', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100), TDTableCol(title: '重量', colKey: 'weight', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100), TDTableCol(title: '生产日期', colKey: 'productData', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100), //TDTableCol(title: '有效日期', colKey: 'deadLineData', align: TDTableColAlign.center, ellipsis: true, ellipsisTitle: true, width: 100) ], data: packageData, onCellTap: (index, dynamic, cell) { clickLine(index, dynamic, cell); }), ), ], )), ), ); } /// 解析条码 void resolveCode() { String code = _goodsCodeController.text; if(StringUtils.isEmpty(code)) { DialogUtils.showWarningMessage(context, "警告", "条码文本框内无数据,请先扫描或者输入数据"); return; } List codeData = code.split(","); if(codeData.length != 6) { DialogUtils.showWarningMessage(context, "警告", "条码格式错误"); return; } setState(() { packageDataId ++; packageData.add({ "id": packageDataId.toString(), "segment1": codeData[0], "itemId": codeData[1], "batch": codeData[2], "quantity": codeData[3], "weight": codeData[4], "productData": codeData[5] }); _goodsCodeController.clear(); }); return; } /// 码盘完成 void wheelComplete() { if(packageData.isEmpty){ DialogUtils.showWarningMessage(context, "警告", "您的码盘数据为空", btnLabel: "确定"); return; } String vehicleNo = _vehicleTextController.text; if(StringUtils.isEmpty(vehicleNo)) { DialogUtils.showWarningMessage(context, "警告", "请先扫描载具号", btnLabel: "返回填写"); return; } int dataCount = packageData.length; DialogUtils.showConfirmMessage(context, "码盘完成", "载具:$vehicleNo 码盘 $dataCount 条数据,是否继续?", confirmBtn: "继续", confirm: () { int taskType = 1; // 1 表示进库,2 表示进站台 BrnLoadingDialog.show(context, content: "正在请求入库"); StockIn.stockInComplete({ "vehicleNo": vehicleNo, "inArea": taskType.toString(), "storageArea" : "", "goods": packageData }).then((response) { if (response["code"] != 200) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showWarningMessage( thisContext, "警告", "服务器请求失败", btnLabel: "我知道了"); } return; } final data = Map.from(jsonDecode(response["data"])); if (data["code"] == 200) { // 请求成功 setState(() { _vehicleTextController.clear(); packageData = []; }); var thisContext = context; if (thisContext.mounted) { DialogUtils.showSuccessMessage( thisContext, "码盘成功", "", btnLabel: "我知道了"); } return; } var thisContext = context; if (thisContext.mounted) { DialogUtils.showWarningMessage( thisContext, "警告", "服务器返回失败:${data["message"]}", btnLabel: "我知道了"); } return; }).catchError((err) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showErrorMessage(thisContext, "请求发生错误", "请求服务器发生错误:${err.toString()}", btnLabel: "我知道了"); } return; }).whenComplete(() { var thisContext = context; if (thisContext.mounted) { BrnLoadingDialog.dismiss(thisContext); } }); }); } /// 行数据点击事件 void clickLine(index, dynamic, cell) { if(cell.colKey == "action") { // 点击删除 delete(dynamic); return; } if(cell.colKey == "quantity") { // 点击数量 modifyNumber(index, dynamic); return; } showDetails(index, dynamic); } /// 删除已经码盘的物料 void delete(dynamic) { setState(() { packageData.removeWhere((r){ return r["id"] == dynamic["id"]; }); }); if(packageData.isEmpty) { packageDataId = 0; } } /// 修改数量 void modifyNumber(index, dynamic) { DialogUtils.showInputMessage(context, "请输入要修改的数量", message: "仅支持数字", confirm: (value) { if(!StringUtils.isNumber(value)) { DialogUtils.showWarningMessage(context, "警告", "该文本框仅支持数字"); return; } setState((){ packageData.where((w)=>w["id"] == dynamic["id"]).first["quantity"] = value; }); }); } /// 显示详细信息 void showDetails(index, tableRow) { List message = []; message.add({"label":"序号:", "msg":tableRow["id"].toString()}); message.add({"label":"采购单号:", "msg":tableRow["segment1"].toString()}); message.add({"label":"物料号:", "msg":tableRow["itemId"].toString()}); message.add({"label":"批次号:", "msg":tableRow["batch"].toString()}); message.add({"label":"数量:", "msg":tableRow["quantity"].toString()}); message.add({"label":"重量:", "msg":tableRow["weight"].toString()}); message.add({"label":"生产日期:", "msg":tableRow["productData"].toString()}); DialogUtils.showMessageList(context, "数据详情", message, btnLabel: "我知道了"); } }