import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/exchange_order.dart'; import 'package:huixiang/retrofit/data/page.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/view_widget/border_text.dart'; import 'package:huixiang/view_widget/icon_text.dart'; import 'package:huixiang/view_widget/my_tab.dart'; import 'package:huixiang/view_widget/no_data_view.dart'; import 'package:huixiang/view_widget/round_button.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; class ExchangeHistoryPage extends StatefulWidget { @override State createState() { return _ExchangeHistoryPage(); } } class _ExchangeHistoryPage extends State with SingleTickerProviderStateMixin { List tabs; List _pages; TabController tabcontroller; @override void initState() { super.initState(); } @override void didChangeDependencies() { super.didChangeDependencies(); if (tabcontroller == null) tabcontroller = TabController(length: 3, vsync: this); tabs = [ MyTab( text: S.of(context).quanbuduihuan, ), MyTab( text: S.of(context).weiwancheng, ), // MyTab( // text: S.of(context).keshiyong, // ), MyTab( text: S.of(context).yiwancheng, ) ]; _pages = [ ExchangeHistoryList(0), ExchangeHistoryList(1), ExchangeHistoryList(2), // ExchangeHistoryList(3) ]; } @override Widget build(BuildContext context) { return DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( title: Text( S.of(context).duihuanlishi, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18, ), ), centerTitle: false, backgroundColor: Color(0xFFFFFFFF), elevation: 0, leading: GestureDetector( onTap: () { Navigator.of(context).pop(); }, child: Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(left: 10), padding: EdgeInsets.all(6), child: Icon( Icons.arrow_back_ios, color: Colors.black, size: 24, ), ), ), titleSpacing: 2, leadingWidth: 56, bottom: PreferredSize( preferredSize: Size(double.infinity, 38), child: TabBar( controller: tabcontroller, indicatorWeight: 2, indicatorColor: Color(0xFF39B54A), indicatorSize: TabBarIndicatorSize.label, indicatorPadding: EdgeInsets.only(top: 3), unselectedLabelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.normal), labelStyle: TextStyle( color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold), labelColor: Colors.black, tabs: tabs, ), ), ), body: TabBarView( children: _pages, controller: tabcontroller, ), ), ); } } class ExchangeHistoryList extends StatefulWidget { final int orderStatus; ExchangeHistoryList(this.orderStatus); @override State createState() { return _ExchangeHistoryList(); } } class _ExchangeHistoryList extends State { ApiService apiService; RefreshController _refreshController = RefreshController(initialRefresh: false); @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService(Dio(), token: value.getString('token')), queryHistory(), }); } int pageNum = 1; PageInfo pageInfo; List orders = []; queryHistory() async { var map = { "pageNum": pageNum, "pageSize": 10, // "state": widget.orderStatus }; if (widget.orderStatus != 0) { map["state"] = widget.orderStatus; } BaseData baseData = await apiService.creditOrderList(map).catchError((error) { _refreshController.loadFailed(); _refreshController.refreshFailed(); }); if (baseData.isSuccess) { pageInfo = PageInfo.fromJson(baseData.data); if (pageNum == 1) { orders.clear(); } orders.addAll(pageInfo.list.map((e) => ExchangeOrder.fromJson(e))); setState(() { _refreshController.loadComplete(); _refreshController.refreshCompleted(); if (pageInfo.pages == pageInfo.pageNum) { _refreshController.loadNoData(); } else { pageNum += 1; } }); } else { _refreshController.refreshFailed(); _refreshController.loadFailed(); Fluttertoast.showToast(msg: baseData.msg); } } _refresh() { pageNum = 1; queryHistory(); } @override Widget build(BuildContext context) { return SmartRefresher( enablePullDown: true, enablePullUp: true, header: WaterDropHeader(), footer: CustomFooter( builder: (BuildContext context, LoadStatus mode) { Widget body; if (mode == LoadStatus.idle) { body = Text("pull up load"); } else if (mode == LoadStatus.loading) { body = CupertinoActivityIndicator(); } else if (mode == LoadStatus.failed) { body = Text("Load Failed!Click retry!"); } else if (mode == LoadStatus.canLoading) { body = Text("release to load more"); } else { body = Text("No more Data"); } return Container( height: 55.0, child: Center(child: body), ); }, ), controller: _refreshController, onRefresh: _refresh, onLoading: queryHistory, child: orders == null || orders.length == 0 ? NoDataView( isShowBtn: false, text: "暂无已完成的订单", fontSize: 16, margin: EdgeInsets.only(top: 120), ) : ListView.builder( itemCount: orders == null ? 0 : orders.length, itemBuilder: (context, position) { return buildOrder(orders[position]); }, ), ); } String orderStatus(state) { String orderStatus = ""; switch (state) { case 1: orderStatus = S.of(context).weiwancheng; break; case 2: orderStatus = S.of(context).yiwancheng; break; case 9: orderStatus = S.of(context).yiquxiao; break; } return orderStatus; } Widget buildOrder(ExchangeOrder exchangeOrder) { return Container( margin: EdgeInsets.fromLTRB(16, 8, 16, 8), padding: EdgeInsets.all(12), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0) ]), child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( S.of(context).chuangjianshijian(exchangeOrder.createTime), style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 14, ), ), Text( orderStatus(exchangeOrder.state), style: TextStyle( color: Color(0xFFFE951E), fontWeight: FontWeight.bold, fontSize: 14, ), ) ], ), SizedBox( height: 16, ), Row( children: [ Text( S.of(context).zitidizhi, style: TextStyle( color: Color(0xFF353535), fontSize: 12, ), ), SizedBox( width: 8, ), Expanded( child: Text( "武昌区凯德1818蓝调步行街(蔡明纬店旁)", style: TextStyle( color: Colors.black, fontSize: 12, ), ), ) ], ), SizedBox( height: 8, ), Row( children: [ Text( S.of(context).zitishijian, style: TextStyle( color: Color(0xFF353535), fontSize: 12, ), ), SizedBox( width: 8, ), Expanded( child: Text( S.of(context).duihuanhouwugegongzuori, style: TextStyle( color: Colors.black, fontSize: 12, ), ), ) ], ), SizedBox( height: 16, ), Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Image.network( (exchangeOrder != null && exchangeOrder.creditOrderDetailList != null) ? exchangeOrder.creditOrderDetailList[0].goodsMainImg : "", errorBuilder: (context, error, stackTrace) { return Image.asset("assets/image/default_1.png", fit: BoxFit.cover); }, width: 66, height: 66, fit: BoxFit.cover, ), SizedBox( width: 12, ), Expanded( child: SizedBox( height: 66, child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( exchangeOrder.creditOrderDetailList[0].name, style: TextStyle( fontSize: 14, fontWeight: FontWeight.bold, color: Color(0xFF353535)), ), SizedBox( height: 2, ), Text( exchangeOrder.useTyped == 3 ? S.of(context).feishiwuduihuanma : exchangeOrder .creditOrderDetailList[0].description, style: TextStyle(fontSize: 10, color: Color(0xFF727272)), ), ], ), ), flex: 1, ), Text( "x1", style: TextStyle( fontSize: 12, color: Color(0xFF353535), ), ) ], ), Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.end, children: [ // Text( // S.of(context).shangpinjifen(90), // style: TextStyle( // fontSize: 12, // color: Color(0xFFA0A0A0), // ), // ), // SizedBox( // width: 4, // ), Text( S.of(context).shifujifen(exchangeOrder.amount), style: TextStyle( fontSize: 12, fontWeight: FontWeight.bold, color: Colors.black, ), ), ], ), SizedBox( height: 12, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ IconText( S.of(context).youxiaoqizhi(exchangeOrder.updateTime), leftImage: "assets/image/icon_order_time.png", iconSize: 16, textStyle: TextStyle( fontSize: 12, color: Color(0xFF353535), ), ), GestureDetector( onTap: () { if (exchangeOrder.state == 1) { receive2Card(exchangeOrder.id); } else { Navigator.of(context).pushNamed('/router/write_off_page'); } }, child: buildBtnStatus(exchangeOrder.state), ), ], ) ], ), ); } receive2Card(id) async { BaseData baseData = await apiService.creditOrderReceive(id); if (baseData.isSuccess) { queryHistory(); } else { Fluttertoast.showToast(msg: baseData.msg); } } Widget buildBtnStatus(state) { if (state == 1) { return RoundButton( padding: EdgeInsets.fromLTRB(8, 4, 8, 4), text: S.of(context).lingqudaokabao, textColor: Colors.white, fontSize: 12, backgroup: Color(0xFF32A060), radius: 2, ); } else { return Container(); // return BorderText( // text: S.of(context).shanchudingdan, // textColor: Color(0xFF32A060), // borderWidth: 1, // borderColor: Color(0xFF32A060), // fontSize: 12, // padding: EdgeInsets.fromLTRB(14, 4, 14, 4), // ); } } }