order_taking_page.dart 20 KB

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