Files
logistics_tool/lib/order_list.dart

313 lines
16 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-20 10:06:05 +08:00
import 'package:path/path.dart' as p;
import 'package:url_launcher/url_launcher.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,
2024-06-20 10:06:05 +08:00
child: Scrollbar(
controller: scrollController,
child: SingleChildScrollView(
controller: scrollController,
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(12.0),
child: DataTable(
columns: _headers.map((el) {
return DataColumn(label: Text(el));
}).toList(),
rows: _data.asMap().entries.map((row) {
return DataRow(
cells: row.value.asMap().entries.map((col) {
String colTitle = _headers[col.key];
return DataCell(colTitle == "物流名称"
2024-06-18 16:52:17 +08:00
? Row(
children: [
2024-06-20 10:06:05 +08:00
Text("${col.value}"),
IconButton(
// padding: EdgeInsets.all(2),
iconSize: 12,
onPressed: () {
_logComController.text =
_data[row.key][col.key]
.toString();
showDialog(
context: context,
builder:
(BuildContext
context) =>
AlertDialog(
// icon: const Icon(Icons.edit),
title: const Text(
"填写物流公司"),
content: Column(
mainAxisSize:
MainAxisSize
.min,
children: [
TextField(
decoration:
const InputDecoration(
labelText: "物流公司"),
controller:
_logComController,
)
],
),
actions: [
ElevatedButton(
onPressed:
() {
context
.pop();
},
child: const Text(
"取消")),
ElevatedButton(
onPressed:
() {
String
logisticsCom =
_logComController.text;
if (logisticsCom
.isEmpty) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("请输入物流公司")));
return;
}
final data =
_data;
_data[row.key][col.key] =
logisticsCom;
setState(
() {
_data =
data;
});
context
.pop();
},
child: const Text(
"确认")),
],
));
},
icon: const Icon(Icons.edit))
],
)
: colTitle == "物流信息"
? Row(
children: [
_data[row.key][col.key]
.toString()
.isNotEmpty
? SizedBox(
width: 120,
child: Text(
_data[row.key]
[col.key]
2024-06-18 16:52:17 +08:00
.toString(),
2024-06-20 10:06:05 +08:00
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("该行数据有误"),
showCloseIcon: true,
));
return;
}
context.pushNamed(
"logistics",
queryParameters: {
"orderNumber":
_data[row.key][2]
.toString(),
"orderPayWay":
_data[row.key][15]
.toString(),
"logistics":
_data[row.key][12]
.toString()
}).then((value) {
if (value != null) {
var newData = _data;
newData[row.key]
[col.key] = value;
setState(() {
_data = newData;
});
}
2024-06-18 16:52:17 +08:00
});
2024-06-20 10:06:05 +08:00
},
child: const Text("填写"),
)
],
2024-06-18 16:52:17 +08:00
)
2024-06-20 10:06:05 +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-20 10:06:05 +08:00
String fileName = "导出订单${DateTime.now().millisecondsSinceEpoch}.xlsx";
String? outputPath = await FilePicker.platform.saveFile(
dialogTitle: "选择保存位置",
fileName: fileName,
type: FileType.custom,
allowedExtensions: ["xlsx", "xls"]);
if (outputPath == null || outputPath.isEmpty) {
2024-06-18 16:52:17 +08:00
return;
}
2024-06-20 10:06:05 +08:00
File(outputPath)
2024-06-17 17:28:54 +08:00
..createSync(recursive: true)
..writeAsBytesSync(fileBytes!);
2024-06-18 16:52:17 +08:00
if (mounted) {
2024-06-20 10:06:05 +08:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text("导出文件成功"),
2024-06-18 16:52:17 +08:00
backgroundColor: Colors.green,
2024-06-20 10:06:05 +08:00
action: SnackBarAction(
label: "打开文件所在位置",
onPressed: () {
final Uri fileUrl = Uri(
scheme: "file",
path: p.dirname(outputPath),
);
launchUrl(fileUrl, mode: LaunchMode.externalApplication);
},
backgroundColor: Colors.white,
textColor: Colors.green,
),
2024-06-18 16:52:17 +08:00
));
}
2024-06-17 17:28:54 +08:00
}
2024-06-14 14:27:56 +08:00
}