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.

659 lines
21 KiB

import 'dart:convert';
import 'dart:ui';
4 years ago
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
4 years ago
import 'package:flutter_swiper/flutter_swiper.dart';
4 years ago
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/main.dart';
import 'package:huixiang/retrofit/data/banner.dart';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/goods.dart';
4 years ago
import 'package:huixiang/retrofit/data/goods_category.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/data/user_info.dart';
4 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/event_type.dart';
import 'package:huixiang/utils/flutter_utils.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.dart';
4 years ago
import 'package:huixiang/view_widget/custom_image.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:pull_to_refresh/pull_to_refresh.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
class PointsMallPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _PointsMallPage();
}
}
4 years ago
class _PointsMallPage extends State<PointsMallPage>
with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
4 years ago
var _itemText = S.current.morenpaixu;
ApiService client;
4 years ago
RefreshController _refreshController = RefreshController(
initialRefresh: false, initialLoadStatus: LoadStatus.canLoading);
List<String> sortString = [
S.current.morenpaixu,
// S.current.duihuanlianggaodaodi,
// S.current.duihuanliangdidaogao,
S.current.jifengaodaodi,
S.current.jifendidaogao,
];
4 years ago
4 years ago
@override
void dispose() {
super.dispose();
_refreshController.dispose();
}
4 years ago
@override
void initState() {
super.initState();
refreshUserInfo();
eventBus.on<EventType>().listen((event) {
if (event.type < 3) {
setState(() {});
}
if (event.type == 3) {
refreshUserInfo();
}
});
}
refreshUserInfo() {
4 years ago
if (mounted)
4 years ago
SharedPreferences.getInstance().then((value) => {
client = ApiService(Dio(),
context: context,
token: value.getString('token'),
showLoading: false),
SmartDialog.showLoading(msg: S.current.zhengzaijiazai),
creditGoods(categoryId),
queryUser(),
});
4 years ago
}
4 years ago
int pageNum = 1;
4 years ago
4 years ago
//排序类型枚举:1-自然排序,2-销量,3-价格
int orderType = 1;
4 years ago
4 years ago
//是否降序排列
bool orderDesc = true;
PageInfo page;
4 years ago
List<Goods> goods = [];
List<GoodsCategory> gooodsCategorys = [];
UserInfo userinfo;
4 years ago
List<BannerData> bannerData = [];
queryUser() async {
4 years ago
BaseData banner = await client.queryBanner({
"model": {"type": "CREDIT_INDEX"},
});
if (banner != null) {
bannerData.clear();
bannerData.addAll((banner.data["records"] as List<dynamic>)
.map((e) => BannerData.fromJson(e))
.toList());
setState(() {});
4 years ago
}
BaseData baseData = await client.queryInfo();
4 years ago
if (baseData != null && baseData.isSuccess) {
userinfo = UserInfo.fromJson(baseData.data);
SharedPreferences.getInstance().then((value) => {
value.setString('user', jsonEncode(baseData.data)),
});
setState(() {});
}
}
creditGoods(categoryId) async {
4 years ago
BaseData dataCategory = await client.goodsCategory({
"current": 1,
"map": {},
"model": {"pageNum": 1, "pageSize": 20, "searchKey": ""},
"order": "descending",
"size": 20,
4 years ago
"sort": "sortOrder"
4 years ago
}).catchError((onError) {
_refreshController.loadFailed();
_refreshController.refreshFailed();
});
if (dataCategory != null && dataCategory.isSuccess) {
PageInfo pageInfo = PageInfo.fromJson(dataCategory.data);
gooodsCategorys.clear();
gooodsCategorys.add(GoodsCategory(name: S.of(context).quanbu));
4 years ago
gooodsCategorys.addAll(
pageInfo.records.map((e) => GoodsCategory.fromJson(e)).toList());
4 years ago
}
4 years ago
var param = {
4 years ago
"categoryId": categoryId ?? "",
"orderDesc": orderDesc,
4 years ago
"orderType": orderType,
"pageNum": pageNum,
4 years ago
"pageSize": 10,
4 years ago
"state": 1
4 years ago
};
4 years ago
BaseData baseData = await client.creditGoods(param).catchError((onError) {
_refreshController.loadFailed();
_refreshController.refreshFailed();
});
SmartDialog.dismiss();
4 years ago
if (baseData != null && baseData.isSuccess) {
page = PageInfo.fromJson(baseData.data);
if (pageNum == 1) {
4 years ago
goods.clear();
}
4 years ago
goods.addAll(page.list.map((e) => Goods.fromJson(e)));
setState(() {
_refreshController.refreshCompleted();
_refreshController.loadComplete();
if (page.pageNum == page.pages) {
_refreshController.loadNoData();
} else {
pageNum += 1;
}
});
4 years ago
} else {
4 years ago
_refreshController.loadFailed();
_refreshController.refreshFailed();
4 years ago
}
4 years ago
}
4 years ago
String categoryId;
_refresh() {
pageNum = 1;
4 years ago
SmartDialog.showLoading(msg: S.current.zhengzaijiazai);
creditGoods(categoryId);
queryUser();
}
4 years ago
@override
Widget build(BuildContext context) {
4 years ago
super.build(context);
4 years ago
List<DropdownMenuItem<String>> sortItems = [];
sortString.forEach((element) {
3 years ago
sortItems.add(DropdownMenuItem(
value: element,
child: Text(
element,
style: TextStyle(
fontSize: 12.sp,
fontWeight: FontWeight.w400,
color: Color(0xff353535)),
)));
});
return SmartRefresher(
enablePullDown: true,
enablePullUp: true,
4 years ago
header: MyHeader(),
4 years ago
physics: BouncingScrollPhysics(),
footer: CustomFooter(
4 years ago
loadStyle: LoadStyle.ShowWhenLoading,
builder: (BuildContext context, LoadStatus mode) {
4 years ago
return MyFooter(mode);
},
),
controller: _refreshController,
onRefresh: _refresh,
4 years ago
onLoading: () {
4 years ago
SmartDialog.showLoading(msg: S.current.zhengzaijiazai);
4 years ago
creditGoods(categoryId);
},
child: SingleChildScrollView(
child: Container(
color: Color(0xFFFAFAFA),
child: Column(
children: [
banner(),
userItem(),
ItemTitle(
text: S.of(context).jifenshangcheng,
imgPath: "assets/image/icon_points_mall.png",
moreText: _itemText,
moreType: 1,
items: sortItems,
onChanged: _sortChange,
),
Container(
alignment: Alignment.centerLeft,
child: DefaultTabController(
4 years ago
length: gooodsCategorys == null ? 0 : gooodsCategorys.length,
child: TabBar(
isScrollable: true,
//可滚动
indicatorColor: Color(0xff39B54A),
labelColor: Color(0xff32A060),
labelStyle:
4 years ago
TextStyle(fontSize: 14.sp, fontWeight: FontWeight.bold),
unselectedLabelStyle: TextStyle(
4 years ago
fontSize: 14.sp,
fontWeight: FontWeight.w400,
),
// controller: tabController,
//未选中文字颜色
unselectedLabelColor: Color(0xff4D4D4D),
indicatorSize: TabBarIndicatorSize.label,
//指示器与文字等宽
4 years ago
tabs: gooodsCategorys == null
? []
: gooodsCategorys
.map((e) => Tab(text: e.name))
.toList(),
onTap: (index) {
4 years ago
categoryId = gooodsCategorys[index].id;
pageNum = 1;
creditGoods(categoryId);
},
),
),
),
GridView.builder(
4 years ago
itemCount: goods == null ? 0 : goods.length,
padding: EdgeInsets.only(
left: 16.w, right: 16.w, top: 13.h, bottom: 16.h),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//一行的Widget数量
crossAxisCount: 2,
//水平子Widget之间间距
4 years ago
crossAxisSpacing: 11.w,
//垂直子Widget之间间距
4 years ago
mainAxisSpacing: 16.w,
4 years ago
//子Widget宽高比例 0.59
4 years ago
childAspectRatio: 166 /
(281 / 2 + (281 / 2) * AppUtils.textScale(context)),
),
itemBuilder: (contetx, index) {
return GestureDetector(
onTap: () {
_toDetails(index);
},
4 years ago
child: buildItem(goods[index]),
);
})
],
),
4 years ago
),
),
);
}
_toDetails(index) async {
await Navigator.of(context).pushNamed('/router/integral_store_page',
4 years ago
arguments: {"goodsId": goods[index].id});
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString("token");
3 years ago
if (token != null && token != "") queryUser();
}
_sortChange(item) {
var index = sortString.indexOf(item);
switch (index) {
case 0:
orderType = 1;
break;
//兑换量
// case 1:
// orderType = 2;
// orderDesc = true;
// break;
// case 2:
// orderType = 2;
// orderDesc = false;
// break;
case 1:
orderType = 3;
orderDesc = true;
break;
case 2:
orderType = 3;
orderDesc = false;
break;
}
4 years ago
SmartDialog.showLoading(msg: S.current.zhengzaijiazai);
creditGoods(categoryId);
setState(() {
_itemText = item;
});
}
Widget buildItem(Goods goods) {
4 years ago
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
4 years ago
borderRadius: BorderRadius.circular(4),
4 years ago
boxShadow: [
BoxShadow(
4 years ago
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
)
4 years ago
],
color: Colors.white),
child: Stack(
alignment: AlignmentDirectional.topEnd,
fit: StackFit.loose,
children: [
Column(
mainAxisSize: MainAxisSize.max,
4 years ago
children: [
4 years ago
MImage(
goods.mainImgPath,
aspectRatio: 1,
radius: BorderRadius.only(
4 years ago
topLeft: Radius.circular(4),
topRight: Radius.circular(4),
4 years ago
),
4 years ago
fit: BoxFit.cover,
4 years ago
errorSrc: "assets/image/default_1.png",
fadeSrc: "assets/image/default_1.png",
4 years ago
),
Expanded(
child: Container(
4 years ago
margin: EdgeInsets.only(
left: 12.w,
right: 12.w,
top: 10.h,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
goods.name,
overflow: TextOverflow.ellipsis,
4 years ago
style: TextStyle(
color: Color(0xff353535),
4 years ago
fontWeight: FontWeight.w500,
fontSize: 16.sp,
4 years ago
),
),
SizedBox(
4 years ago
height: 5.h,
),
Container(
4 years ago
height: 35.h * AppUtils.textScale(context),
child: Text(
goods.description,
maxLines: 2,
4 years ago
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Color(0xFF727272),
4 years ago
fontWeight: FontWeight.w400,
fontSize: 12.sp,
),
),
),
SizedBox(
height: 4.h,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
4 years ago
Expanded(
child: Text(
S.of(context).yuan_(goods.worth),
style: TextStyle(
color: Color(0xFF585858),
decoration: TextDecoration.lineThrough,
decorationColor: Color(0xFF585858),
4 years ago
fontWeight: FontWeight.w400,
4 years ago
fontSize: 12.sp,
),
4 years ago
),
4 years ago
flex: 1,
),
Text(
S.of(context).jifen_(goods.price),
style: TextStyle(
color: Color(0xFF32A060),
fontSize: 14.sp,
fontWeight: FontWeight.bold,
4 years ago
),
),
],
),
],
),
4 years ago
),
flex: 1,
),
SizedBox(
height: 10.h,
),
4 years ago
],
),
Visibility(
visible: goods.isHot,
child: ClipRRect(
borderRadius: BorderRadius.only(topRight: Radius.circular(4)),
child: Image.asset(
"assets/image/icon_hot_right_top.png",
width: 36.w,
height: 36.h,
3 years ago
fit: BoxFit.cover,
),
4 years ago
),
),
4 years ago
],
),
);
}
Widget userItem() {
return InkWell(
onTap: () {
SharedPreferences.getInstance().then((value) {
4 years ago
if (value.getString("token") == null ||
value.getString("token") == "") {
Navigator.of(context)
.pushNamed('/router/login_page', arguments: {"login": "login"});
}
});
},
child: Container(
margin: EdgeInsets.all(16),
child: Row(
children: [
MImage(
userinfo != null ? userinfo.headimg : "",
4 years ago
width: 50,
height: 50,
isCircle: true,
fit: BoxFit.cover,
4 years ago
errorSrc: "assets/image/default_user.png",
fadeSrc: "assets/image/default_user.png",
),
Expanded(
child: Container(
4 years ago
margin: EdgeInsets.only(left: 15.w),
height: 50.h,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
userinfo == null
? Text(
S.of(context).denglu,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w500,
color: Color(0xFF353535)),
)
: Row(
4 years ago
children: [
Text(
userinfo.nickname,
style: TextStyle(
4 years ago
fontSize: 16.sp,
fontWeight: FontWeight.w500,
color: Color(0xFF353535)),
4 years ago
),
SizedBox(
4 years ago
width: 4.w,
4 years ago
),
Image.asset(
"assets/image/icon_user.png",
4 years ago
width: 18.w,
height: 18.h,
4 years ago
),
],
),
4 years ago
SizedBox(
height: 2.h,
4 years ago
),
userinfo == null
? Text(
S.of(context).weidengluxinxi,
style: TextStyle(
fontSize: 12.sp,
fontWeight: FontWeight.w400,
color: Color(0xFF2F2F2F),
),
4 years ago
)
: Text(
userinfo == null ? "" : "NO.${userinfo.vipNo}",
4 years ago
style: TextStyle(
fontSize: 12.sp,
fontWeight: FontWeight.w400,
color: Color(0xFF2F2F2F),
4 years ago
),
),
],
),
),
flex: 1,
),
userinfo == null
? Icon(
Icons.keyboard_arrow_right,
size: 20,
color: Colors.black,
)
: Container(
4 years ago
margin: EdgeInsets.only(left: 15.w),
height: 50.h,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
4 years ago
Text(
S.of(context).yiyoujifen,
style: TextStyle(
fontSize: 12.sp,
4 years ago
fontWeight: FontWeight.w500,
4 years ago
color: Color(0xFF4C4C4C),
),
),
SizedBox(
4 years ago
height: 4.h,
),
Text(
4 years ago
(userinfo != null) ? "${userinfo.points}" : "",
style: TextStyle(
fontSize: 16.sp,
color: Color(0xFFF8BA61),
fontWeight: FontWeight.bold,
),
),
],
),
4 years ago
)
],
),
),
4 years ago
);
}
banner() {
return Container(
4 years ago
margin: EdgeInsets.only(top: 16.h),
4 years ago
child: AspectRatio(
aspectRatio: 2.0,
child: Swiper(
4 years ago
pagination: SwiperPagination(
alignment: Alignment.bottomCenter,
builder: DotSwiperPaginationBuilder(
size: 8,
activeSize: 8,
space: 5,
activeColor: Colors.black,
color: Colors.black.withAlpha(76),
),
),
viewportFraction: 0.7,
scale: 0.7,
loop: false,
itemBuilder: (context, position) {
return InkWell(
onTap: () {
bannerClick(bannerData[position]);
},
child: Container(
margin: EdgeInsets.only(bottom: 40.h),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
),
child: MImage(
bannerData != null && position < bannerData.length
? bannerData[position].imgUrl
: "",
radius: BorderRadius.circular(8),
fit: BoxFit.cover,
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
4 years ago
),
4 years ago
),
);
},
itemCount: bannerData != null ? bannerData.length : 1,
),
4 years ago
),
);
}
4 years ago
4 years ago
/// contentType 跳转类型(0:不跳转,1:积分商品,2:活动,3:文章)
bannerClick(BannerData bannerData) async {
switch (bannerData.contentType) {
4 years ago
case 1:
Navigator.of(context).pushNamed('/router/integral_store_page',
arguments: {"goodsId": bannerData.content});
break;
case 2:
Navigator.of(context)
.pushNamed('/router/store_detail_page', arguments: {
"activityId": bannerData.content,
});
break;
case 3:
Navigator.of(context)
.pushNamed('/router/store_detail_page', arguments: {
"articleId": bannerData.content,
});
break;
}
}
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}