order_taking_page.dart 12 KB

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