add scroll bar
This commit is contained in:
@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
|
||||
import 'package:async/async.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
@ -18,7 +19,10 @@ class Logistics extends StatefulWidget {
|
||||
class _LogisticsState extends State<Logistics> {
|
||||
List<dynamic> goodsList = [];
|
||||
String _orderPayWay = "";
|
||||
|
||||
// String _logistics = "";
|
||||
List _packages = [];
|
||||
bool _packageInit = false;
|
||||
int _activePackage = 0;
|
||||
final TextEditingController _goodsNameController =
|
||||
TextEditingController(text: "");
|
||||
@ -43,6 +47,8 @@ class _LogisticsState extends State<Logistics> {
|
||||
GoRouterState.of(context).uri.queryParameters["orderNumber"];
|
||||
String? orderPayWay =
|
||||
GoRouterState.of(context).uri.queryParameters["orderPayWay"];
|
||||
String? logistics =
|
||||
GoRouterState.of(context).uri.queryParameters["logistics"];
|
||||
_orderPayWay = orderPayWay!;
|
||||
|
||||
if (orderNumber?.isEmpty ?? true) {
|
||||
@ -54,6 +60,30 @@ class _LogisticsState extends State<Logistics> {
|
||||
queryParameters: {"orderNumber": orderNumber});
|
||||
});
|
||||
goodsList = response.data["value"];
|
||||
|
||||
if (logistics != null && logistics.isNotEmpty && !_packageInit) {
|
||||
try {
|
||||
List packages = jsonDecode(logistics);
|
||||
|
||||
_packages = packages;
|
||||
|
||||
if (_orderPayWay != "套餐商品") {
|
||||
for (var p in _packages) {
|
||||
p["goods"].forEach((g) {
|
||||
var targetGoods = goodsList
|
||||
.firstWhere((tg) => g["id"] == tg["id"], orElse: () => null);
|
||||
if (targetGoods != null) {
|
||||
targetGoods["goodsNum"] -= g["num"];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
_packageInit = true;
|
||||
} catch (e) {
|
||||
e;
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -73,6 +103,7 @@ class _LogisticsState extends State<Logistics> {
|
||||
if (hasUnAddedGoods) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text("有未添加至包裹的商品"),
|
||||
showCloseIcon: true,
|
||||
backgroundColor: Colors.redAccent,
|
||||
));
|
||||
return;
|
||||
@ -90,6 +121,7 @@ class _LogisticsState extends State<Logistics> {
|
||||
if (!result) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
backgroundColor: Colors.redAccent,
|
||||
showCloseIcon: true,
|
||||
content: Text("有未填写的数据")));
|
||||
return;
|
||||
}
|
||||
@ -184,8 +216,9 @@ class _LogisticsState extends State<Logistics> {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
"请填写完整商品信息")));
|
||||
content: Text("请填写完整商品信息"),
|
||||
showCloseIcon: true,
|
||||
));
|
||||
return;
|
||||
}
|
||||
int goodsNum = int.parse(
|
||||
@ -216,77 +249,89 @@ class _LogisticsState extends State<Logistics> {
|
||||
child: const Text("添加到包裹"))
|
||||
],
|
||||
)
|
||||
: ListView.builder(
|
||||
itemCount: goodsList
|
||||
: goodsList
|
||||
.where((el) => el["goodsNum"] > 0)
|
||||
.length,
|
||||
itemBuilder: (context, index) {
|
||||
final goods = goodsList
|
||||
.where((el) => el["goodsNum"] > 0)
|
||||
.toList()[index];
|
||||
return ListTile(
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
// 检查商品及规格是否已存在于目标包裹
|
||||
int targetIndex = _packages[
|
||||
_activePackage]["goods"]
|
||||
.indexWhere((el) =>
|
||||
el["id"] ==
|
||||
goods["id"] &&
|
||||
goods["wisdGoodsSpec"]
|
||||
["id"] ==
|
||||
el["specId"]);
|
||||
if (targetIndex != -1) {
|
||||
setState(() {
|
||||
_packages[_activePackage]
|
||||
["goods"]
|
||||
[targetIndex]
|
||||
["num"] += 1;
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
_packages[_activePackage]
|
||||
.isNotEmpty
|
||||
? ListView.builder(
|
||||
itemCount: goodsList
|
||||
.where(
|
||||
(el) => el["goodsNum"] > 0)
|
||||
.length,
|
||||
itemBuilder: (context, index) {
|
||||
final goods = goodsList
|
||||
.where((el) =>
|
||||
el["goodsNum"] > 0)
|
||||
.toList()[index];
|
||||
return ListTile(
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
// 检查商品及规格是否已存在于目标包裹
|
||||
int targetIndex = _packages[
|
||||
_activePackage]
|
||||
["goods"]
|
||||
.add({
|
||||
"id": goods["id"],
|
||||
"specId":
|
||||
goods["wisdGoodsSpec"]
|
||||
.indexWhere((el) =>
|
||||
el["id"] ==
|
||||
goods["id"] &&
|
||||
goods["wisdGoodsSpec"]
|
||||
["id"] ==
|
||||
el["specId"]);
|
||||
if (targetIndex != -1) {
|
||||
setState(() {
|
||||
_packages[_activePackage]
|
||||
["goods"]
|
||||
[targetIndex]
|
||||
["num"] += 1;
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
_packages[_activePackage]
|
||||
["goods"]
|
||||
.add({
|
||||
"id": goods["id"],
|
||||
"specId": goods[
|
||||
"wisdGoodsSpec"]
|
||||
["id"],
|
||||
"name":
|
||||
goods["goodsName"],
|
||||
"spec":
|
||||
goods?["wisdGoodsSpec"]
|
||||
"name": goods[
|
||||
"goodsName"],
|
||||
"spec": goods?[
|
||||
"wisdGoodsSpec"]
|
||||
?[
|
||||
"specName"] ??
|
||||
"",
|
||||
"num": 1
|
||||
});
|
||||
});
|
||||
}
|
||||
var curGoods =
|
||||
goodsList.firstWhere(
|
||||
(el) =>
|
||||
el["id"] ==
|
||||
goods["id"],
|
||||
orElse: () => -1);
|
||||
"num": 1
|
||||
});
|
||||
});
|
||||
}
|
||||
var curGoods =
|
||||
goodsList.firstWhere(
|
||||
(el) =>
|
||||
el["id"] ==
|
||||
goods["id"],
|
||||
orElse: () => -1);
|
||||
|
||||
setState(() {
|
||||
curGoods["goodsNum"] -= 1;
|
||||
});
|
||||
},
|
||||
setState(() {
|
||||
curGoods["goodsNum"] -= 1;
|
||||
});
|
||||
},
|
||||
),
|
||||
leading: Image.network(
|
||||
goods["goodsPhoto"],
|
||||
width: 100,
|
||||
height: 100,
|
||||
),
|
||||
title: Text(
|
||||
"${goods["goodsName"]} - ${goods?["wisdGoodsSpec"]?["specName"] ?? ''}"),
|
||||
subtitle: Text(
|
||||
"数量 : ${goods?["goodsNum"]}"),
|
||||
);
|
||||
})
|
||||
: const Center(
|
||||
child: Icon(
|
||||
CupertinoIcons.circle,
|
||||
size: 36,
|
||||
),
|
||||
leading: Image.network(
|
||||
goods["goodsPhoto"],
|
||||
width: 100,
|
||||
height: 100,
|
||||
),
|
||||
title: Text(
|
||||
"${goods["goodsName"]} - ${goods?["wisdGoodsSpec"]?["specName"] ?? ''}"),
|
||||
subtitle: Text(
|
||||
"数量 : ${goods?["goodsNum"]}"),
|
||||
);
|
||||
})),
|
||||
)),
|
||||
const SizedBox(
|
||||
width: 20,
|
||||
),
|
||||
@ -300,10 +345,39 @@ class _LogisticsState extends State<Logistics> {
|
||||
itemBuilder:
|
||||
(BuildContext context, int index) {
|
||||
return PackageCard(
|
||||
logisticsNumber: _packages[index]
|
||||
["logisticsNumber"],
|
||||
onDeleteGoods: (goods) {
|
||||
if (goods["num"] == 1) {
|
||||
setState(() {
|
||||
_packages[index]["goods"] =
|
||||
_packages[index]["goods"]
|
||||
.where((el) =>
|
||||
el["id"] !=
|
||||
goods["id"])
|
||||
.toList();
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
goods["num"] -= 1;
|
||||
});
|
||||
}
|
||||
if (_orderPayWay != "套餐商品") {
|
||||
setState(() {
|
||||
goodsList.firstWhere(
|
||||
(e) =>
|
||||
goods["id"] ==
|
||||
e["id"],
|
||||
orElse: () => null)?[
|
||||
"goodsNum"] += 1;
|
||||
});
|
||||
}
|
||||
},
|
||||
onLogisticsChanged: (value) {
|
||||
_packages[index]
|
||||
["logisticsNumber"] = value;
|
||||
},
|
||||
orderPayWay: _orderPayWay,
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_activePackage = index;
|
||||
@ -312,6 +386,23 @@ class _LogisticsState extends State<Logistics> {
|
||||
onClose: () {
|
||||
var curId =
|
||||
_packages[index]["id"];
|
||||
if (_orderPayWay != "套餐商品") {
|
||||
_packages[index]["goods"]
|
||||
.forEach((el) {
|
||||
var targetGoods =
|
||||
goodsList.firstWhere(
|
||||
(goodsItem) =>
|
||||
goodsItem["id"] ==
|
||||
el["id"],
|
||||
orElse: () => -1);
|
||||
if (targetGoods != -1) {
|
||||
setState(() {
|
||||
targetGoods["goodsNum"] +=
|
||||
el["num"];
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
setState(() {
|
||||
_activePackage =
|
||||
_activePackage >= index
|
||||
@ -341,6 +432,7 @@ class _LogisticsState extends State<Logistics> {
|
||||
"goods": []
|
||||
}
|
||||
];
|
||||
_activePackage += 1;
|
||||
});
|
||||
},
|
||||
child: const Text("添加包裹"))
|
||||
@ -351,10 +443,14 @@ class _LogisticsState extends State<Logistics> {
|
||||
);
|
||||
} else if (snapshot.hasError) {
|
||||
return const Center(
|
||||
child: Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
color: Colors.red,
|
||||
size: 60,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@ -375,21 +471,36 @@ class PackageCard extends StatefulWidget {
|
||||
required this.onLogisticsChanged,
|
||||
required this.totalPackagesCount,
|
||||
required this.onClose,
|
||||
required this.orderPayWay,
|
||||
required this.onDeleteGoods,
|
||||
required this.logisticsNumber,
|
||||
});
|
||||
|
||||
final bool isActive;
|
||||
final Function() onTap;
|
||||
final Function() onClose;
|
||||
final Function(Map<String, dynamic> goods) onDeleteGoods;
|
||||
final Map<String, dynamic> package;
|
||||
final int index;
|
||||
final Function(String) onLogisticsChanged;
|
||||
final int totalPackagesCount;
|
||||
final String orderPayWay;
|
||||
final String logisticsNumber;
|
||||
|
||||
@override
|
||||
State<PackageCard> createState() => _PackageCardState();
|
||||
}
|
||||
|
||||
class _PackageCardState extends State<PackageCard> {
|
||||
final TextEditingController _logisticsController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
super.initState();
|
||||
_logisticsController.text = widget.logisticsNumber;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
@ -417,6 +528,7 @@ class _PackageCardState extends State<PackageCard> {
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
TextField(
|
||||
controller: _logisticsController,
|
||||
onChanged: widget.onLogisticsChanged,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(), labelText: "物流单号"),
|
||||
@ -427,13 +539,26 @@ class _PackageCardState extends State<PackageCard> {
|
||||
DataTable(columns: const [
|
||||
DataColumn(label: Text("名称")),
|
||||
DataColumn(label: Text("规格")),
|
||||
DataColumn(label: Text("数量"))
|
||||
DataColumn(label: Text("数量")),
|
||||
DataColumn(label: Text("操作"))
|
||||
], rows: [
|
||||
for (var goodsItem in widget.package["goods"])
|
||||
for (var goodsItem
|
||||
in widget.package["goods"].asMap().entries)
|
||||
DataRow(cells: [
|
||||
DataCell(Text("${goodsItem["name"]}")),
|
||||
DataCell(Text("${goodsItem["spec"]}")),
|
||||
DataCell(Text("${goodsItem["num"]}")),
|
||||
DataCell(Text("${goodsItem.value["name"]}")),
|
||||
DataCell(Text("${goodsItem.value["spec"]}")),
|
||||
DataCell(Text("${goodsItem.value["num"]}")),
|
||||
DataCell(SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: IconButton(
|
||||
icon: const Icon(CupertinoIcons.delete),
|
||||
iconSize: 16,
|
||||
padding: const EdgeInsets.all(4),
|
||||
onPressed: () {
|
||||
widget.onDeleteGoods(goodsItem.value);
|
||||
}),
|
||||
)),
|
||||
])
|
||||
]),
|
||||
],
|
||||
|
Reference in New Issue
Block a user