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.

292 lines
9.5 KiB

4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
4 years ago
import 'package:fluttertoast/fluttertoast.dart';
4 years ago
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';
4 years ago
import 'package:huixiang/view_widget/item_title.dart';
4 years ago
import 'package:huixiang/view_widget/my_footer.dart';
import 'package:huixiang/view_widget/new_coupon_widget.dart';
4 years ago
import 'package:huixiang/view_widget/round_button.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) => {
4 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 {
BaseData baseData = await apiService.queryCoupon({
"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) {
4 years ago
PageInfo pageInfo = PageInfo.fromJson(baseData.data);
coupons.addAll(pageInfo.list.map((e) => Coupon.fromJson(e)).toList());
4 years ago
_refreshController.refreshCompleted();
_refreshController.loadComplete();
if (pageInfo.pageNum == pageInfo.pages) {
_refreshController.loadNoData();
} else {
pageNum += 1;
}
4 years ago
setState(() {});
} else {
Fluttertoast.showToast(msg: baseData.msg);
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(
appBar: AppBar(
title: Text(
4 years ago
S.of(context).lingquanzhongxin,
4 years ago
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
centerTitle: false,
backgroundColor: Color(0xFFF7F7F7),
elevation: 0,
leading: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Container(
alignment: Alignment.centerRight,
4 years ago
margin: EdgeInsets.only(left: 10.w),
4 years ago
padding: EdgeInsets.all(6),
child: Icon(
Icons.arrow_back_ios,
color: Colors.black,
size: 24,
4 years ago
),
4 years ago
),
),
titleSpacing: 2,
4 years ago
leadingWidth: 56.w,
4 years ago
),
body: Container(
child: Column(
children: [
ItemTitle(
4 years ago
text: S.of(context).kelingqudeyouhuiquan,
4 years ago
imgPath: "assets/image/icon_card_coupon.png",
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
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: ListView.builder(
itemBuilder: (context, position) {
return GestureDetector(
child: NewCouponWidget(coupons[position], (type) {
if (type == 1) {
receiveCoupon(coupons[position].id);
} else {
4 years ago
//Navigator.of(context).pushNamed('/router/mine_card')
showStoreSelector(coupons[position].storeList);
4 years ago
}
}, () {
setState(() {
coupons[position].isEx = !coupons[position].isEx;
});
}),
);
},
itemCount: coupons != null ? coupons.length : 0,
),
),
)
4 years ago
],
),
4 years ago
),
);
}
4 years ago
receiveCoupon(couponId) async {
BaseData baseData = await apiService.receiveCoupon(couponId);
if (baseData.isSuccess) {
refreshCoupon();
showAlertDialog();
} else {
Fluttertoast.showToast(msg: baseData.msg);
}
}
4 years ago
showStoreSelector(storeList) {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) {
return SelectorStoreWidget(storeList);
});
}
4 years ago
showAlertDialog() {
//显示对话框
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
titlePadding: EdgeInsets.all(10),
backgroundColor: Colors.transparent,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6)),
),
children: <Widget>[
Stack(
alignment: Alignment.topCenter,
children: [
Container(
margin: EdgeInsets.only(top: 40.h),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8))),
width: 218.w,
height: 210.h,
child: Column(
children: [
Padding(
padding: EdgeInsets.only(top: 50.h, bottom: 19.h),
child: Text(
"领取成功",
style: TextStyle(
color: Color(0xff353535),
fontSize: 16.sp,
fontWeight: FontWeight.bold,
),
),
),
Text(
"您已成功领取优惠劵,请在",
style: TextStyle(
color: Color(0xff353535),
fontSize: 14.sp,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
child: Text(
S.of(context).kaquan,
style: TextStyle(
color: Color(0xffFF7A1A),
fontSize: 14.sp,
fontWeight: FontWeight.bold,
),
),
onTap: () {
Navigator.of(context)
.pushNamed('/router/mine_card');
},
),
Text(
"中查看",
style: TextStyle(
color: Color(0xff353535),
fontSize: 14.sp,
),
),
],
),
Padding(
padding: EdgeInsets.only(top: 30.h),
child: RoundButton(
width: 130.w,
height: 34.h,
text: S.of(context).queren,
textColor: Colors.white,
fontSize: 12.sp,
callback: () {
Navigator.of(context).pop();
},
padding: EdgeInsets.only(
top: 2.h,
bottom: 2.h,
left: 8.w,
right: 8.w,
),
backgroup: Color(0xff32A060),
),
)
],
),
),
Image.asset(
"assets/image/icon_order_success.png",
fit: BoxFit.cover,
width: 80.w,
height: 80.h,
),
],
)
],
);
},
);
}
4 years ago
}