order_taking_page.dart 17 KB

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