order_taking_page.dart 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 Container(
  400. margin: EdgeInsets.all(4),
  401. decoration: BoxDecoration(
  402. color: Colors.white,
  403. borderRadius: BorderRadius.all(Radius.circular(8)),
  404. boxShadow: [
  405. BoxShadow(color: Color.fromRGBO(217, 226, 233, 1),offset: Offset(0, 4),blurRadius: 3.0),
  406. ]
  407. ),
  408. child: Container(
  409. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), 0),
  410. child: Column(
  411. children: < Widget > [
  412. orderNoRow(widget.order.orderNo, widget.order.createTime),
  413. orderAddress(widget.order.sendCompanyName,widget.order.receiptCompanyName),
  414. orderItem('货物名称', widget.order.goods, 15),
  415. orderItem('货物件数', '${widget.order.goodsQuantity}件', 30),
  416. orderCutLine(),
  417. orderBtn(context, animationStatus, controller, str, widget.order.orderNo),
  418. AnimatedOpen(animation: animation, openInfo: widget.order, )
  419. ],
  420. ),
  421. )
  422. );
  423. }
  424. dispose() {
  425. //路由销毁时需要释放动画资源
  426. controller.dispose();
  427. super.dispose();
  428. }
  429. Widget orderBtn(BuildContext context, bool animationStatus, AnimationController controller, String str, String orderNo) {
  430. return Container(
  431. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  432. child: Row(
  433. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  434. children: < Widget > [
  435. InkWell(
  436. child: Container(
  437. width: ScreenUtil.getInstance().setWidth(100),
  438. height: ScreenUtil.getInstance().setHeight(48),
  439. color: Colors.white,
  440. child: Center(
  441. child: Text(str, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(64, 98, 254, 1)), ),
  442. )
  443. ),
  444. onTap: () {
  445. if (animationStatus) {
  446. //动画恢复到初始状态时执行动画(正向)
  447. controller.forward();
  448. } else {
  449. //动画执行结束时反向执行动画
  450. controller.reverse();
  451. }
  452. },
  453. ),
  454. InkWell(
  455. child: Container(
  456. width: ScreenUtil.getInstance().setWidth(94),
  457. height: ScreenUtil.getInstance().setWidth(48),
  458. decoration: BoxDecoration(
  459. color: Color.fromRGBO(252, 97, 128, 1),
  460. borderRadius: BorderRadius.all(Radius.circular(4.0))
  461. ),
  462. child: Center(
  463. child: Text("接单", style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Colors.white), ),
  464. ),
  465. ),
  466. onTap: () {
  467. showDialog < Null > (
  468. context: context, //BuildContext对象
  469. barrierDismissible: false,
  470. builder: (BuildContext context) {
  471. return new LoadingDialog( //调用对话框
  472. text: '是否确认接单', childCallback: (val) {
  473. if (val) {
  474. orderReception(orderNo).then((resp) {
  475. if (isNotError(context, resp) && this.mounted) {
  476. OrderReception obj = OrderReception.fromJson(resp.data);
  477. if(obj.success){
  478. widget.callback(widget.index);
  479. }else{
  480. showError(obj.msg);
  481. }
  482. }
  483. });
  484. } else {
  485. return;
  486. }
  487. },
  488. );
  489. });
  490. },
  491. )
  492. ],
  493. )
  494. );
  495. }
  496. showError(String str) {
  497. showDialog < Null > (
  498. context: context, //BuildContext对象
  499. barrierDismissible: false,
  500. builder: (BuildContext context) {
  501. return MyAlertDialog( //调用对话框
  502. text: str, childCallback: (val) {},
  503. );
  504. });
  505. }
  506. }
  507. class AnimatedOpen extends AnimatedWidget {
  508. final OrderList openInfo;
  509. AnimatedOpen({
  510. Key key,
  511. Animation < dynamic > animation,
  512. @required this.openInfo
  513. }): super(key: key, listenable: animation);
  514. Widget build(BuildContext context) {
  515. final Animation < dynamic > animation = listenable;
  516. return Container(
  517. height: ScreenUtil.getInstance().setHeight(animation.value),
  518. child: orderDetailInfo(openInfo),
  519. );
  520. }
  521. }
  522. Widget orderNoRow(String orderNo, int createTime) { //订单号及时间
  523. String date = fromatDate(createTime);
  524. return Container(
  525. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
  526. child: Row(
  527. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  528. children: < Widget > [
  529. Text("订单号:${orderNo}", style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  530. Text('${date}', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  531. ],
  532. ),
  533. );
  534. }
  535. Widget orderAddress(String startAddress, String endAddress) { //运送路线
  536. return Container(
  537. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  538. child: Row(children: < Widget > [
  539. Text('${startAddress} ---> ${endAddress}',
  540. overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(30)), ),
  541. ], )
  542. );
  543. }
  544. Widget orderCutLine() { //分割线
  545. return Container(
  546. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  547. height: ScreenUtil.getInstance().setHeight(1),
  548. color: Color.fromRGBO(223, 223, 223, 1),
  549. );
  550. }
  551. Widget orderDetailInfo(OrderList obj) { //卡片展开信息
  552. String sendAddress = obj.sendCompanyProvinceName +' '+ (obj.sendCompanyCityName=='市辖区'?'':obj.sendCompanyCityName) +' '+ obj.sendCompanyAreaName+' '+ obj.sendCompanyAddress;
  553. String receiptAddress = obj.receiptCompanyProvinceName + ' '+(obj.receiptCompanyCityName=='市辖区'?'':obj.receiptCompanyCityName) +' '+ obj.receiptCompanyAreaName+' '+ obj.receiptCompanyAddress;
  554. return Container(
  555. child: Wrap(
  556. children: < Widget > [
  557. orderCutLine(),
  558. orderPhone('发件电话', obj.sendCompanyPhone, 15),
  559. orderItem('发件地址', sendAddress, 30),
  560. orderPhone('收件电话', obj.receiptCompanyPhone, 15),
  561. orderItem('收件地址', receiptAddress, 30),
  562. ],
  563. )
  564. );
  565. }
  566. Widget orderItem(String key, String value, int marginBottom) { //货物名称,件数
  567. return Container(
  568. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(marginBottom)),
  569. child: Row(children: < Widget > [
  570. Text('${key}:${value}',
  571. overflow: TextOverflow.ellipsis,
  572. style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  573. ], )
  574. );
  575. }
  576. Widget orderPhone(String key, String value, int marginBottom) {
  577. return InkWell(
  578. child: Container(
  579. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(marginBottom)),
  580. child: Row(children: < Widget > [
  581. Text('${key}:', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  582. Text('${value}', style: TextStyle(color: Color.fromRGBO(64, 98, 254, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  583. ], )
  584. ),
  585. onTap: () async {
  586. String phone = 'tel:${value}';
  587. print(phone);
  588. if (await canLaunch(phone)) {
  589. await (launch(phone));
  590. } else {
  591. throw 'Could not launch $value';
  592. }
  593. },
  594. );
  595. }