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.

503 lines
16 KiB

3 years ago
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';
3 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/classic_header.dart';
3 years ago
import 'package:huixiang/view_widget/coupon_details_dialog.dart';
3 years ago
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';
import 'package:shimmer/shimmer.dart';
3 years ago
import '../utils/flutter_utils.dart';
3 years ago
class CouponPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _CouponPage();
}
}
class _CouponPage extends State<CouponPage> {
ApiService? apiService;
late RefreshController _refreshController;
int networkStatus = 0;
String networkError = "";
3 years ago
@override
void dispose() {
super.dispose();
_refreshController.dispose();
}
@override
void initState() {
super.initState();
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();
}
queryCard() async {
BaseData<PageInfo<Coupon>>? baseData = await apiService?.queryCard({
3 years ago
"centre": true,
"pageNum": pageNum,
"pageSize": 10,
"searchKey": "",
"state": state
}).catchError((error) {
networkError = AppUtils.dioErrorTypeToString(error.type);
networkStatus = -1;
setState(() {});
3 years ago
_refreshController.loadFailed();
_refreshController.refreshFailed();
});
if (baseData?.isSuccess ?? false) {
if (baseData?.data?.list?.isNotEmpty ?? false) {
if (pageNum == 1) {
coupons.clear();
}
coupons.addAll(baseData!.data!.list!);
3 years ago
}
setState(() {
_refreshController.refreshCompleted();
_refreshController.loadComplete();
if (baseData?.data?.pageNum == baseData?.data?.pages) {
3 years ago
_refreshController.loadNoData();
} else {
pageNum += 1;
}
});
networkStatus = 1;
3 years ago
} else {
_refreshController.loadFailed();
_refreshController.refreshFailed();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: MyAppBar(
title: S.of(context).youhuiquan,
3 years ago
titleColor: Colors.black,
background: Color(0xFFFFFFFF),
leadingColor: Colors.black,
),
body: Column(
children: [
Container(
height: 54.h,
padding: EdgeInsets.only(left: 14.w, right: 14.w, bottom: 12.h),
margin: EdgeInsets.only(bottom: 10),
color: Color(0xFFFFFFFF),
child: Row(
children: [
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
state = 1;
_onRefresh();
});
},
child: Container(
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topLeft: Radius.circular(6),
bottomLeft: Radius.circular(6),
),
border: Border.all(
color: Color(0xFF32A060),
width: 1,
3 years ago
),
color: state == 1 ? Color(0xFF32A060) : Color(0xFFFFFFFF),
),
child: Text(
S.of(context).keyongquan,
style: TextStyle(
fontWeight: MyFontWeight.medium,
fontSize: 15.sp,
color:
state == 1 ? Color(0xFFFFFFFF) : Color(0xFF32A060),
),
),
),
)),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
state = 2;
_onRefresh();
});
},
child: Container(
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border(
top: BorderSide(width: 1, color: Color(0xFF32A060)),
bottom: BorderSide(width: 1, color: Color(0xFF32A060)),
),
color: state == 2 ? Color(0xFF32A060) : Color(0xFFFFFFFF),
),
child: Text(
S.of(context).yishiyong,
style: TextStyle(
fontWeight: MyFontWeight.medium,
fontSize: 15.sp,
color:
state == 2 ? Color(0xFFFFFFFF) : Color(0xFF32A060),
),
),
),
)),
3 years ago
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
state = 3;
_onRefresh();
});
},
child: Container(
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: new BorderRadius.only(
topRight: Radius.circular(6),
bottomRight: Radius.circular(6),
),
border: Border.all(
color: Color(0xFF32A060),
width: 1,
3 years ago
),
color: state == 3 ? Color(0xFF32A060) : Color(0xFFFFFFFF),
),
child: Text(
S.of(context).shixiaoquan,
style: TextStyle(
fontWeight: MyFontWeight.medium,
fontSize: 15.sp,
color:
state == 3 ? Color(0xFFFFFFFF) : Color(0xFF32A060),
),
),
),
))
3 years ago
],
),
),
Expanded(
child: SmartRefresher(
controller: _refreshController,
enablePullDown: true,
enablePullUp: true,
physics: BouncingScrollPhysics(),
header: MyHeader(),
footer: CustomFooter(
builder: (context, mode) {
return MyFooter(mode);
},
),
onRefresh: _onRefresh,
onLoading: queryCard,
child: networkStatus == -1?noNetwork():(networkStatus == 0
? ListView.builder(
itemCount: 10,
physics: BouncingScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, position) {
return GestureDetector(
onTap: () {},
child: couponItemSm(),
);
},
)
: ((coupons.length > 0)
3 years ago
? ListView.builder(
3 years ago
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],
3 years ago
}).then((value) {
_onRefresh();
});
} else {
showStoreSelector(
coupons[position].storeList);
}
}
3 years ago
},
3 years ago
() {
setState(() {
coupons[position].isEx =
!coupons[position].isEx;
});
},
() {
3 years ago
setState(() {
showCouponDetails(position);
});
},
type: 0,
3 years ago
),
3 years ago
);
},
itemCount:coupons.length??0,
3 years ago
)
: NoDataView(
src: "assets/image/ka.webp",
isShowBtn: false,
text: state == 1
? "目前暂无优惠券,请到领劵中心领取哦~"
: (state == 2 ? "目前暂无已使用的优惠券哦~" : "目前暂无失效的优惠券哦~"),
3 years ago
fontSize: 16.sp,
margin:
EdgeInsets.only(top: 120.h, left: 60, right: 60),
))),
3 years ago
),
),
],
),
);
}
receiveCoupon(couponId) async {
BaseData? baseData =
await apiService?.receiveCoupon(couponId).catchError((onError) {});
if (baseData?.isSuccess ?? false) {
3 years ago
_onRefresh();
showAlertDialog();
}
}
///选择店铺使用
showStoreSelector(storeList) {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) {
return SelectorStoreWidget(storeList);
},
);
3 years ago
}
showAlertDialog() {
//显示对话框
showDialog(
context: context,
builder: (BuildContext context) {
return ReceiveSuccess();
},
);
}
3 years ago
///使用详情
showCouponDetails(index) {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) {
return CouponDetailsWidget(coupons[index]);
},
);
3 years ago
}
3 years ago
Widget couponItemSm() {
return Container(
height: 143.h,
width: double.infinity,
margin: EdgeInsets.fromLTRB(14.w, 6.h, 14.w, 6.h),
padding: EdgeInsets.only(
right: 13.w,
top: 12.h,
bottom: 13.h,
left: 18.w,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(6),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
)
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Shimmer.fromColors(
baseColor: Color(0XFFD8D8D8),
highlightColor: Color(0XFFD8D8D8),
child: Container(
color: Color(0XFFD8D8D8),
width: 143.w,
height: 21.h,
),
),
SizedBox(
height: 6.h,
),
Shimmer.fromColors(
baseColor: Color(0XFFD8D8D8),
highlightColor: Color(0XFFD8D8D8),
child: Container(
color: Color(0XFFD8D8D8),
width: 150.w,
height: 17.h,
),
),
Spacer(),
Shimmer.fromColors(
baseColor: Color(0XFFD8D8D8),
highlightColor: Color(0XFFD8D8D8),
child: Container(
color: Color(0XFFD8D8D8),
width: 48.w,
height: 17.h,
),
),
],
),
),
Column(
children: [
Shimmer.fromColors(
baseColor: Color(0XFFD8D8D8),
highlightColor: Color(0XFFD8D8D8),
child: Container(
color: Color(0XFFD8D8D8),
width: 50.w,
height: 50.h,
),
),
SizedBox(
height: 10.h,
),
Shimmer.fromColors(
baseColor: Color(0XFFD8D8D8),
highlightColor: Color(0XFFD8D8D8),
child: Container(
color: Color(0XFFD8D8D8),
width: 59.w,
height: 17.h,
),
),
SizedBox(
height: 15.h,
),
Shimmer.fromColors(
baseColor: Color(0XFFD8D8D8),
highlightColor: Color(0XFFD8D8D8),
child: Container(
decoration: BoxDecoration(
color: Color(0XFFD8D8D8),
borderRadius: BorderRadius.circular(10),
),
width: 64.w,
height: 19.h,
),
)
],
)
],
),
);
}
Widget noNetwork() {
return Container(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
// "无法连接到网络",
networkError.substring(0, networkError.indexOf(",")),
style: TextStyle(
fontSize: 14.sp,
color: Color(0xFF0D0D0D),
fontWeight: MyFontWeight.bold),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.h),
child: Text(
"请检查网络设置或稍后重试",
style: TextStyle(
fontSize: 12.sp,
color: Color(0xFF7A797F),
fontWeight: MyFontWeight.regular),
),
),
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
_onRefresh();
},
child: Container(
decoration: BoxDecoration(
color: Color(0xff32A060),
borderRadius: BorderRadius.circular(15),
),
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 3.h),
child: Text(
"重试",
style: TextStyle(
fontSize: 14.sp,
color: Colors.white,
fontWeight: MyFontWeight.regular),
)),
)
],
),
);
}
3 years ago
}