order_taking_page.dart 17 KB

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