You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
6.7 KiB
210 lines
6.7 KiB
import 'package:dio/dio.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:huixiang/generated/l10n.dart'; |
|
import 'package:huixiang/data/base_data.dart'; |
|
import 'package:huixiang/data/coupon.dart'; |
|
import 'package:huixiang/data/page.dart'; |
|
import 'package:huixiang/retrofit/retrofit_api.dart'; |
|
import 'package:huixiang/view_widget/classic_header.dart'; |
|
import 'package:huixiang/view_widget/coupon_details_dialog.dart'; |
|
import 'package:huixiang/view_widget/item_title.dart'; |
|
import 'package:huixiang/view_widget/my_appbar.dart'; |
|
import 'package:huixiang/view_widget/my_footer.dart'; |
|
import 'package:huixiang/view_widget/new_coupon_widget.dart'; |
|
import 'package:huixiang/view_widget/no_data_view.dart'; |
|
import 'package:huixiang/view_widget/receive_success.dart'; |
|
import 'package:huixiang/view_widget/selector_store_dialog.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 RollCenterPage extends StatefulWidget { |
|
@override |
|
State<StatefulWidget> createState() { |
|
return _RollCenterPage(); |
|
} |
|
} |
|
|
|
class _RollCenterPage extends State<RollCenterPage> { |
|
late RefreshController _refreshController; |
|
|
|
ApiService? apiService; |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
|
|
SharedPreferences.getInstance().then((value) => { |
|
apiService = ApiService( |
|
Dio(), |
|
context: context, |
|
token: value.getString('token'), |
|
), |
|
queryCoupon(), |
|
}); |
|
_refreshController = RefreshController(initialRefresh: false); |
|
} |
|
|
|
int pageNum = 1; |
|
List<Coupon> coupons = []; |
|
|
|
queryCoupon() async { |
|
BaseData<PageInfo<Coupon>>? baseData = await apiService?.queryCoupon({ |
|
"centre": true, |
|
"pageNum": pageNum, |
|
"pageSize": 10, |
|
"searchKey": "", |
|
"state": 0 |
|
}).catchError((onError) { |
|
_refreshController?.refreshFailed(); |
|
_refreshController?.loadFailed(); |
|
}); |
|
if (pageNum == 1) coupons.clear(); |
|
if (baseData?.isSuccess ?? false) { |
|
if (baseData!.data != null && (baseData.data!.list?.isNotEmpty ?? false)) { |
|
coupons.addAll(baseData.data!.list!); |
|
} |
|
_refreshController.refreshCompleted(); |
|
_refreshController.loadComplete(); |
|
if (baseData.data?.pageNum == baseData.data?.pages) { |
|
_refreshController.loadNoData(); |
|
} else { |
|
pageNum += 1; |
|
} |
|
setState(() {}); |
|
} else { |
|
_refreshController.refreshFailed(); |
|
_refreshController.loadFailed(); |
|
} |
|
} |
|
|
|
refreshCoupon() { |
|
pageNum = 1; |
|
queryCoupon(); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
appBar: MyAppBar( |
|
title: S.of(context).lingquanzhongxin, |
|
titleColor: Colors.black, |
|
background: Color(0xFFF7F7F7), |
|
leadingColor: Colors.black, |
|
), |
|
body: Container( |
|
child: Column( |
|
children: [ |
|
ItemTitle( |
|
text: S.of(context).kelingqudeyouhuiquan, |
|
imgPath: "assets/image/icon_card_coupon.webp", |
|
moreText: S.of(context).chakanwodekaquan, |
|
onTap: () { |
|
Navigator.of(context).pushNamed('/router/mine_card'); |
|
}, |
|
), |
|
SizedBox( |
|
height: 23.h, |
|
), |
|
Expanded( |
|
child: SmartRefresher( |
|
enablePullDown: true, |
|
enablePullUp: true, |
|
header: MyHeader(), |
|
footer: CustomFooter( |
|
builder: (BuildContext context, LoadStatus? mode) { |
|
return MyFooter(mode); |
|
}, |
|
), |
|
controller: _refreshController, |
|
onRefresh: refreshCoupon, |
|
onLoading: queryCoupon, |
|
physics: BouncingScrollPhysics(), |
|
child: (coupons.length > 0) |
|
? ListView.builder( |
|
itemBuilder: (context, position) { |
|
return GestureDetector( |
|
child: NewCouponWidget( |
|
coupons[position], |
|
(type) { |
|
if (type == 1) { |
|
receiveCoupon(coupons[position].id); |
|
} else { |
|
if (coupons[position].bizType == 5 || coupons[position].bizType == 3) { |
|
Navigator.of(context).pushNamed( |
|
'/router/write_off_page', |
|
arguments: { |
|
"couponId": coupons[position].id, |
|
"coupon": coupons[position] |
|
}); |
|
} else { |
|
showStoreSelector(coupons[position].storeList); |
|
} |
|
} |
|
}, |
|
() { |
|
setState((){ |
|
coupons[position].isEx = !coupons[position].isEx; |
|
}); |
|
}, |
|
(){ |
|
showCouponDetails(position); |
|
}, |
|
type: 1, |
|
), |
|
); |
|
}, |
|
itemCount: (coupons.length > 0) ? coupons.length : 0, |
|
) |
|
: NoDataView( |
|
src: "assets/image/ka.webp", |
|
isShowBtn: false, |
|
text: S.of(context).haimeiyouyouhuiquankeyilingqu, |
|
fontSize: 16.sp, |
|
), |
|
), |
|
), |
|
], |
|
), |
|
), |
|
); |
|
} |
|
|
|
receiveCoupon(couponId) async { |
|
BaseData? baseData = await apiService?.receiveCoupon(couponId); |
|
if (baseData?.isSuccess ?? false) { |
|
refreshCoupon(); |
|
showAlertDialog(); |
|
} |
|
} |
|
|
|
showStoreSelector(storeList) { |
|
showModalBottomSheet( |
|
context: context, |
|
backgroundColor: Colors.transparent, |
|
builder: (context) { |
|
return SelectorStoreWidget(storeList); |
|
}, |
|
); |
|
} |
|
|
|
showAlertDialog() { |
|
//显示对话框 |
|
showDialog( |
|
context: context, |
|
builder: (BuildContext context) { |
|
return ReceiveSuccess(); |
|
}, |
|
); |
|
} |
|
|
|
///使用详情 |
|
showCouponDetails(index) { |
|
showModalBottomSheet( |
|
context: context, |
|
backgroundColor: Colors.transparent, |
|
builder: (context) { |
|
return CouponDetailsWidget(coupons[index]); |
|
}); |
|
} |
|
}
|
|
|