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.

217 lines
6.7 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';
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:huixiang/view_widget/my_footer.dart';
4 years ago
import 'package:huixiang/view_widget/new_coupon_widget.dart';
4 years ago
import 'package:huixiang/view_widget/receive_success.dart';
import 'package:huixiang/view_widget/selector_store_dialog.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
class CouponsPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _CouponsPage();
}
}
class _CouponsPage extends State<CouponsPage> {
4 years ago
ApiService apiService;
RefreshController _refreshController;
4 years ago
@override
void initState() {
super.initState();
4 years ago
SharedPreferences.getInstance().then((value) {
apiService =
ApiService(Dio(), context: context, token: value.getString('token'));
queryCard();
});
_refreshController = RefreshController(initialRefresh: false);
}
List<Coupon> coupons = [];
int pageNum = 1;
int state = 1;
_onRefresh() async {
pageNum = 1;
queryCard();
4 years ago
}
4 years ago
queryCard() async {
BaseData baseData = await apiService.queryCard({
"centre": true,
"pageNum": pageNum,
"pageSize": 10,
"searchKey": "",
"state": state
}).catchError((error) {
_refreshController.loadFailed();
_refreshController.refreshFailed();
});
if (baseData != null && baseData.isSuccess) {
PageInfo pageInfo = PageInfo.fromJson(baseData.data);
if (pageNum == 1) {
coupons.clear();
}
coupons.addAll(pageInfo.list.map((e) => Coupon.fromJson(e)));
setState(() {
_refreshController.refreshCompleted();
_refreshController.loadComplete();
if (pageInfo.pageNum == pageInfo.pages) {
_refreshController.loadNoData();
} else {
pageNum += 1;
}
});
} else {
_refreshController.loadFailed();
_refreshController.refreshFailed();
Fluttertoast.showToast(msg: baseData.msg);
}
}
4 years ago
@override
Widget build(BuildContext context) {
return Scaffold(
4 years ago
body: Column(
children: [
Row(
children: [
Container(
width: 68.w,
height: 26.h,
alignment: Alignment.center,
margin: EdgeInsets.only(
left: 16.w, right: 12.w, top: 10.h, bottom: 14.h),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(2)),
color: state == 1 ? Colors.white : Color(0xffE5E5E5),
),
child: GestureDetector(
onTap: () {
setState(() {
state = 1;
_onRefresh();
});
},
child: Text(
S.of(context).keyongquan,
style: TextStyle(
fontSize: 14.sp,
fontWeight: state == 1 ? FontWeight.bold : FontWeight.normal,
color: state == 1 ? Colors.black : Color(0xff727272),
),
),
),
4 years ago
),
4 years ago
Container(
width: 68.w,
height: 26.h,
alignment: Alignment.center,
margin: EdgeInsets.only(left: 16.w, top: 10.h, bottom: 14.h),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(2)),
color: state == 3 ? Colors.white : Color(0xffE5E5E5),
),
child: GestureDetector(
onTap: () {
setState(() {
state = 3;
_onRefresh();
});
},
child: Text(
S.of(context).shixiaoquan,
style: TextStyle(
fontSize: 14.sp,
fontWeight: state == 3 ? FontWeight.bold : FontWeight.normal,
color: state == 3 ? Colors.black : Color(0xff727272),
),
4 years ago
),
4 years ago
),
4 years ago
),
4 years ago
],
),
Expanded(
child: SmartRefresher(
controller: _refreshController,
enablePullDown: true,
enablePullUp: true,
physics: BouncingScrollPhysics(),
header: MyHeader(),
footer: CustomFooter(
builder: (context, mode) {
return MyFooter(mode);
},
4 years ago
),
4 years ago
onRefresh: _onRefresh,
onLoading: queryCard,
child: ListView.builder(
itemBuilder: (context, position) {
return GestureDetector(
4 years ago
child: NewCouponWidget(coupons[position], (type) {
if (type == 1) {
receiveCoupon(coupons[position].id);
} else {
//Navigator.of(context).pushNamed('/router/mine_card')
showStoreSelector(coupons[position].storeList);
}
}, () {
4 years ago
setState(() {
coupons[position].isEx = !coupons[position].isEx;
});
4 years ago
}, type: 0,),
4 years ago
);
4 years ago
},
4 years ago
itemCount: coupons != null ? coupons.length : 0,
4 years ago
),
4 years ago
),
4 years ago
),
],
),
4 years ago
);
}
4 years ago
receiveCoupon(couponId) async {
BaseData baseData = await apiService.receiveCoupon(couponId);
if (baseData.isSuccess) {
_onRefresh();
showAlertDialog();
} else {
Fluttertoast.showToast(msg: baseData.msg);
}
}
showStoreSelector(storeList) {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) {
return SelectorStoreWidget(storeList);
});
}
showAlertDialog() {
//显示对话框
showDialog(
context: context,
builder: (BuildContext context) {
return ReceiveSuccess();
},
);
}
4 years ago
}