import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/order_info.dart'; import 'package:huixiang/retrofit/data/page.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/status_utils.dart'; import 'package:huixiang/view_widget/classic_header.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:huixiang/view_widget/my_footer.dart'; import 'package:huixiang/view_widget/my_tab.dart'; import 'package:huixiang/view_widget/no_data_view.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class OrderHistoryPage extends StatefulWidget { @override State createState() { return _OrderHistoryPage(); } } class _OrderHistoryPage extends State with SingleTickerProviderStateMixin { List _pages; TabController tabcontroller; @override void didChangeDependencies() { super.didChangeDependencies(); if (tabcontroller == null) tabcontroller = TabController(length: 4, vsync: this); _pages = [ OrderHistoryList(0), OrderHistoryList(1), OrderHistoryList(2), OrderHistoryList(3) ]; } @override Widget build(BuildContext context) { return DefaultTabController( length: 3, child: Scaffold( appBar: MyAppBar( title: S.of(context).dingdan, titleColor: Colors.black, titleSize: 18.sp, background: Color(0xFFFFFFFF), leadingColor: Colors.black, toolbarHeight: kToolbarHeight + MediaQuery.of(context).padding.top, bottom: PreferredSize( preferredSize: Size(double.infinity, 38.h), child: TabBar( controller: tabcontroller, isScrollable: false, indicatorWeight: 2.w, indicatorSize: TabBarIndicatorSize.label, indicatorColor: Color(0xFF39B54A), indicatorPadding: EdgeInsets.only(top: 3.h), unselectedLabelStyle: TextStyle( fontSize: 16.sp, fontWeight: FontWeight.normal, ), labelStyle: TextStyle( color: Colors.black, fontSize: 16.sp, fontWeight: FontWeight.bold, ), labelColor: Colors.black, tabs: [ MyTab( text: S.of(context).quanbu, ), MyTab( text: S.of(context).daifukuan, ), MyTab( text: S.of(context).weiwancheng, ), MyTab( text: S.of(context).yiwancheng, ) ], ), ), ), body: TabBarView( children: _pages, controller: tabcontroller, ), ), ); } } class OrderHistoryList extends StatefulWidget { final int orderStatus; OrderHistoryList(this.orderStatus); @override State createState() { return _OrderHistoryList(); } } class _OrderHistoryList extends State with AutomaticKeepAliveClientMixin { final RefreshController refreshController = RefreshController(); @override Widget build(BuildContext context) { super.build(context); return SmartRefresher( controller: refreshController, enablePullDown: true, enablePullUp: true, physics: BouncingScrollPhysics(), header: MyHeader(), footer: CustomFooter( builder: (context, mode) { return MyFooter(mode); }, ), onRefresh: _onRefresh, onLoading: queryOrder, child: (orderInfos != null && orderInfos.length > 0) ? ListView.builder( itemCount: orderInfos != null ? orderInfos.length : 0, itemBuilder: (context, position) { return InkWell( onTap: () { Navigator.of(context).pushNamed('/router/order_details', arguments: {"id": orderInfos[position].id}); }, child: orderItem(orderInfos[position]), ); }) : NoDataView( isShowBtn: false, text: "还没有订单,快去下一单吧~", fontSize: 16.sp, margin: EdgeInsets.only(top: 120), ), ); } bool isRemake = true; ApiService apiService; int current = 1; _onRefresh() { current = 1; queryOrder(); } List orderInfos = []; queryOrder() async { BaseData baseData = await apiService.orderList({ "current": current, "model": {"status": widget.orderStatus}, "order": "descending", "size": 10, "sort": "id" }).catchError((onError) { refreshController.refreshFailed(); refreshController.loadFailed(); }); if (baseData != null && baseData.isSuccess) { PageInfo pageInfo = PageInfo.fromJson(baseData.data); if (current == 1) { orderInfos.clear(); } orderInfos .addAll(pageInfo.records.map((e) => OrderInfo.fromJson(e)).toList()); refreshController.refreshCompleted(); refreshController.loadComplete(); if (current * 10 > int.tryParse(pageInfo.total)) { refreshController.loadNoData(); } else { current += 1; } setState(() {}); } else { refreshController.refreshFailed(); refreshController.loadFailed(); } } @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService(Dio(), token: value.getString("token")), queryOrder(), }); } Widget orderItem(OrderInfo orderInfo) { return Container( margin: EdgeInsets.fromLTRB(16.w, 8.h, 16.w, 8.h), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(4), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(25), offset: Offset(0, 1), blurRadius: 12, spreadRadius: 0, ) ]), child: Column( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: 19.w, height: 19.h, margin: EdgeInsets.only(left: 12.w, top: 12.h), alignment: Alignment.center, decoration: BoxDecoration( color: Color(0xff32A060), borderRadius: BorderRadius.circular(2), ), child: Text( (orderInfo != null && orderInfo.isTakeOut == 0) ? "自" : "外", style: TextStyle( fontSize: 12.sp, fontWeight: FontWeight.bold, color: Colors.white, ), ), ), Expanded( child: Container( width: double.infinity, margin: EdgeInsets.only(left: 6.w, top: 12.h), alignment: Alignment.centerLeft, child: Text( (orderInfo != null) ? orderInfo.storeName : "", style: TextStyle( fontWeight: FontWeight.bold, fontSize: 14.sp, color: Color(0xFF353535), ), ), ), flex: 1, ), Padding( padding: EdgeInsets.only(top: 12.h, right: 12.w), child: Text( (orderInfo != null && orderInfo.storeVO != null && orderInfo.storeVO.posType != null) ? StatusUtils.statusText( context, orderInfo.refundStatus, orderInfo.orderStatus, orderInfo.payStatus, orderInfo.sendStatus, orderInfo.isTakeOut) : "", style: TextStyle( fontSize: 14.sp, fontWeight: FontWeight.bold, color: (orderInfo == null) ? Color(0xFF32A060) : (orderInfo.refundStatus == 1 || orderInfo.orderStatus >= 5) ? Colors.grey : (orderInfo.orderStatus == 4) ? Color(0xFF32A060) : Color(0xffFE951E), ), ), ), ], ), Container( margin: EdgeInsets.only(left: 37.w), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( S.of(context).xiadanshijian_( (orderInfo != null) ? orderInfo.createTime : ""), style: TextStyle( fontSize: 10.sp, color: Color(0xFF727272), ), ), SizedBox( height: 8.h, ), Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( child: Container( child: Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: goodsItem((orderInfo != null && orderInfo.productList != null) ? orderInfo.productList : null), ), ), flex: 1, ), Padding( padding: EdgeInsets.all(22.w), child: Image.asset( "assets/image/icon_order_more.png", width: 24.w, height: 24.h, ), ) ], ), ], ), ), SizedBox( height: 12.h, ), Container( margin: EdgeInsets.only(right: 12.w, bottom: 12.h), child: Directionality( textDirection: TextDirection.rtl, child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text.rich( TextSpan( children: [ TextSpan( text: S.of(context).gong, style: TextStyle( fontSize: 12.sp, color: Color(0xFF868686), ), ), TextSpan( text: (orderInfo != null && orderInfo.productList != null) ? "${orderInfo.productList.length}" : "0", style: TextStyle( fontSize: 12.sp, fontWeight: FontWeight.bold, color: Colors.black, ), ), TextSpan( text: S.of(context).jian, style: TextStyle( fontSize: 12.sp, color: Color(0xFF868686), ), ), ], ), ), SizedBox( width: 4.w, ), Text.rich( TextSpan( children: [ TextSpan( text: S.of(context).heji, style: TextStyle( fontSize: 12.sp, color: Color(0xFF868686), ), ), TextSpan( text: totalPrice(orderInfo), style: TextStyle( fontSize: 12.sp, fontWeight: FontWeight.bold, color: Colors.black, ), ), TextSpan( text: S.of(context).yuan, style: TextStyle( fontSize: 12.sp, color: Color(0xFF868686), ), ), ], ), ), ], ), SizedBox( height: 8.h, ), Row( children: (orderInfo != null) ? StatusUtils.statusBtn( context, orderInfo.payStatus, orderInfo.orderStatus, orderInfo.isTakeOut, orderInfo.sendStatus, orderInfo.refundStatus, orderInfo.dayFlowCode, orderInfo.logisticsName, orderInfo.logisticsNum, orderInfo.shipperCode, orderInfo.productList.length, orderInfo.productList.length > 0 ? orderInfo.productList[0].skuImg : "") : [], ), ], ), ), ), ], ), ); } String totalPrice(orderInfo) { if (orderInfo == null) return ""; double totalPrice = (double.tryParse(orderInfo.orderSum) + double.tryParse(orderInfo.postFee)); if (orderInfo.orderDetail != null && orderInfo.orderDetail.couponDTO != null) { totalPrice -= double.tryParse(orderInfo.orderDetail.couponDTO.money); } return "$totalPrice"; } List goodsItem(List products) { if (products == null) return []; if (products.length > 3) { products = products.sublist(0, 3); } return products .map( (e) => Container( margin: EdgeInsets.symmetric(horizontal: 2.w), child: Column( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center, children: [ MImage( e.skuImg, width: 75.w, height: 75.w, fit: BoxFit.cover, errorSrc: "assets/image/default_1.png", fadeSrc: "assets/image/default_1.png", ), SizedBox( height: 4.h, ), if (isRemake) Container( width: 75.w, child: Text( e.productName, maxLines: 1, textAlign: TextAlign.center, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 10.sp, color: Color(0xFF353535), ), ), ), ], ), ), ) .toList(); } @override bool get wantKeepAlive => true; }