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.8 KiB

4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
4 years ago
import 'package:huixiang/retrofit/data/coupon.dart';
4 years ago
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.dart';
3 years ago
import 'package:huixiang/view_widget/coupon_details_dialog.dart';
4 years ago
import 'package:huixiang/view_widget/item_title.dart';
4 years ago
import 'package:huixiang/view_widget/my_appbar.dart';
4 years ago
import 'package:huixiang/view_widget/my_footer.dart';
import 'package:huixiang/view_widget/new_coupon_widget.dart';
3 years ago
import 'package:huixiang/view_widget/no_data_view.dart';
4 years ago
import 'package:huixiang/view_widget/receive_success.dart';
4 years ago
import 'package:huixiang/view_widget/selector_store_dialog.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
4 years ago
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
class RollCenterPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _RollCenterPage();
}
}
4 years ago
class _RollCenterPage extends State<RollCenterPage> {
4 years ago
RefreshController _refreshController;
4 years ago
ApiService apiService;
4 years ago
@override
void initState() {
super.initState();
4 years ago
4 years ago
SharedPreferences.getInstance().then((value) => {
3 years ago
apiService = ApiService(
Dio(),
context: context,
token: value.getString('token'),
),
4 years ago
queryCoupon(),
});
_refreshController = RefreshController(initialRefresh: false);
4 years ago
}
4 years ago
int pageNum = 1;
4 years ago
List<Coupon> coupons = [];
4 years ago
4 years ago
queryCoupon() async {
3 years ago
BaseData<PageInfo<Coupon>> baseData = await apiService.queryCoupon({
4 years ago
"centre": true,
"pageNum": pageNum,
"pageSize": 10,
"searchKey": "",
4 years ago
"state": 0
}).catchError((onError) {
_refreshController.refreshFailed();
_refreshController.loadFailed();
4 years ago
});
4 years ago
if (pageNum == 1) coupons.clear();
if (baseData != null && baseData.isSuccess) {
3 years ago
coupons.addAll(baseData.data.list);
4 years ago
_refreshController.refreshCompleted();
_refreshController.loadComplete();
3 years ago
if (baseData.data.pageNum == baseData.data.pages) {
4 years ago
_refreshController.loadNoData();
} else {
pageNum += 1;
}
4 years ago
setState(() {});
} else {
4 years ago
_refreshController.refreshFailed();
_refreshController.loadFailed();
4 years ago
}
4 years ago
}
4 years ago
4 years ago
refreshCoupon() {
pageNum = 1;
queryCoupon();
4 years ago
}
4 years ago
@override
Widget build(BuildContext context) {
4 years ago
return Scaffold(
4 years ago
appBar: MyAppBar(
title: S.of(context).lingquanzhongxin,
titleColor: Colors.black,
background: Color(0xFFF7F7F7),
leadingColor: Colors.black,
4 years ago
),
body: Container(
child: Column(
children: [
ItemTitle(
4 years ago
text: S.of(context).kelingqudeyouhuiquan,
imgPath: "assets/image/icon_card_coupon.webp",
4 years ago
moreText: S.of(context).chakanwodekaquan,
4 years ago
onTap: () {
4 years ago
Navigator.of(context).pushNamed('/router/mine_card');
4 years ago
},
4 years ago
),
4 years ago
SizedBox(
height: 23.h,
),
4 years ago
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(),
3 years ago
child: (coupons != null && 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) {
3 years ago
Navigator.of(context).pushNamed(
'/router/write_off_page',
arguments: {
"couponId": coupons[position].id,
"coupon": coupons[position].toJson()
});
} else {
showStoreSelector(coupons[position].storeList);
}
}
},
() {
setState((){
coupons[position].isEx = !coupons[position].isEx;
});
},
3 years ago
(){
showCouponDetails(position);
},
3 years ago
type: 1,
),
);
4 years ago
},
3 years ago
itemCount: (coupons != null && coupons.length > 0) ? coupons.length : 0,
)
: NoDataView(
src: "assets/image/ka.webp",
3 years ago
isShowBtn: false,
text: S.of(context).haimeiyouyouhuiquankeyilingqu,
fontSize: 16.sp,
4 years ago
),
4 years ago
),
3 years ago
),
4 years ago
],
),
4 years ago
),
);
}
4 years ago
receiveCoupon(couponId) async {
BaseData baseData = await apiService.receiveCoupon(couponId);
if (baseData != null && baseData.isSuccess) {
4 years ago
refreshCoupon();
showAlertDialog();
}
}
4 years ago
showStoreSelector(storeList) {
showModalBottomSheet(
3 years ago
context: context,
backgroundColor: Colors.transparent,
builder: (context) {
return SelectorStoreWidget(storeList);
},
);
4 years ago
}
4 years ago
showAlertDialog() {
//显示对话框
showDialog(
context: context,
builder: (BuildContext context) {
4 years ago
return ReceiveSuccess();
4 years ago
},
);
}
3 years ago
///使用详情
showCouponDetails(index) {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) {
return CouponDetailsWidget(coupons[index]);
});
}
4 years ago
}