order_taking_page.dart 15 KB

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