order_taking_page.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:url_launcher/url_launcher.dart';
  4. import '../util/api.dart';
  5. import '../util/models.dart';
  6. import '../showDialog.dart';
  7. import '../util/session.dart';
  8. import '../util/util.dart';
  9. import '../showAlert.dart';
  10. class OrderTakingPage extends StatefulWidget {
  11. OrderTakingPage({
  12. Key key
  13. }): super(key: key);
  14. _OrderTakingPageState createState() => _OrderTakingPageState();
  15. }
  16. class _OrderTakingPageState extends State < OrderTakingPage > with AutomaticKeepAliveClientMixin {
  17. ScrollController _scrollController = ScrollController(); //初始化滚动轴监控对象
  18. List < OrderList > orderList = []; //订单列表
  19. bool hasNextPage = true;
  20. List lineList = []; //线路列表
  21. String stationId;
  22. List < bool > checkStatusList = [];
  23. bool showLineStatus = true;
  24. String lineIdStr = '';
  25. String oldLineIdStr = '';
  26. int page = 1;
  27. bool loadMoreOrder = true;
  28. @override
  29. void initState() {
  30. super.initState();
  31. getKey('stationId').then((value) {
  32. stationId = value;
  33. getLine().then((childValue) {
  34. getKey('lineIdStr').then((value) {
  35. if (value != null) {
  36. getOrderList(stationId,value, page).then((resp) {
  37. if (isNotError(context, resp) && this.mounted) {
  38. setState(() {
  39. orderList = OrderListObj.fromJson(resp.data).data.items;
  40. hasNextPage = OrderListObj.fromJson(resp.data).data.hasNextPage;
  41. });
  42. lineIdStr = value;
  43. }
  44. });
  45. } else {
  46. getOrderList(stationId,lineIdStr, page).then((resp) {
  47. if (isNotError(context, resp) && this.mounted) {
  48. setState(() {
  49. orderList = OrderListObj.fromJson(resp.data).data.items;
  50. hasNextPage = OrderListObj.fromJson(resp.data).data.hasNextPage;
  51. });
  52. }
  53. });
  54. }
  55. });
  56. });
  57. });
  58. _scrollController.addListener(() {
  59. if (!showLineStatus) {
  60. setState(() {
  61. showLineStatus = true;
  62. });
  63. switchLine(context);
  64. }
  65. if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
  66. if (hasNextPage && loadMoreOrder) {
  67. loadMoreOrder = false;
  68. page = ++page;
  69. getOrderList(stationId,lineIdStr, page).then((resp) {
  70. if (isNotError(context, resp) && this.mounted) {
  71. OrderListObj tempObj = OrderListObj.fromJson(resp.data);
  72. setState(() {
  73. orderList.addAll(tempObj.data.items);
  74. hasNextPage = tempObj.data.hasNextPage;
  75. });
  76. loadMoreOrder = true;
  77. } else {
  78. loadMoreOrder = true;
  79. }
  80. });
  81. }
  82. }
  83. });
  84. }
  85. Future getLine() async {
  86. setState(() {
  87. lineList.clear();
  88. });
  89. await getAllLine().then((resp) {
  90. if (isNotError(context, resp) && this.mounted) {
  91. setState(() {
  92. checkStatusList = [];
  93. lineList = StationLine.fromJson(resp.data).data;
  94. LineObj tempLine =LineObj(code:'xxxx',id:-1,name:'未知线路');//创建未知线路
  95. lineList.add(tempLine);//添加未知线路
  96. getKey('lineIdStr').then((value) {
  97. if (value != null) {
  98. for (int i = 0; i < lineList.length; i++) {
  99. if (value.indexOf(lineList[i].id.toString()) != -1) {
  100. checkStatusList.add(true);
  101. } else {
  102. checkStatusList.add(false);
  103. }
  104. if (i < lineList.length - 1) {
  105. lineIdStr += lineList[i].id.toString() + ',';
  106. } else {
  107. lineIdStr += lineList[i].id.toString();
  108. }
  109. }
  110. } else {
  111. for (int i = 0; i < lineList.length; i++) {
  112. checkStatusList.add(true);
  113. if (i < lineList.length - 1) {
  114. lineIdStr += lineList[i].id.toString() + ',';
  115. } else {
  116. lineIdStr += lineList[i].id.toString();
  117. }
  118. }
  119. }
  120. });
  121. });
  122. return lineIdStr;
  123. }
  124. });
  125. }
  126. @override
  127. bool get wantKeepAlive => true;
  128. @override
  129. Widget build(BuildContext context) {
  130. return Container(
  131. child: Scaffold(
  132. appBar: AppBar(
  133. title: Text('接单', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(36)), ),
  134. centerTitle: true,
  135. backgroundColor: Color.fromRGBO(64, 98, 254, 1),
  136. actions: < Widget > [
  137. IconButton(icon: Icon(Icons.add_circle_outline),
  138. onPressed: () {
  139. setState(() {
  140. showLineStatus = !showLineStatus;
  141. });
  142. if (!showLineStatus) {
  143. getLine();
  144. } else {
  145. switchLine(context);
  146. }
  147. }, )
  148. ]
  149. ),
  150. body: Stack(
  151. children: < Widget > [
  152. RefreshIndicator(
  153. onRefresh: _onRefresh, //回调方法
  154. child: InkWell(
  155. child: Container(
  156. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(20), ScreenUtil.getInstance().setWidth(20), ScreenUtil.getInstance().setWidth(20), 0),
  157. decoration: BoxDecoration(
  158. color: Color.fromRGBO(246, 246, 254, 1)
  159. ),
  160. child: ListView.builder(
  161. controller: _scrollController,
  162. physics: AlwaysScrollableScrollPhysics(),
  163. itemCount: orderList.length + 1,
  164. itemBuilder: (BuildContext context, int index) { //itemBuilder实际上就是一个map的循环
  165. if (index < orderList.length) {
  166. return OrderCardContainer(order: orderList[index], index: index, callback: (val) => deleteOrder(val));
  167. }
  168. if (hasNextPage) {
  169. return _getMoreWidget();
  170. } else {
  171. return _noMoreWidegt();
  172. }
  173. },
  174. )
  175. ),
  176. onTap: () {
  177. setState(() {
  178. showLineStatus = true;
  179. });
  180. switchLine(context);
  181. },
  182. ),
  183. ),
  184. Positioned(
  185. top: 0,
  186. right: 0,
  187. child:
  188. Offstage(
  189. offstage: showLineStatus,
  190. child: Container(
  191. width: ScreenUtil.getInstance().setWidth(350),
  192. height: ScreenUtil.getInstance().setHeight(500),
  193. margin: EdgeInsets.fromLTRB(0, 1, 1, 0),
  194. padding: EdgeInsets.fromLTRB(1, 0, 1, 0),
  195. decoration: BoxDecoration(
  196. color: Colors.white,
  197. borderRadius: BorderRadius.all(Radius.circular(4.0)),
  198. boxShadow: [ //阴影
  199. BoxShadow(
  200. color: Colors.black54,
  201. offset: Offset(2.0, 2.0),
  202. blurRadius: 4.0
  203. )
  204. ]
  205. ),
  206. child: lineList.length == 0 ? Center(
  207. child: Container(
  208. width: ScreenUtil.getInstance().setWidth(50),
  209. height: ScreenUtil.getInstance().setHeight(50),
  210. child: CircularProgressIndicator(
  211. )
  212. ),
  213. ) :
  214. ListView.builder(
  215. itemCount: lineList.length,
  216. itemBuilder: (BuildContext context, int index) { //itemBuilder实际上就是一个map的循环
  217. if (index < lineList.length) {
  218. return _lineWidget(lineList[index], index, checkStatusList[index]);
  219. }
  220. },
  221. ),
  222. ),
  223. )
  224. )
  225. ],
  226. )
  227. ),
  228. );
  229. }
  230. Widget _lineWidget(LineObj lineObj, int index, bool checked) {
  231. return InkWell(
  232. child: Container(
  233. height: ScreenUtil.getInstance().setHeight(80),
  234. decoration: BoxDecoration(
  235. border: BorderDirectional(bottom: BorderSide(color: Colors.white, width: 0.1))
  236. ),
  237. child: Row(
  238. children: < Widget > [
  239. Padding(
  240. padding: EdgeInsets.only(right: 5),
  241. child: checked ? Icon(Icons.check, color: Colors.black, ) : Icon(Icons.check, color: Color.fromRGBO(255, 255, 255, 0.0, )),
  242. ),
  243. Expanded(
  244. flex: 1,
  245. child: Center(
  246. child: Text(lineObj.name, style: TextStyle(color: Colors.black))
  247. )
  248. )
  249. ],
  250. ),
  251. ),
  252. onTap: () {
  253. setState(() {
  254. checkStatusList[index] = !checked;
  255. });
  256. },
  257. );
  258. }
  259. Widget _getMoreWidget() {
  260. return Center(
  261. child: Padding(
  262. padding: EdgeInsets.all(10.0),
  263. child: Row(
  264. mainAxisAlignment: MainAxisAlignment.center,
  265. crossAxisAlignment: CrossAxisAlignment.center,
  266. children: < Widget > [
  267. Text(
  268. '加载中...',
  269. style: TextStyle(fontSize: 16.0),
  270. ),
  271. Container(
  272. width: ScreenUtil.getInstance().setWidth(50),
  273. height: ScreenUtil.getInstance().setHeight(50),
  274. child: CircularProgressIndicator()
  275. )
  276. ],
  277. ),
  278. ),
  279. );
  280. }
  281. Widget _noMoreWidegt() {
  282. return Center(
  283. child: Padding(
  284. padding: EdgeInsets.all(10.0),
  285. child: Text(
  286. '没有更多数据了...',
  287. style: TextStyle(fontSize: 16.0),
  288. ),
  289. ),
  290. );
  291. }
  292. Future _onRefresh() async {
  293. page = 1;
  294. print(lineIdStr);
  295. await getOrderList(stationId,lineIdStr, page).then((resp) {
  296. if (isNotError(context, resp) && this.mounted) {
  297. setState(() {
  298. orderList = OrderListObj.fromJson(resp.data).data.items;
  299. });
  300. }
  301. });
  302. }
  303. void deleteOrder(val) { //确认接单后的操作
  304. setState(() {
  305. orderList.removeAt(val);
  306. if (orderList.length < 4) {
  307. if (hasNextPage) {
  308. setState(() {
  309. page = ++page;
  310. });
  311. getOrderList(stationId,lineIdStr, page).then((resp) {
  312. if (resp.success && resp.code == 1) {
  313. setState(() {
  314. orderList.addAll(resp.data.items);
  315. hasNextPage = resp.data.hasNextPage;
  316. });
  317. }
  318. });
  319. }
  320. }
  321. });
  322. }
  323. void switchLine(BuildContext context) { //切换线路
  324. lineIdStr = '';
  325. for (int i = 0; i < checkStatusList.length; i++) {
  326. if (checkStatusList[i]) {
  327. lineIdStr += lineList[i].id.toString() + ',';
  328. }
  329. }
  330. if (lineIdStr == '') {
  331. setState(() {
  332. orderList = [];
  333. hasNextPage = false;
  334. });
  335. return;
  336. }
  337. lineIdStr = lineIdStr.substring(0, lineIdStr.length - 1);
  338. getKey('oldLineIdStr').then((oldLineIdStr){
  339. if(oldLineIdStr == lineIdStr) return;
  340. setKey('oldLineIdStr', lineIdStr);
  341. setState(() {
  342. orderList = [];
  343. hasNextPage = true;
  344. });
  345. getOrderList(stationId,lineIdStr, page).then((resp) {
  346. if (isNotError(context, resp) && this.mounted) {
  347. OrderListObj tempObj = OrderListObj.fromJson(resp.data);
  348. setState(() {
  349. orderList = tempObj.data.items;
  350. hasNextPage = tempObj.data.hasNextPage;
  351. });
  352. oldLineIdStr = lineIdStr;
  353. setKey('lineIdStr', lineIdStr);
  354. }
  355. });
  356. });
  357. }
  358. }
  359. class OrderCardContainer extends StatefulWidget {
  360. final OrderList order;
  361. final int index;
  362. final callback;
  363. OrderCardContainer({
  364. Key key,
  365. @required this.order,
  366. @required this.index,
  367. @required this.callback
  368. }): super(key: key);
  369. _OrderCardContainerState createState() => _OrderCardContainerState();
  370. }
  371. class _OrderCardContainerState extends State < OrderCardContainer > with SingleTickerProviderStateMixin {
  372. Animation < dynamic > animation;
  373. AnimationController controller;
  374. String str = "联系方式";
  375. bool animationStatus = true;
  376. @override
  377. initState() {
  378. super.initState();
  379. controller = new AnimationController(duration: const Duration(milliseconds: 200), vsync: this);
  380. animation = Tween(begin: 0, end: 230).animate(controller);
  381. animation.addStatusListener((status) {
  382. if (status == AnimationStatus.completed) {
  383. //动画执行结束时反向执行动画
  384. setState(() {
  385. animationStatus = false;
  386. str = "收起";
  387. });
  388. } else if (status == AnimationStatus.dismissed) {
  389. //动画恢复到初始状态时执行动画(正向)
  390. setState(() {
  391. animationStatus = true;
  392. str = "联系方式";
  393. });
  394. }
  395. });
  396. }
  397. @override
  398. Widget build(BuildContext context) {
  399. return Card(
  400. color: Colors.white,
  401. elevation: 3.0,
  402. child: Container(
  403. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), 0),
  404. child: Column(
  405. children: < Widget > [
  406. orderNoRow(widget.order.orderNo, widget.order.createTime),
  407. orderAddress(widget.order.sendCompanyName,widget.order.receiptCompanyName),
  408. orderItem('货物名称', widget.order.goods, 15),
  409. orderItem('货物件数', '${widget.order.goodsQuantity}件', 30),
  410. orderCutLine(),
  411. orderBtn(context, animationStatus, controller, str, widget.order.orderNo),
  412. AnimatedOpen(animation: animation, openInfo: widget.order, )
  413. ],
  414. ),
  415. )
  416. );
  417. }
  418. dispose() {
  419. //路由销毁时需要释放动画资源
  420. controller.dispose();
  421. super.dispose();
  422. }
  423. Widget orderBtn(BuildContext context, bool animationStatus, AnimationController controller, String str, String orderNo) {
  424. return Container(
  425. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  426. child: Row(
  427. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  428. children: < Widget > [
  429. InkWell(
  430. child: Container(
  431. width: ScreenUtil.getInstance().setWidth(100),
  432. height: ScreenUtil.getInstance().setHeight(48),
  433. color: Colors.white,
  434. child: Center(
  435. child: Text(str, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(64, 98, 254, 1)), ),
  436. )
  437. ),
  438. onTap: () {
  439. if (animationStatus) {
  440. //动画恢复到初始状态时执行动画(正向)
  441. controller.forward();
  442. } else {
  443. //动画执行结束时反向执行动画
  444. controller.reverse();
  445. }
  446. },
  447. ),
  448. InkWell(
  449. child: Container(
  450. width: ScreenUtil.getInstance().setWidth(94),
  451. height: ScreenUtil.getInstance().setWidth(48),
  452. decoration: BoxDecoration(
  453. color: Color.fromRGBO(252, 97, 128, 1),
  454. borderRadius: BorderRadius.all(Radius.circular(4.0))
  455. ),
  456. child: Center(
  457. child: Text("接单", style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Colors.white), ),
  458. ),
  459. ),
  460. onTap: () {
  461. showDialog < Null > (
  462. context: context, //BuildContext对象
  463. barrierDismissible: false,
  464. builder: (BuildContext context) {
  465. return new LoadingDialog( //调用对话框
  466. text: '是否确认接单', childCallback: (val) {
  467. if (val) {
  468. orderReception(orderNo).then((resp) {
  469. if (isNotError(context, resp) && this.mounted) {
  470. OrderReception obj = OrderReception.fromJson(resp.data);
  471. if(obj.success){
  472. widget.callback(widget.index);
  473. }else{
  474. showError(obj.msg);
  475. }
  476. }
  477. });
  478. } else {
  479. return;
  480. }
  481. },
  482. );
  483. });
  484. },
  485. )
  486. ],
  487. )
  488. );
  489. }
  490. showError(String str) {
  491. showDialog < Null > (
  492. context: context, //BuildContext对象
  493. barrierDismissible: false,
  494. builder: (BuildContext context) {
  495. return MyAlertDialog( //调用对话框
  496. text: str, childCallback: (val) {},
  497. );
  498. });
  499. }
  500. }
  501. class AnimatedOpen extends AnimatedWidget {
  502. final OrderList openInfo;
  503. AnimatedOpen({
  504. Key key,
  505. Animation < dynamic > animation,
  506. @required this.openInfo
  507. }): super(key: key, listenable: animation);
  508. Widget build(BuildContext context) {
  509. final Animation < dynamic > animation = listenable;
  510. return Container(
  511. height: ScreenUtil.getInstance().setHeight(animation.value),
  512. child: orderDetailInfo(openInfo),
  513. );
  514. }
  515. }
  516. Widget orderNoRow(String orderNo, int createTime) { //订单号及时间
  517. String date = fromatDate(createTime);
  518. return Container(
  519. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
  520. child: Row(
  521. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  522. children: < Widget > [
  523. Text("订单号:${orderNo}", style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  524. Text('${date}', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  525. ],
  526. ),
  527. );
  528. }
  529. Widget orderAddress(String startAddress, String endAddress) { //运送路线
  530. return Container(
  531. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  532. child: Row(children: < Widget > [
  533. Text('${startAddress} ---> ${endAddress}', style: TextStyle(color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(30)), ),
  534. ], )
  535. );
  536. }
  537. Widget orderCutLine() { //分割线
  538. return Container(
  539. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  540. height: ScreenUtil.getInstance().setHeight(1),
  541. color: Color.fromRGBO(223, 223, 223, 1),
  542. );
  543. }
  544. Widget orderDetailInfo(OrderList obj) { //卡片展开信息
  545. String sendAddress = obj.sendCompanyProvinceName +' '+ (obj.sendCompanyCityName=='市辖区'?'':obj.sendCompanyCityName) +' '+ obj.sendCompanyAreaName+' '+ obj.sendCompanyAddress;
  546. String receiptAddress = obj.receiptCompanyProvinceName + ' '+(obj.receiptCompanyCityName=='市辖区'?'':obj.receiptCompanyCityName) +' '+ obj.receiptCompanyAreaName+' '+ obj.receiptCompanyAddress;
  547. return Container(
  548. child: Wrap(
  549. children: < Widget > [
  550. orderCutLine(),
  551. orderPhone('发件电话', obj.sendCompanyPhone, 15),
  552. orderItem('发件地址', sendAddress, 30),
  553. orderPhone('收件电话', obj.receiptCompanyPhone, 15),
  554. orderItem('收件地址', receiptAddress, 30),
  555. ],
  556. )
  557. );
  558. }
  559. Widget orderItem(String key, String value, int marginBottom) { //货物名称,件数
  560. return Container(
  561. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(marginBottom)),
  562. child: Row(children: < Widget > [
  563. Text('${key}:${value}', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  564. ], )
  565. );
  566. }
  567. Widget orderPhone(String key, String value, int marginBottom) {
  568. return InkWell(
  569. child: Container(
  570. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(marginBottom)),
  571. child: Row(children: < Widget > [
  572. Text('${key}:', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  573. Text('${value}', style: TextStyle(color: Color.fromRGBO(64, 98, 254, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  574. ], )
  575. ),
  576. onTap: () async {
  577. String phone = 'tel:${value}';
  578. print(phone);
  579. if (await canLaunch(phone)) {
  580. await (launch(phone));
  581. } else {
  582. throw 'Could not launch $value';
  583. }
  584. },
  585. );
  586. }