import 'dart:async'; import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/order/invoices_manage/pdf_screen.dart'; import 'package:path_provider/path_provider.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:shimmer/shimmer.dart'; import 'package:url_launcher/url_launcher.dart'; import '../../community/photo_view_gallery_screen.dart'; import '../../retrofit/data/base_data.dart'; import '../../retrofit/data/invoices_detail_info.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'; class InvoicesDetailPage extends StatefulWidget { final Map arguments; InvoicesDetailPage({this.arguments}); @override State createState() { return _InvoicesDetailPage(); } } class _InvoicesDetailPage extends State { ApiService apiService; InvoicesDetailInfo invoicesDetailInfo; String remotePDFpath = ""; String networkError = ""; int networkStatus = 0; final RefreshController refreshController = RefreshController(); @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService( Dio(), context: context, token: value.getString('token'), ), queryInvoices(widget?.arguments["id"] ??""), }); } ///离开页面记着销毁和清除 @override void dispose() { super.dispose(); refreshController.dispose(); } ///发票详情 queryInvoices(id) async { if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService( Dio(), context: context, token: value.getString("token"), ); } BaseData baseData = await apiService.invoiceDetail(id).catchError((error) { networkError = AppUtils.dioErrorTypeToString(error.type); networkStatus = -1; setState(() {}); refreshController.refreshFailed(); refreshController.loadFailed(); }); if (baseData != null && baseData.isSuccess) { invoicesDetailInfo = baseData.data; createFileOfPdfUrl().then((f) { setState(() { remotePDFpath = f.path; }); }); refreshController.refreshCompleted(); networkStatus = 1; setState(() {}); } else { refreshController.refreshFailed(); refreshController.loadFailed(); SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } ///从网络下载 PDF 文件。 Future createFileOfPdfUrl() async { Completer completer = Completer(); print("Start download file from internet!"); try { final url = invoicesDetailInfo?.ossUrl ?? ""; final filename = url.substring(url.lastIndexOf("/") + 1); var request = await HttpClient().getUrl(Uri.parse(url)); var response = await request.close(); var bytes = await consolidateHttpClientResponseBytes(response); var dir = await getApplicationDocumentsDirectory(); print("Download files"); print("${dir.path}/$filename"); File file = File("${dir.path}/$filename"); await file.writeAsBytes(bytes, flush: true); completer.complete(file); } catch (e) { throw Exception('Error parsing asset file!'); } return completer.future; } @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() : (networkStatus == 0 ? invoicesSm() : SmartRefresher( controller: refreshController, enablePullDown: true, header: MyHeader(), footer: CustomFooter( builder: (context, mode) { return MyFooter(mode); }, ), onRefresh: () { queryInvoices(widget?.arguments["id"] ?? ""); }, physics: BouncingScrollPhysics(), scrollController: ScrollController(), child: Container( margin: EdgeInsets.only(top: 12.h), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Container( padding: EdgeInsets.symmetric(horizontal: 17.w), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( padding: EdgeInsets.symmetric( vertical: 16.h, horizontal: 18.w), margin: EdgeInsets.symmetric(vertical: 12.h), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(6), boxShadow: [ BoxShadow( color: Color(0x0F06152E), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "电子发票资讯", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 15.sp, fontWeight: MyFontWeight.bold, ), ), Padding( padding: EdgeInsets.only( top: 16.h, bottom: 18.h), child: Row( children: [ Expanded( child: Text( "发票状态", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Text( invoicesState( invoicesDetailInfo?.state ?? ""), style: TextStyle( color: Color(0xFF32A060), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ), ], ), ), Padding( padding: EdgeInsets.only(bottom: 18.h), child: Row( children: [ Expanded( child: Text( "发票抬头", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Expanded( child: Text( invoicesDetailInfo ?.invoiceHeaderName ?? "", maxLines: 1, overflow: TextOverflow.ellipsis, textAlign: TextAlign.end, style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), ], ), ), Padding( padding: EdgeInsets.only(bottom: 18.h), child: Row( children: [ Expanded( child: Text( "申请时间", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Text( invoicesDetailInfo?.reviewerTime ?? "", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ), ], ), ), Row( children: [ Expanded( child: Text( "开票金额", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Text( invoicesDetailInfo?.money ?? "", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ), ], ) ], ), ), GestureDetector( onTap: () { if ((invoicesDetailInfo?.ossUrl ?? "") .endsWith("pdf")) { Navigator.push( context, MaterialPageRoute( builder: (context) => PDFScreen( path: remotePDFpath, inlet: 0, ), ), ); } else { Navigator.push( context, MaterialPageRoute( builder: (context) => PhotoViewGalleryScreen( images: [ invoicesDetailInfo?.ossUrl ?? "" ], //传入图片list index: 0, //传入当前点击的图片的index ), )); } }, child: Container( padding: EdgeInsets.symmetric( vertical: 16.h, horizontal: 17.w), margin: EdgeInsets.symmetric(vertical: 12.h), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(6), boxShadow: [ BoxShadow( color: Color(0x0F06152E), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], ), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Text( "1张发票,含${invoicesDetailInfo?.orderList?.length ?? 0}个订单", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Padding( padding: EdgeInsets.only(right: 6.w), child: Text( "查看", style: TextStyle( color: Color(0xFF1A1A1A), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ), ), Icon(Icons.arrow_forward_ios, color: Color(0xFF181818), size: 14.sp) ], ), ), ), ], ), )), Row( children: [ Expanded( child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { if ((invoicesDetailInfo?.ossUrl ?? "") .endsWith("pdf")) { showPdfAlertDialog(); } else { Navigator.push( context, MaterialPageRoute( builder: (context) => PhotoViewGalleryScreen( images: [ invoicesDetailInfo?.ossUrl ?? "" ], //传入图片list index: 0, //传入当前点击的图片的index ), )); } }, child: Container( padding: EdgeInsets.symmetric(vertical: 16.h), margin: EdgeInsets.only( bottom: 34.h, right: 9.5.w, left: 14.w), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(45), border: Border.all( color: Color(0xff32A060), width: 1.w, ), ), width: double.infinity, alignment: Alignment.center, child: Text( "查看电子发票", style: TextStyle( color: Color(0xff32A060), fontSize: 16.sp, fontWeight: MyFontWeight.medium, ), ), ), )), Expanded( child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { launchBrowser(); }, child: Container( padding: EdgeInsets.symmetric(vertical: 16.h), margin: EdgeInsets.only( bottom: 34.h, left: 9.5.w, right: 14.w), decoration: BoxDecoration( color: Color(0xff32A060), borderRadius: BorderRadius.circular(45), ), width: double.infinity, alignment: Alignment.center, child: Text( "下载电子发票", style: TextStyle( color: Colors.white, fontSize: 16.sp, fontWeight: MyFontWeight.medium, ), ), ), )), ], ) ], ), ), )), ); } ///发票状态 String invoicesState(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 ""; } } ///查看电子发票 showPdfAlertDialog() { //显示对话框 showDialog( context: context, builder: (BuildContext context) { return PDFScreen(path: remotePDFpath, inlet: 1); }, ); } ///下载电子发票 launchBrowser() async { String url = invoicesDetailInfo?.ossUrl ?? ""; if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } Widget invoicesSm() { return Container( margin: EdgeInsets.only(top: 12.h), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Container( padding: EdgeInsets.symmetric(horizontal: 17.w), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 18.w), margin: EdgeInsets.symmetric(vertical: 12.h), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(6), boxShadow: [ BoxShadow( color: Color(0x0F06152E), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 90.w, height: 20.h, ), ), Padding( padding: EdgeInsets.only(top: 16.h, bottom: 18.h), child: Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), ], ), ), Padding( padding: EdgeInsets.only(bottom: 18.h), child: Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), ], ), ), Padding( padding: EdgeInsets.only(bottom: 18.h), child: Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), ], ), ), Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 58.w, height: 20.h, ), ), ], ) ], ), ), Container( padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 17.w), margin: EdgeInsets.symmetric(vertical: 12.h), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(6), boxShadow: [ BoxShadow( color: Color(0x0F06152E), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], ), child: Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 124.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( margin: EdgeInsets.only(right: 10), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 28.w, height: 20.h, ), ), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 18.w, height: 20.h, ), ), ], ), ), ], ), )), Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( // padding: EdgeInsets.symmetric(vertical: 16.h), margin: EdgeInsets.only(bottom: 34.h, right: 9.5.w, left: 14.w), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(45), ), width: 164.w, height: 48.h, ), ), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( // padding: EdgeInsets.symmetric(vertical: 16.h), margin: EdgeInsets.only(bottom: 34.h, right: 9.5.w, left: 14.w), decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(45), ), width: 164.w, height: 48.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: () { queryInvoices(widget?.arguments["id"] ?? ""); }, 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), )), ) ], ), ); } }