import 'dart:convert'; import 'dart:ffi'; import 'package:bruno/bruno.dart'; import 'package:flutter/material.dart'; import 'package:uuid/uuid.dart'; import 'package:wms_app/api_client/stock_in.dart'; import 'package:wms_app/component/card/stock_in_card_xugong.dart'; import 'package:wms_app/model/bo/stock_in_data_xugong.dart'; import '../../common/colorCom.dart'; import '../../utils/dialogUtils.dart'; class StockInNormal extends StatefulWidget { const StockInNormal({super.key}); @override State createState() => _StockInNormalState(); } /// 通用普通码盘界面 class _StockInNormalState extends State { Uuid uuidGen = Uuid(); final _vehicleTextController = TextEditingController(); // 载具号输入框 final _goodsCodeController = TextEditingController(); // 条码输入框 final _goodsNumController = BrnStepsController(); final _listIdController = TextEditingController(); final _orderTypeController = TextEditingController(); final _customerNameController = TextEditingController(); final _goodsDescController = TextEditingController(); final _goodsUnitController = TextEditingController(); final _weightController = TextEditingController(); final _sizeController = TextEditingController(); final _spare1Controller = TextEditingController(); final _spare2Controller = TextEditingController(); int goodsNum = 1; // 物料数量 List stockInDataXuGong = []; @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), ), actions: [ TextButton( onPressed: () {}, style: const ButtonStyle( foregroundColor: WidgetStatePropertyAll(Colors.white)), child: const Text("清空"), ) ], ), body: Center( child: Padding( padding: const EdgeInsets.only(top: 5, left: 10, right: 10), child: ListView( children: [ BrnTextInputFormItem( controller: _goodsCodeController, title: "物料号:", hint: "请输入或扫描物料号", isRequire: true, ), BrnStepInputFormItem( title: "物料数量:", minLimit: 1, value: goodsNum, maxLimit: 9999, isRequire: true, canManualInput: true, onChanged: (newValue, _) { setState(() { goodsNum = newValue; }); }), BrnTextInputFormItem( controller: _listIdController, title: "单据号:", hint: "请输入或扫描单据号", isRequire: true, ), BrnTextInputFormItem( controller: _orderTypeController, title: "单据类型:", hint: "请输入或扫描单据类型", isRequire: true, ), BrnTextInputFormItem( controller: _customerNameController, title: "客户名称:", hint: "请输入或扫描客户名称", isRequire: true, ), BrnTextInputFormItem( controller: _goodsDescController, title: "物料描述:", hint: "请输入或扫描物料描述", isRequire: true, ), BrnTextInputFormItem( controller: _goodsUnitController, title: "物料单位:", hint: "请输入物料单位", isRequire: true, ), BrnTextInputFormItem( controller: _weightController, title: "重量:", hint: "仅支持数字(保留小数点四位)", isRequire: true, unit: "KG", ), BrnTextInputFormItem( controller: _sizeController, title: "尺寸:", hint: "请输入或扫描尺寸", isRequire: true, ), BrnTextInputFormItem( controller: _spare1Controller, title: "备用1:", hint: "请输入", isRequire: true, ), BrnTextInputFormItem( controller: _spare2Controller, title: "备用2:", hint: "请输入", isRequire: true, ), ElevatedButton( onPressed: resolveCode, style: ButtonStyle( backgroundColor: WidgetStateProperty.all(ColorCommon.colorScheme), ), child: const Text("添加物料", style: TextStyle(color: Colors.white))), BrnTextInputFormItem( controller: _vehicleTextController, title: "容器号:", hint: "", isRequire: true, ), 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("添加在载具的物料:"), ), StockInCardXuGong(stockInDataXuGong: stockInDataXuGong) ], )), ), ); } /// 解析条码 void resolveCode() { setState(() { stockInDataXuGong.add(StockInDataXuGong( customerName: _customerNameController.text, weight: double.parse(_weightController.text), size: int.parse(_sizeController.text), unit: _goodsUnitController.text, listId: _listIdController.text, orderType: int.parse(_orderTypeController.text), goodsNum: goodsNum, goodsId: _goodsCodeController.text, goodsDesc: _goodsDescController.text, spare1: _spare1Controller.text, spare2: _spare2Controller.text)); }); return; } void wheelComplete() { if (stockInDataXuGong.isEmpty) { DialogUtils.showWarningMessage( context, "警告", "您的码盘数据为空", btnLabel: "确定"); return; } String vehicleNo = _vehicleTextController.text; int dataCount = stockInDataXuGong.length; DialogUtils.showConfirmMessage( context, "码盘完成", "载具:$vehicleNo 码盘 $dataCount 条数据,是否继续?", confirmBtn: "继续", confirm: () { int taskType = 1; // 1 表示进库,2 表示进站台 BrnLoadingDialog.show(context, content: "正在请求入库"); _stockInComplete(vehicleNo).then((isTaskSuccess) { _dismissLoading(); if (isTaskSuccess) { _showSuccessDialog("入库成功"); _clearData(); } else { _showWarningDialog("创建任务失败"); } }).catchError((err) { _dismissLoading(); _handleError(err); }); }); } // 完成入库请求 Future _stockInComplete(String vehicleNo) async { try { var response = await StockInApi.stockInComplete( stockInDataXuGong, vehicleNo); if (response["code"] != 200) { _showWarningDialog("服务器请求失败"); return false; } return true; } catch (err) { _handleError(err); return false; } } // 显示错误提示 void _handleError(dynamic err) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showErrorMessage( thisContext, "请求发生错误", "请求服务器发生错误:${err.toString()}", btnLabel: "我知道了" ); } } // 显示成功提示 void _showSuccessDialog(String message) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showSuccessMessage( thisContext, message, "", btnLabel: "我知道了" ); } } // 显示警告提示 void _showWarningDialog(String message) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showWarningMessage( thisContext, "警告", message, btnLabel: "我知道了" ); } } // 清除数据 void _clearData() { setState(() { _vehicleTextController.clear(); stockInDataXuGong = []; }); } // 隐藏加载对话框 void _dismissLoading() { var thisContext = context; if (thisContext.mounted) { BrnLoadingDialog.dismiss(thisContext); } } }