Files
logistics_tool/lib/order_list.dart

287 lines
14 KiB
Dart
Raw Normal View History

2024-06-14 14:27:56 +08:00
import 'dart:io';
import 'package:excel/excel.dart';
import 'package:file_picker/file_picker.dart';
2024-06-18 16:52:17 +08:00
import 'package:flutter/cupertino.dart';
2024-06-14 14:27:56 +08:00
import 'package:flutter/material.dart';
2024-06-14 17:22:52 +08:00
import 'package:go_router/go_router.dart';
2024-06-14 14:27:56 +08:00
class OrderList extends StatefulWidget {
const OrderList({super.key});
@override
State<OrderList> createState() => _OrderListState();
}
class _OrderListState extends State<OrderList> {
final List lst = [];
2024-06-18 16:52:17 +08:00
List<String> _headers = [];
List<List<dynamic>> _data = [];
2024-06-17 17:28:54 +08:00
late Excel _excel;
2024-06-14 14:27:56 +08:00
ScrollController scrollController = ScrollController();
2024-06-17 17:28:54 +08:00
final TextEditingController _logComController =
TextEditingController(text: "");
2024-06-14 14:27:56 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
2024-06-18 16:52:17 +08:00
title: const Text("订单列表"),
2024-06-14 14:27:56 +08:00
actions: [
2024-06-18 16:52:17 +08:00
ElevatedButton(
2024-06-14 14:27:56 +08:00
onPressed: () {
importExcelFile();
},
2024-06-18 16:52:17 +08:00
child: const Row(
children: [Icon(Icons.add_circle), Text("导入")],
)),
const SizedBox(
width: 4,
),
ElevatedButton(
2024-06-17 17:28:54 +08:00
onPressed: () {
exportExcelFile();
},
2024-06-18 16:52:17 +08:00
child: const Row(
children: [Icon(CupertinoIcons.download_circle), Text("导出")],
)),
const SizedBox(
width: 4,
),
2024-06-14 14:27:56 +08:00
],
),
body: Container(
width: double.infinity,
padding: const EdgeInsets.all(20),
2024-06-18 16:52:17 +08:00
child: _headers.isNotEmpty
? Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Card(
color: Colors.white,
margin: EdgeInsets.zero,
child: /*Scrollbar(
2024-06-14 17:22:52 +08:00
// controller: scrollController,
child:*/
2024-06-18 16:52:17 +08:00
SingleChildScrollView(
2024-06-14 17:22:52 +08:00
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(12.0),
child: DataTable(
2024-06-18 16:52:17 +08:00
columns: _headers.map((el) {
2024-06-14 17:22:52 +08:00
return DataColumn(label: Text(el));
}).toList(),
2024-06-18 16:52:17 +08:00
rows: _data.asMap().entries.map((row) {
2024-06-14 17:22:52 +08:00
return DataRow(
2024-06-17 17:28:54 +08:00
cells: row.value.asMap().entries.map((col) {
2024-06-18 16:52:17 +08:00
String colTitle = _headers[col.key];
2024-06-17 17:28:54 +08:00
return DataCell(colTitle == "物流名称"
2024-06-14 17:22:52 +08:00
? Row(
children: [
Text("${col.value}"),
IconButton(
// padding: EdgeInsets.all(2),
iconSize: 12,
onPressed: () {
2024-06-17 17:28:54 +08:00
_logComController.text =
2024-06-18 16:52:17 +08:00
_data[row.key][col.key]
2024-06-17 17:28:54 +08:00
.toString();
2024-06-14 17:22:52 +08:00
showDialog(
context: context,
builder: (BuildContext
context) =>
AlertDialog(
// icon: const Icon(Icons.edit),
title: const Text(
"填写物流公司"),
2024-06-17 17:28:54 +08:00
content: Column(
2024-06-14 17:22:52 +08:00
mainAxisSize:
MainAxisSize.min,
children: [
TextField(
decoration:
2024-06-17 17:28:54 +08:00
const InputDecoration(
2024-06-14 17:22:52 +08:00
labelText:
"物流公司"),
2024-06-17 17:28:54 +08:00
controller:
_logComController,
2024-06-14 17:22:52 +08:00
)
],
),
actions: [
ElevatedButton(
2024-06-17 17:28:54 +08:00
onPressed: () {
context.pop();
},
2024-06-14 17:22:52 +08:00
child: const Text(
"取消")),
ElevatedButton(
2024-06-17 17:28:54 +08:00
onPressed: () {
String
logisticsCom =
_logComController
.text;
if (logisticsCom
.isEmpty) {
ScaffoldMessenger.of(
context)
.showSnackBar(const SnackBar(
content:
Text("请输入物流公司")));
return;
}
2024-06-18 16:52:17 +08:00
final data =
_data;
_data[row.key][
col.key] =
2024-06-17 17:28:54 +08:00
logisticsCom;
setState(() {
2024-06-18 16:52:17 +08:00
_data = data;
2024-06-17 17:28:54 +08:00
});
context.pop();
},
2024-06-14 17:22:52 +08:00
child: const Text(
"确认")),
],
));
},
icon: const Icon(Icons.edit))
],
)
2024-06-17 17:28:54 +08:00
: colTitle == "物流信息"
2024-06-18 16:52:17 +08:00
? Row(
children: [
_data[row.key][col.key]
.toString()
.isNotEmpty
? SizedBox(
width: 120,
child: Text(
_data[row.key][col.key].toString(),
overflow:
TextOverflow.ellipsis,
),
)
: const SizedBox(),
ElevatedButton(
onPressed: () {
String orderNumber =
_data[row.key][2]
.toString();
String orderPayWay =
_data[row.key][15]
.toString();
if (orderPayWay.isEmpty ||
orderNumber.isEmpty) {
ScaffoldMessenger.of(context)
.showSnackBar(
const SnackBar(
content: Text(
"该行数据有误")));
return;
}
context.pushNamed("logistics",
queryParameters: {
"orderNumber":
_data[row.key][2]
.toString(),
"orderPayWay":
_data[row.key][15]
.toString(),
}).then((value) {
if (value != null) {
var newData = _data;
newData[row.key][col.key] =
value;
setState(() {
_data = newData;
});
}
2024-06-17 17:28:54 +08:00
});
2024-06-18 16:52:17 +08:00
},
child: const Text("填写"),
)
],
2024-06-14 17:22:52 +08:00
)
: Text("${col.value}"));
}).toList());
}).toList(),
),
)
2024-06-18 16:52:17 +08:00
/*)*/
,
)
],
)
: const Center(
child: Icon(CupertinoIcons.cloud_upload),
),
2024-06-14 14:27:56 +08:00
));
}
Future<String> importExcelFile() async {
FilePickerResult? result = await FilePicker.platform
.pickFiles(type: FileType.custom, allowedExtensions: ["xlsx", "xls"]);
2024-06-18 16:52:17 +08:00
2024-06-14 14:27:56 +08:00
if (result != null) {
File file = File(result.files.single.path!);
var bytes = file.readAsBytesSync();
Excel excel = Excel.decodeBytes(bytes);
2024-06-17 17:28:54 +08:00
_excel = excel;
2024-06-14 14:27:56 +08:00
String tableKey = excel.tables.keys.first;
2024-06-18 16:52:17 +08:00
List<String> headers = excel.tables[tableKey]?.rows.first.map((el) {
2024-06-14 14:27:56 +08:00
return el?.value.toString() ?? "";
}).toList() ??
[];
2024-06-18 16:52:17 +08:00
List<List<dynamic>> data = excel.tables[tableKey]?.rows
2024-06-17 17:28:54 +08:00
.where((el) => (el.first?.value?.toString() ?? "").isNotEmpty)
.toList()
.sublist(1)
.map((row) {
return row.map((col) {
return col?.value ?? "";
}).toList();
}).toList() ??
[];
2024-06-14 14:27:56 +08:00
setState(() {
2024-06-18 16:52:17 +08:00
_headers = headers;
_data = data;
2024-06-14 14:27:56 +08:00
});
} else {
// 取消选择
}
return "";
}
2024-06-17 17:28:54 +08:00
exportExcelFile() async {
2024-06-18 16:52:17 +08:00
_data.asMap().entries.forEach((row) {
2024-06-17 17:28:54 +08:00
row.value.asMap().entries.forEach((col) {
int rowIndex = row.key + 1;
int colIndex = col.key;
if (colIndex == 11 || colIndex == 12) {
_excel.updateCell(
"订单列表",
CellIndex.indexByColumnRow(
columnIndex: colIndex, rowIndex: rowIndex),
TextCellValue(col.value.toString()));
}
});
});
var fileBytes = _excel.save();
2024-06-18 16:52:17 +08:00
String? directory =
await FilePicker.platform.getDirectoryPath(dialogTitle: "选择保存位置");
if (directory == null || directory.isEmpty) {
return;
}
File("$directory\\导出订单${DateTime.now().millisecondsSinceEpoch}.xlsx")
2024-06-17 17:28:54 +08:00
..createSync(recursive: true)
..writeAsBytesSync(fileBytes!);
2024-06-18 16:52:17 +08:00
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("导出文件成功"),
backgroundColor: Colors.green,
));
}
2024-06-17 17:28:54 +08:00
}
2024-06-14 14:27:56 +08:00
}