This commit is contained in:
quantulr
2024-06-18 16:52:17 +08:00
parent e0701ba823
commit d2091a90dd
4 changed files with 659 additions and 99 deletions

View File

@ -2,10 +2,9 @@ import 'dart:io';
import 'package:excel/excel.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:path_provider/path_provider.dart';
class OrderList extends StatefulWidget {
const OrderList({super.key});
@ -16,8 +15,8 @@ class OrderList extends StatefulWidget {
class _OrderListState extends State<OrderList> {
final List lst = [];
List<String> headers = [];
List<List<dynamic>> data = [];
List<String> _headers = [];
List<List<dynamic>> _data = [];
late Excel _excel;
ScrollController scrollController = ScrollController();
final TextEditingController _logComController =
@ -28,45 +27,54 @@ class _OrderListState extends State<OrderList> {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: const Text("test"),
title: const Text("订单列表"),
actions: [
IconButton(
ElevatedButton(
onPressed: () {
importExcelFile();
},
icon: const Icon(Icons.add_circle)),
IconButton(
child: const Row(
children: [Icon(Icons.add_circle), Text("导入")],
)),
const SizedBox(
width: 4,
),
ElevatedButton(
onPressed: () {
exportExcelFile();
},
icon: const Icon(Icons.output))
child: const Row(
children: [Icon(CupertinoIcons.download_circle), Text("导出")],
)),
const SizedBox(
width: 4,
),
],
),
body: Container(
width: double.infinity,
// color: Colors.white,
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Card(
color: Colors.white,
margin: EdgeInsets.zero,
child: headers.isNotEmpty
? /*Scrollbar(
child: _headers.isNotEmpty
? Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Card(
color: Colors.white,
margin: EdgeInsets.zero,
child: /*Scrollbar(
// controller: scrollController,
child:*/
SingleChildScrollView(
SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(12.0),
child: DataTable(
columns: headers.map((el) {
columns: _headers.map((el) {
return DataColumn(label: Text(el));
}).toList(),
rows: data.asMap().entries.map((row) {
rows: _data.asMap().entries.map((row) {
return DataRow(
cells: row.value.asMap().entries.map((col) {
String colTitle = headers[col.key];
String colTitle = _headers[col.key];
return DataCell(colTitle == "物流名称"
? Row(
children: [
@ -76,7 +84,7 @@ class _OrderListState extends State<OrderList> {
iconSize: 12,
onPressed: () {
_logComController.text =
data[row.key][col.key]
_data[row.key][col.key]
.toString();
showDialog(
context: context,
@ -122,13 +130,13 @@ class _OrderListState extends State<OrderList> {
Text("请输入物流公司")));
return;
}
final _data =
data;
data[row.key][col
.key] =
final data =
_data;
_data[row.key][
col.key] =
logisticsCom;
setState(() {
data = _data;
_data = data;
});
context.pop();
},
@ -141,58 +149,91 @@ class _OrderListState extends State<OrderList> {
],
)
: colTitle == "物流信息"
? 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(),
? 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;
});
}
});
},
child: const Text("填写"),
},
child: const Text("填写"),
)
],
)
: Text("${col.value}"));
}).toList());
}).toList(),
),
)
/*)*/
: const Text("empty"),
)
],
),
/*)*/
,
)
],
)
: const Center(
child: Icon(CupertinoIcons.cloud_upload),
),
));
}
Future<String> importExcelFile() async {
FilePickerResult? result = await FilePicker.platform
.pickFiles(type: FileType.custom, allowedExtensions: ["xlsx", "xls"]);
if (result != null) {
File file = File(result.files.single.path!);
var bytes = file.readAsBytesSync();
Excel excel = Excel.decodeBytes(bytes);
_excel = excel;
String tableKey = excel.tables.keys.first;
List<String> _headers = excel.tables[tableKey]?.rows.first.map((el) {
List<String> headers = excel.tables[tableKey]?.rows.first.map((el) {
return el?.value.toString() ?? "";
}).toList() ??
[];
List<List<dynamic>> _data = excel.tables[tableKey]?.rows
List<List<dynamic>> data = excel.tables[tableKey]?.rows
.where((el) => (el.first?.value?.toString() ?? "").isNotEmpty)
.toList()
.sublist(1)
@ -203,8 +244,8 @@ class _OrderListState extends State<OrderList> {
}).toList() ??
[];
setState(() {
headers = _headers;
data = _data;
_headers = headers;
_data = data;
});
} else {
// 取消选择
@ -213,7 +254,7 @@ class _OrderListState extends State<OrderList> {
}
exportExcelFile() async {
data.asMap().entries.forEach((row) {
_data.asMap().entries.forEach((row) {
row.value.asMap().entries.forEach((col) {
int rowIndex = row.key + 1;
int colIndex = col.key;
@ -227,14 +268,19 @@ class _OrderListState extends State<OrderList> {
});
});
var fileBytes = _excel.save();
// var directory = await getApplicationDocumentsDirectory();
File(
"C:\\Users\\ayaya\\Desktop\\导出订单${DateTime.now().millisecondsSinceEpoch}.xlsx")
String? directory =
await FilePicker.platform.getDirectoryPath(dialogTitle: "选择保存位置");
if (directory == null || directory.isEmpty) {
return;
}
File("$directory\\导出订单${DateTime.now().millisecondsSinceEpoch}.xlsx")
..createSync(recursive: true)
..writeAsBytesSync(fileBytes!);
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("导出文件成功"),
backgroundColor: Colors.green,
));
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("导出文件成功"),
backgroundColor: Colors.green,
));
}
}
}