XuGongTeJi_flutter/lib/page/stockIn/stock_in_buy.dart
2025-02-20 13:23:50 +08:00

282 lines
9.2 KiB
Dart

import 'dart:convert';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:wms_app/api_client/stock_in.dart';
import 'package:wms_app/model/bo/stock_in_data_xugong.dart';
import '../../common/colorCom.dart';
import '../../utils/dialogUtils.dart';
class StockInBuy extends StatefulWidget {
const StockInBuy({super.key});
@override
State<StockInBuy> createState() => _StockInBuyState();
}
class _StockInBuyState extends State<StockInBuy> {
List<StockInDataXuGong> stockInDataXuGong = [];
@override
void initState() {
super.initState();
_fetchOrderData();
}
void _fetchOrderData() {
Future.delayed(Duration(seconds: 1), () {
String orderNo = "order123";
_fetchGoodsData(orderNo);
});
}
void _fetchGoodsData(String orderNo) {
Future.delayed(Duration(seconds: 1), () {
setState(() {
stockInDataXuGong = [
StockInDataXuGong(
goodsId: "001",
goodsNum: 5,
orderNo: orderNo,
orderType: "订购",
customerName: "Customer A",
goodsDesc: "Material 1 Description",
weight: 20.0,
size: "Small",
spare1: "Spare1A",
spare2: "Spare2A",
containerNo: "Container123"),
StockInDataXuGong(
goodsId: "002",
goodsNum: 10,
orderNo: orderNo,
orderType: "订购",
customerName: "Customer B",
goodsDesc: "Material 2 Description",
weight: 15.5,
size: "Medium",
spare1: "Spare1B",
spare2: "Spare2B",
containerNo: "Container456"),
];
});
});
}
@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: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: stockInDataXuGong.length,
itemBuilder: (context, index) {
final item = stockInDataXuGong[index];
return Card(
margin: const EdgeInsets.symmetric(vertical: 5),
child: ListTile(
onTap: () => _editGood(item),
title: Row(
children: [
Expanded(child: Text("物料号: ${item.goodsId}")),
Expanded(child: Text("数量: ${item.goodsNum}")),
Expanded(child: Text("描述: ${item.goodsDesc}")),
Expanded(child: Text("重量: ${item.weight} KG")),
Expanded(child: Text("尺寸: ${item.size}")),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
_addMaterial(item);
},
),
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
_editGood(item);
},
),
],
),
),
);
},
),
),
Padding(
padding: const EdgeInsets.all(10),
child: ElevatedButton(
onPressed: _completeStockIn,
child: const Text("码盘完成"),
),
),
],
),
),
),
);
}
void _editGood(StockInDataXuGong item) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditGoodPage(item: item, onSave: _updateGood),
),
);
}
void _updateGood(StockInDataXuGong updatedItem) {
setState(() {
final index =
stockInDataXuGong.indexWhere((e) => e.goodsId == updatedItem.goodsId);
if (index != -1) {
stockInDataXuGong[index] = updatedItem;
}
});
}
void _addMaterial(StockInDataXuGong item) {
DialogUtils.showSuccessMessage(context, "添加物料成功", "", btnLabel: "我知道了");
}
void _completeStockIn() {
if (stockInDataXuGong.isEmpty) {
DialogUtils.showWarningMessage(context, "警告", "您的码盘数据为空", btnLabel: "确定");
return;
}
DialogUtils.showConfirmMessage(context, "码盘完成", "是否完成入库?", confirmBtn: "完成",
confirm: () {
BrnLoadingDialog.show(context, content: "正在请求入库");
StockInApi.stockInComplete({
"vehicleNo":
"",
"inArea": "1",
"storageArea": "",
"goods": stockInDataXuGong,
}).then((response) {
if (response["code"] != 200) {
DialogUtils.showWarningMessage(context, "警告", "服务器请求失败",
btnLabel: "我知道了");
return;
}
final data = Map<String, dynamic>.from(jsonDecode(response["data"]));
if (data["code"] == 200) {
setState(() {
stockInDataXuGong.clear();
});
DialogUtils.showSuccessMessage(context, "码盘成功", "", btnLabel: "我知道了");
} else {
DialogUtils.showWarningMessage(
context, "警告", "服务器返回失败:${data["message"]}",
btnLabel: "我知道了");
}
}).catchError((err) {
DialogUtils.showErrorMessage(
context, "请求发生错误", "请求服务器发生错误:${err.toString()}",
btnLabel: "我知道了");
}).whenComplete(() {
BrnLoadingDialog.dismiss(context);
});
});
}
}
class EditGoodPage extends StatefulWidget {
final StockInDataXuGong item;
final Function(StockInDataXuGong) onSave;
const EditGoodPage({super.key, required this.item, required this.onSave});
@override
_EditGoodPageState createState() => _EditGoodPageState();
}
class _EditGoodPageState extends State<EditGoodPage> {
late StockInDataXuGong _editedItem;
@override
void initState() {
super.initState();
_editedItem = widget.item;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("编辑物料"),
backgroundColor: ColorCommon.colorScheme,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(10),
child: Column(
children: [
_buildEditableField("物料号", _editedItem.goodsId,
(value) => _editedItem.goodsId = value),
_buildEditableField("数量", _editedItem.goodsNum.toString(),
(value) => _editedItem.goodsNum = double.tryParse(value) ?? 0),
_buildEditableField("单据号", _editedItem.orderNo,
(value) => _editedItem.orderNo = value),
_buildEditableField("单据类型", _editedItem.orderType,
(value) => _editedItem.orderType = value),
_buildEditableField("客户名称", _editedItem.customerName,
(value) => _editedItem.customerName = value),
_buildEditableField("描述", _editedItem.goodsDesc,
(value) => _editedItem.goodsDesc = value),
_buildEditableField("重量", _editedItem.weight.toString(),
(value) => _editedItem.weight = double.tryParse(value) ?? 0.0),
_buildEditableField(
"尺寸", _editedItem.size, (value) => _editedItem.size = value),
_buildEditableField("备用1", _editedItem.spare1,
(value) => _editedItem.spare1 = value),
_buildEditableField("备用2", _editedItem.spare2,
(value) => _editedItem.spare2 = value),
_buildEditableField("容器号", _editedItem.containerNo,
(value) => _editedItem.containerNo = value),
ElevatedButton(
onPressed: () {
widget.onSave(_editedItem);
Navigator.pop(context);
},
child: const Text("保存修改"),
),
],
),
),
);
}
Widget _buildEditableField(
String title, String initialValue, Function(String) onChanged) {
TextEditingController controller =
TextEditingController(text: initialValue);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: TextField(
controller: controller,
decoration: InputDecoration(labelText: title),
onChanged: onChanged,
keyboardType: TextInputType.text,
),
);
}
}