order_taking_page.dart 15 KB

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