order_taking_page.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. class OrderTakingPage extends StatefulWidget {
  9. OrderTakingPage({
  10. Key key
  11. }): super(key: key);
  12. _OrderTakingPageState createState() => _OrderTakingPageState();
  13. }
  14. class _OrderTakingPageState extends State < OrderTakingPage > with AutomaticKeepAliveClientMixin {
  15. ScrollController _scrollController = ScrollController(); //初始化滚动轴监控对象
  16. List < OrderList > orderList = []; //订单列表
  17. bool hasNextPage = true;
  18. List lineList = []; //线路列表
  19. String stationId;
  20. List < bool > checkStatusList = [];
  21. bool showLineStatus = true;
  22. String lineIdStr = '';
  23. int page = 1;
  24. @override
  25. void initState() {
  26. super.initState();
  27. getKey('stationId').then((value) {
  28. setState(() {
  29. stationId = value;
  30. });
  31. getLine(value).then((childValue) {
  32. getOrderList(value, lineIdStr, page).then((resp) {
  33. if (resp.success && resp.code == 1) {
  34. setState(() {
  35. orderList = resp.data.items;
  36. hasNextPage = resp.data.hasNextPage;
  37. });
  38. }
  39. });
  40. });
  41. });
  42. _scrollController.addListener(() {
  43. if (!showLineStatus) {
  44. setState(() {
  45. showLineStatus = true;
  46. });
  47. switchLine();
  48. }
  49. if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
  50. print('滑动到底部了');
  51. if (hasNextPage) {
  52. setState(() {
  53. page = ++page;
  54. });
  55. getOrderList(stationId, lineIdStr, page).then((resp) {
  56. if (resp.success && resp.code == 1) {
  57. setState(() {
  58. orderList.addAll(resp.data.items);
  59. hasNextPage = resp.data.hasNextPage;
  60. });
  61. }
  62. });
  63. }
  64. }
  65. });
  66. }
  67. Future getLine(value) async {
  68. await getLineFromStation(value).then((resp) {
  69. if (resp.success && resp.code == 1) {
  70. setState(() {
  71. checkStatusList = [];
  72. lineList = resp.data;
  73. for (int i = 0; i < lineList.length; i++) {
  74. checkStatusList.add(true);
  75. if (i < lineList.length - 1) {
  76. lineIdStr += lineList[i].id.toString() + ',';
  77. } else {
  78. lineIdStr += lineList[i].id.toString();
  79. }
  80. }
  81. stationId = value;
  82. });
  83. print(lineIdStr);
  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();
  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();
  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. getOrderList(value, lineIdStr, 1).then((resp) {
  247. setState(() {
  248. orderList = resp.data.items;
  249. });
  250. });
  251. });
  252. }
  253. void deleteOrder(val) { //确认接单后的操作
  254. setState(() {
  255. orderList.removeAt(val);
  256. if (orderList.length < 4) {
  257. if (hasNextPage) {
  258. setState(() {
  259. page = ++page;
  260. });
  261. getOrderList(stationId, lineIdStr, page).then((resp) {
  262. if (resp.success && resp.code == 1) {
  263. setState(() {
  264. orderList.addAll(resp.data.items);
  265. hasNextPage = resp.data.hasNextPage;
  266. });
  267. }
  268. });
  269. }
  270. }
  271. });
  272. }
  273. void switchLine() { //切换线路
  274. setState(() {
  275. orderList = [];
  276. hasNextPage =true;
  277. });
  278. lineIdStr = '';
  279. for (int i = 0; i < checkStatusList.length; i++) {
  280. if (checkStatusList[i]) {
  281. lineIdStr += lineList[i].id.toString() + ',';
  282. }
  283. }
  284. lineIdStr = lineIdStr.substring(0, lineIdStr.length - 1);
  285. getOrderList(stationId, lineIdStr, page).then((resp) {
  286. if (resp.success && resp.code == 1) {
  287. setState(() {
  288. orderList = resp.data.items;
  289. hasNextPage = resp.data.hasNextPage;
  290. });
  291. }
  292. });
  293. }
  294. }
  295. class OrderCardContainer extends StatefulWidget {
  296. final OrderList order;
  297. final int index;
  298. final callback;
  299. OrderCardContainer({
  300. Key key,
  301. @required this.order,
  302. @required this.index,
  303. @required this.callback
  304. }): super(key: key);
  305. _OrderCardContainerState createState() => _OrderCardContainerState();
  306. }
  307. class _OrderCardContainerState extends State < OrderCardContainer > with SingleTickerProviderStateMixin {
  308. Animation < double > animation;
  309. AnimationController controller;
  310. String str = "联系方式";
  311. bool animationStatus = true;
  312. @override
  313. initState() {
  314. super.initState();
  315. controller = new AnimationController(
  316. duration: const Duration(milliseconds: 200), vsync: this);
  317. animation = new Tween(begin: 0.0, end: 230.0).animate(controller);
  318. animation.addStatusListener((status) {
  319. if (status == AnimationStatus.completed) {
  320. //动画执行结束时反向执行动画
  321. setState(() {
  322. animationStatus = false;
  323. str = "收起";
  324. });
  325. } else if (status == AnimationStatus.dismissed) {
  326. //动画恢复到初始状态时执行动画(正向)
  327. setState(() {
  328. animationStatus = true;
  329. str = "联系方式";
  330. });
  331. }
  332. });
  333. }
  334. @override
  335. Widget build(BuildContext context) {
  336. return Card(
  337. color: Colors.white,
  338. elevation: 3.0,
  339. child: Container(
  340. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), 0),
  341. child: Column(
  342. children: < Widget > [
  343. orderNoRow(widget.order.orderNo, widget.order.createTime),
  344. orderAddress(widget.order.receiptCompanyName, widget.order.sendCompanyName),
  345. orderItem('货物名称', widget.order.goods, 15),
  346. orderItem('货物件数', '${widget.order.goodsQuantity}件', 30),
  347. orderCutLine(),
  348. orderBtn(context, animationStatus, controller, str),
  349. AnimatedOpen(animation: animation, openInfo: widget.order, )
  350. ],
  351. ),
  352. )
  353. );
  354. }
  355. dispose() {
  356. //路由销毁时需要释放动画资源
  357. controller.dispose();
  358. super.dispose();
  359. }
  360. Widget orderBtn(BuildContext context, bool animationStatus, AnimationController controller, String str) {
  361. return Container(
  362. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  363. child: Row(
  364. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  365. children: < Widget > [
  366. GestureDetector(
  367. child: Container(
  368. width: ScreenUtil.getInstance().setWidth(100),
  369. height: ScreenUtil.getInstance().setHeight(48),
  370. color: Colors.white,
  371. child: Center(
  372. child: Text(str, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(64, 98, 254, 1)), ),
  373. )
  374. ),
  375. onTap: () {
  376. if (animationStatus) {
  377. //动画恢复到初始状态时执行动画(正向)
  378. controller.forward();
  379. } else {
  380. //动画执行结束时反向执行动画
  381. controller.reverse();
  382. }
  383. },
  384. ),
  385. GestureDetector(
  386. child: Container(
  387. width: ScreenUtil.getInstance().setWidth(94),
  388. height: ScreenUtil.getInstance().setWidth(48),
  389. decoration: BoxDecoration(
  390. color: Color.fromRGBO(252, 97, 128, 1),
  391. borderRadius: BorderRadius.all(Radius.circular(4.0))
  392. ),
  393. child: Center(
  394. child: Text("接单", style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Colors.white), ),
  395. ),
  396. ),
  397. onTap: () {
  398. getKey('stationId').then((value) {
  399. print(value);
  400. });
  401. showDialog < Null > (
  402. context: context, //BuildContext对象
  403. barrierDismissible: false,
  404. builder: (BuildContext context) {
  405. return new LoadingDialog( //调用对话框
  406. text: '是否确认接单', childCallback: (val) {
  407. if (val) {
  408. widget.callback(widget.index);
  409. } else {
  410. return;
  411. }
  412. },
  413. );
  414. });
  415. },
  416. )
  417. ],
  418. )
  419. );
  420. }
  421. }
  422. class AnimatedOpen extends AnimatedWidget {
  423. final OrderList openInfo;
  424. AnimatedOpen({
  425. Key key,
  426. Animation < double > animation,
  427. @required this.openInfo
  428. }): super(key: key, listenable: animation);
  429. Widget build(BuildContext context) {
  430. final Animation < double > animation = listenable;
  431. return Container(
  432. height: ScreenUtil.getInstance().setHeight(animation.value),
  433. child: orderDetailInfo(openInfo),
  434. );
  435. }
  436. }
  437. Widget orderNoRow(String orderNo, int createTime) { //订单号及时间
  438. return Container(
  439. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
  440. child: Row(
  441. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  442. children: < Widget > [
  443. Text("订单号:${orderNo}", style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  444. Text('${createTime}', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  445. ],
  446. ),
  447. );
  448. }
  449. Widget orderAddress(String startAddress, String endAddress) { //运送路线
  450. return Container(
  451. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  452. child: Row(children: < Widget > [
  453. Text('${startAddress} ---> ${endAddress}', style: TextStyle(color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(30)), ),
  454. ], )
  455. );
  456. }
  457. Widget orderCutLine() { //分割线
  458. return Container(
  459. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
  460. height: ScreenUtil.getInstance().setHeight(1),
  461. color: Color.fromRGBO(223, 223, 223, 1),
  462. );
  463. }
  464. Widget orderDetailInfo(OrderList obj) { //卡片展开信息
  465. return Container(
  466. child: Wrap(
  467. children: < Widget > [
  468. orderCutLine(),
  469. orderPhone('发件电话', obj.sendCompanyPhone, 15),
  470. orderItem('发件地址', obj.sendCompanyAddress, 30),
  471. orderPhone('收件电话', obj.receiptCompanyPhone, 15),
  472. orderItem('收件地址', obj.receiptCompanyAddress, 30),
  473. ],
  474. )
  475. );
  476. }
  477. Widget orderItem(String key, String value, int marginBottom) { //货物名称,件数
  478. return Container(
  479. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(marginBottom)),
  480. child: Row(children: < Widget > [
  481. Text('${key}:${value}', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  482. ], )
  483. );
  484. }
  485. Widget orderPhone(String key, String value, int marginBottom) {
  486. return InkWell(
  487. child: Container(
  488. margin: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(marginBottom)),
  489. child: Row(children: < Widget > [
  490. Text('${key}:', style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  491. Text('${value}', style: TextStyle(color: Color.fromRGBO(64, 98, 254, 1), fontSize: ScreenUtil.getInstance().setSp(24)), ),
  492. ], )
  493. ),
  494. onLongPress: () async {
  495. String phone = 'tel:${value}';
  496. print(phone);
  497. if (await canLaunch(phone)) {
  498. await (launch(phone));
  499. } else {
  500. throw 'Could not launch $value';
  501. }
  502. },
  503. );
  504. }