import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:intl/intl.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:shimmer/shimmer.dart'; import '../../generated/l10n.dart'; import '../../retrofit/data/base_data.dart'; import '../../retrofit/data/invoices_history_list.dart'; import '../../retrofit/retrofit_api.dart'; import '../../utils/flutter_utils.dart'; import '../../utils/font_weight.dart'; import '../../view_widget/classic_header.dart'; import '../../view_widget/my_appbar.dart'; import '../../view_widget/my_footer.dart'; import '../../view_widget/no_data_view.dart'; class InvoicesHistory extends StatefulWidget { @override State createState() { return _InvoicesHistory(); } } class _InvoicesHistory extends State { final RefreshController refreshController = RefreshController(); ApiService apiService; String networkError = ""; int networkStatus = 0; List records = []; int _current = 1; Map> _map = {}; String _timeList; @override void initState() { super.initState(); _onRefresh(); } ///离开页面记着销毁和清除 @override void dispose() { super.dispose(); refreshController.dispose(); } ///开票列表 queryInvoiceList() async { if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService(Dio(), context: context, token: value.getString("token"), showLoading: false); } BaseData baseData = await apiService.invoicePage({ "current": _current, "size": 10, "searchKey": "", }).catchError((error) { networkError = AppUtils.dioErrorTypeToString(error.type); networkStatus = -1; setState(() {}); refreshController.refreshFailed(); refreshController.loadFailed(); }); if (!mounted) return; if (baseData != null && baseData.isSuccess) { records.addAll(baseData?.data?.records ?? []); (baseData?.data?.records ?? []).forEach((element) { if(_map.containsKey((element.reviewerTime ?? element.createTime).toString().substring(0,7))){ _map[(element.reviewerTime ?? element.createTime).toString().substring(0,7)].add(element); }else{ _map[(element.reviewerTime ?? element.createTime).toString().substring(0,7)] = [element]; } }); if(_map.isNotEmpty) _timeList = _map.keys.toList().first; if ((baseData?.data?.records ?? []).isEmpty || records.length.toString() == baseData.data.total) refreshController.loadNoData(); else refreshController.loadComplete(); networkStatus = 1; } else { SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } _onRefresh({bool isShowLoad = true}) async { if (isShowLoad) EasyLoading.show( status: S.current.zhengzaijiazai, maskType: EasyLoadingMaskType.black); await queryInvoiceList(); EasyLoading.dismiss(); if (!mounted) return; if (refreshController.isRefresh) refreshController.refreshCompleted(); setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: false, appBar: MyAppBar( title: "开票历史", titleColor: Colors.black, background: Colors.white, leadingColor: Colors.black, ), body: networkStatus == -1 ? noNetwork() : Container( child: SmartRefresher( controller: refreshController, enablePullDown: true, enablePullUp: records.length == 0 ? false : true, header: MyHeader(), footer: CustomFooter( builder: (context, mode) { return MyFooter(mode); }, ), onRefresh: () { _current = 1; records.clear(); _onRefresh(isShowLoad: false); }, onLoading: () { _current++; _onRefresh(isShowLoad: false); }, physics: BouncingScrollPhysics(), scrollController: ScrollController(), child: Container( child: networkStatus == 0 ? Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(bottom: 16.h,top: 16.h,left: 17.w), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 70.w, height: 20.h, ), ), Expanded(child: ListView.builder( itemCount: 10, physics: BouncingScrollPhysics(), shrinkWrap: true, itemBuilder: (context, position) { return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () {}, child: invoicesHistoryListSm(), ); }, )), ], ) : ((records == null || records.length == 0) ? NoDataView( src: "assets/image/ding_dan.webp", isShowBtn: false, text: "暂无开票记录", fontSize: 16, margin: EdgeInsets.only(top: 120,left: 20,right: 20), ) : Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: EdgeInsets.only(top: 16.h, bottom: 16.h, left: 17.w), child: Text( _timeList.toString(), style: TextStyle( color: Color(0xff1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Expanded(child: ListView.builder( itemCount: records?.length ?? 0, physics: BouncingScrollPhysics(), shrinkWrap: true, itemBuilder: (context, position) { return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { if(records[position].state == "AUDIT_PASS") Navigator.of(context) .pushNamed('/router/invoices_detail_page',arguments: { "id":records[position].id ?? "" }); }, child: invoicesHistoryList(records[position]), ); }, )) ], )),), ), ), ); } Widget invoicesHistoryList(Records records) { return Container( padding: EdgeInsets.symmetric(vertical: 14.h, horizontal: 12.w), margin: EdgeInsets.only(left: 12.w, right: 12.w, bottom: 12.h), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Color(0x0F06152E), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], borderRadius: BorderRadius.circular(8), ), child: Column( children: [ Padding( padding: EdgeInsets.only(bottom: 25.h), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Expanded( child: Text( records?.invoiceHeaderName ?? "", style: TextStyle( color: Colors.black, fontSize: 14.sp, fontWeight: MyFontWeight.bold, ), )), Text( invoicesStatus(records?.state ?? ""), style: TextStyle( color: Color(0xff32A060), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ) ], ), ), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Padding( padding: EdgeInsets.only(right: 16.w), child: Text( "¥${records?.money ?? ""}", style: TextStyle( color: Color(0xff32A060), fontSize: 16.sp, fontWeight: MyFontWeight.medium, ), )), Expanded( child: Text( "电子普票", style: TextStyle( color: Color(0xff32A060), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Text( records?.reviewerTime ?? records?.createTime ?? "", style: TextStyle( color: Color(0xff4D4D4D), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ) ], ) ], ), ); } ///发票状态 String invoicesStatus(state) { if (state == "UN_AUDIT") { return "待审核"; } else if (state =="AUDIT_PASS") { return "开票成功"; } else if (state =="AUDIT_VOID") { return "已作废"; }else if (state =="AUDIT_FAIL") { return "开票失败"; }else { return ""; } } Widget invoicesHistoryListSm() { return Container( padding: EdgeInsets.symmetric(vertical: 14.h, horizontal: 12.w), margin: EdgeInsets.symmetric(horizontal: 14.w, vertical: 12.h), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Color(0x0F06152E), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], borderRadius: BorderRadius.circular(8), ), child: Column( children: [ Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 24), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 180.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 25), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 42.w, height: 20.h, ), ), ], ), SizedBox( height: 25.h, ), Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 20), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 47.w, height: 20.h, ), ), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 56.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 25), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 80.w, height: 20.h, ), ), ], ), ], ), ); } Widget noNetwork() { return Container( width: double.infinity, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( // "无法连接到网络", networkError.substring(0, networkError.indexOf(",")), style: TextStyle( fontSize: 14.sp, color: Color(0xFF0D0D0D), fontWeight: MyFontWeight.bold), ), Padding( padding: EdgeInsets.symmetric(vertical: 10.h), child: Text( "请检查网络设置或稍后重试", style: TextStyle( fontSize: 12.sp, color: Color(0xFF7A797F), fontWeight: MyFontWeight.regular), ), ), GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { _onRefresh(); }, child: Container( decoration: BoxDecoration( color: Color(0xff32A060), borderRadius: BorderRadius.circular(15), ), padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 3.h), child: Text( "重试", style: TextStyle( fontSize: 14.sp, color: Colors.white, fontWeight: MyFontWeight.regular), )), ) ], ), ); } }