import 'package:flutter/material.dart'; import '/common/colorCom.dart'; import 'package:wms_app/utils/dialogUtils.dart'; import 'package:wms_app/api_client/stock_out.dart'; import 'dart:convert'; import 'package:logger/logger.dart'; var logger = Logger( printer: PrettyPrinter(), ); class StockOutEmpty extends StatefulWidget { const StockOutEmpty({super.key}); @override State createState() => _CallEmptyCartPageState(); } class _CallEmptyCartPageState extends State { int availableVehiclesNum = 0; bool isDataLoaded = false; String goodsId = "0"; int selectedQuantity = 1; @override void initState() { super.initState(); _fetchAvailableVehicles(); } Future _fetchAvailableVehicles() async { // 只有在数据未加载过时才重新发起请求 if (isDataLoaded) return; await StockOutApi.getAvailVehicleList().then((response) { if (response["code"] != 200) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showWarningMessage(thisContext, "警告", "服务器请求失败", btnLabel: "我知道了"); } return; } logger.e("yuqili response ${response}"); final data = List.from(jsonDecode(response["data"])); logger.e("yuqili data ${data}"); setState(() { availableVehiclesNum = data.length; isDataLoaded = true; // 数据加载完毕,设置标志位 }); }).catchError((err) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showErrorMessage( thisContext, "请求发生错误", "请求服务器发生错误:${err.toString()}", btnLabel: "我知道了"); } }); } void callEmptyCart(int quantity) { // 业务逻辑,调用出库接口等 StockOutApi.addStockOut(goodsId, quantity, "空托盘出库").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) { DialogUtils.showSuccessMessage(context, "成功", "空托已呼叫", btnLabel: "我知道了"); return; } }).catchError((err) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showErrorMessage( thisContext, "请求发生错误", "请求服务器发生错误:${err.toString()}", btnLabel: "我知道了"); } }); } @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: FutureBuilder( future: _fetchAvailableVehicles(), // 使用FutureBuilder来等待获取所有车辆信息 builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Center(child: CircularProgressIndicator()); // 显示加载中 } else if (snapshot.hasError) { return Center( child: Text("请求失败: ${snapshot.error.toString()}"), ); } else { return Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.start, children: [ // 添加一个文本来提示用户选择空托数量 Text("呼叫空托数量: ", style: TextStyle(fontSize: 16)), // 下拉框 DropdownButton( value: selectedQuantity, items: List.generate(availableVehiclesNum, (index) { return DropdownMenuItem( value: index + 1, child: Text("${index + 1}"), ); }), onChanged: (value) { setState(() { selectedQuantity = value!; }); }, hint: Text("选择空托盘数量"), ), SizedBox(width: 20), // 添加一些空间 // 出库按钮 ElevatedButton( onPressed: () => callEmptyCart(selectedQuantity), style: ButtonStyle( backgroundColor: MaterialStateProperty.all( ColorCommon.colorScheme), ), child: Text("出库", style: TextStyle(color: Colors.white)), ), ], ), ], ), ); } }, ), ); } }