export xlsx
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:logistics_tools/utils/request.dart';
|
||||
|
||||
class Logistics extends StatefulWidget {
|
||||
const Logistics({super.key});
|
||||
@ -9,6 +11,30 @@ class Logistics extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LogisticsState extends State<Logistics> {
|
||||
List<dynamic> goodsList = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Future<Response> fetchGoodsList(BuildContext context) async {
|
||||
String? orderNumber =
|
||||
GoRouterState.of(context).uri.queryParameters["orderNumber"];
|
||||
String? orderPayWay =
|
||||
GoRouterState.of(context).uri.queryParameters["orderPayWay"];
|
||||
print(orderPayWay);
|
||||
if (orderNumber?.isEmpty ?? true) {
|
||||
context.pop();
|
||||
}
|
||||
Response response = await dio.get(
|
||||
"https://www.sanpinhuicai.com/wisdommining/api/order/goodsOfOrder",
|
||||
queryParameters: {"orderNumber": orderNumber});
|
||||
goodsList = response.data["value"];
|
||||
return response;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@ -16,27 +42,68 @@ class _LogisticsState extends State<Logistics> {
|
||||
title: const Text("填写物流信息"),
|
||||
),
|
||||
body: Container(
|
||||
padding: const EdgeInsets.all(40),
|
||||
width: double.infinity,
|
||||
child: Card(
|
||||
color: Colors.white,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(20),
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(8))),
|
||||
child: SizedBox(
|
||||
height: 50,
|
||||
width: 120,
|
||||
padding: const EdgeInsets.all(40),
|
||||
width: double.infinity,
|
||||
child: FutureBuilder(
|
||||
future: fetchGoodsList(context),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Response> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return Card(
|
||||
color: Colors.white,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: ListView.builder(
|
||||
itemCount: goodsList.length,
|
||||
itemBuilder: (context, index) {
|
||||
final goods = goodsList[index];
|
||||
return ListTile(
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {},
|
||||
),
|
||||
leading: Image.network(
|
||||
goods["goodsPhoto"],
|
||||
width: 100,
|
||||
height: 100,
|
||||
),
|
||||
title: Text(
|
||||
"${goods["goodsName"]} - ${goods?["wisdGoodsSpec"]?["specName"] ?? ''}"),
|
||||
subtitle:
|
||||
Text("数量 : ${goods?["goodsNum"]}"),
|
||||
);
|
||||
})),
|
||||
const Expanded(flex: 1, child: Placeholder())
|
||||
/*
|
||||
GridView.builder(
|
||||
gridDelegate:
|
||||
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 5,
|
||||
childAspectRatio: 1.0),
|
||||
itemCount: goodsList.length,
|
||||
itemBuilder: (context, index) {
|
||||
return Icon(Icons.add);
|
||||
}),
|
||||
*/
|
||||
],
|
||||
)),
|
||||
);
|
||||
} else if (snapshot.hasError) {
|
||||
return const Center(
|
||||
child: Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
||||
);
|
||||
} else {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
},
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
@ -13,10 +13,14 @@ class MyApp extends StatelessWidget {
|
||||
final _router = GoRouter(
|
||||
routes: [
|
||||
GoRoute(
|
||||
name: "order_list",
|
||||
path: '/',
|
||||
builder: (context, state) => const OrderList(),
|
||||
),
|
||||
GoRoute(path: '/logistics', builder: (context, state) => const Logistics())
|
||||
GoRoute(
|
||||
path: '/logistics',
|
||||
name: "logistics",
|
||||
builder: (context, state) => const Logistics())
|
||||
],
|
||||
);
|
||||
|
||||
|
@ -3,7 +3,9 @@ import 'dart:io';
|
||||
import 'package:excel/excel.dart';
|
||||
import 'package:file_picker/file_picker.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 +18,10 @@ class _OrderListState extends State<OrderList> {
|
||||
final List lst = [];
|
||||
List<String> headers = [];
|
||||
List<List<dynamic>> data = [];
|
||||
|
||||
late Excel _excel;
|
||||
ScrollController scrollController = ScrollController();
|
||||
final TextEditingController _logComController =
|
||||
TextEditingController(text: "");
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -31,7 +35,11 @@ class _OrderListState extends State<OrderList> {
|
||||
importExcelFile();
|
||||
},
|
||||
icon: const Icon(Icons.add_circle)),
|
||||
IconButton(onPressed: () {}, icon: const Icon(Icons.output))
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
exportExcelFile();
|
||||
},
|
||||
icon: const Icon(Icons.output))
|
||||
],
|
||||
),
|
||||
body: Container(
|
||||
@ -55,11 +63,11 @@ class _OrderListState extends State<OrderList> {
|
||||
columns: headers.map((el) {
|
||||
return DataColumn(label: Text(el));
|
||||
}).toList(),
|
||||
rows: data.map((row) {
|
||||
rows: data.asMap().entries.map((row) {
|
||||
return DataRow(
|
||||
cells: row.asMap().entries.map((col) {
|
||||
String col_title = headers[col.key];
|
||||
return DataCell(col_title == "物流名称"
|
||||
cells: row.value.asMap().entries.map((col) {
|
||||
String colTitle = headers[col.key];
|
||||
return DataCell(colTitle == "物流名称"
|
||||
? Row(
|
||||
children: [
|
||||
Text("${col.value}"),
|
||||
@ -67,7 +75,9 @@ class _OrderListState extends State<OrderList> {
|
||||
// padding: EdgeInsets.all(2),
|
||||
iconSize: 12,
|
||||
onPressed: () {
|
||||
// Dialog()
|
||||
_logComController.text =
|
||||
data[row.key][col.key]
|
||||
.toString();
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext
|
||||
@ -76,25 +86,52 @@ class _OrderListState extends State<OrderList> {
|
||||
// icon: const Icon(Icons.edit),
|
||||
title: const Text(
|
||||
"填写物流公司"),
|
||||
content: const Column(
|
||||
content: Column(
|
||||
mainAxisSize:
|
||||
MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
decoration:
|
||||
InputDecoration(
|
||||
const InputDecoration(
|
||||
labelText:
|
||||
"物流公司"),
|
||||
controller:
|
||||
_logComController,
|
||||
)
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
context.pop();
|
||||
},
|
||||
child: const Text(
|
||||
"取消")),
|
||||
ElevatedButton(
|
||||
onPressed: () {},
|
||||
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(
|
||||
"确认")),
|
||||
],
|
||||
@ -103,10 +140,29 @@ class _OrderListState extends State<OrderList> {
|
||||
icon: const Icon(Icons.edit))
|
||||
],
|
||||
)
|
||||
: col_title == "物流信息"
|
||||
: colTitle == "物流信息"
|
||||
? ElevatedButton(
|
||||
onPressed: () {
|
||||
context.push("/logistics");
|
||||
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(),
|
||||
});
|
||||
},
|
||||
child: const Text("填写"),
|
||||
)
|
||||
@ -130,18 +186,22 @@ class _OrderListState extends State<OrderList> {
|
||||
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) {
|
||||
return el?.value.toString() ?? "";
|
||||
}).toList() ??
|
||||
[];
|
||||
List<List<dynamic>> _data =
|
||||
excel.tables[tableKey]?.rows.sublist(1).map((row) {
|
||||
return row.map((col) {
|
||||
return col?.value ?? "";
|
||||
}).toList();
|
||||
}).toList() ??
|
||||
[];
|
||||
List<List<dynamic>> _data = excel.tables[tableKey]?.rows
|
||||
.where((el) => (el.first?.value?.toString() ?? "").isNotEmpty)
|
||||
.toList()
|
||||
.sublist(1)
|
||||
.map((row) {
|
||||
return row.map((col) {
|
||||
return col?.value ?? "";
|
||||
}).toList();
|
||||
}).toList() ??
|
||||
[];
|
||||
setState(() {
|
||||
headers = _headers;
|
||||
data = _data;
|
||||
@ -151,4 +211,30 @@ class _OrderListState extends State<OrderList> {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
exportExcelFile() async {
|
||||
data.asMap().entries.forEach((row) {
|
||||
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();
|
||||
// var directory = await getApplicationDocumentsDirectory();
|
||||
File(
|
||||
"C:\\Users\\ayaya\\Desktop\\导出订单${DateTime.now().millisecondsSinceEpoch}.xlsx")
|
||||
..createSync(recursive: true)
|
||||
..writeAsBytesSync(fileBytes!);
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text("导出文件成功"),
|
||||
backgroundColor: Colors.green,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
3
lib/utils/request.dart
Normal file
3
lib/utils/request.dart
Normal file
@ -0,0 +1,3 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
final dio = Dio();
|
Reference in New Issue
Block a user