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.

573 lines
18 KiB

import 'dart:convert';
4 years ago
import 'dart:io';
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';
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';
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';
import 'package:huixiang/view_widget/login_tips.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;
RefreshController _refreshController =
RefreshController(initialRefresh: false);
List<String> sortString = [
S.current.morenpaixu,
// S.current.duihuanlianggaodaodi,
// S.current.duihuanliangdidaogao,
S.current.jifengaodaodi,
S.current.jifendidaogao,
];
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
SharedPreferences.getInstance().then((value) => {
client = ApiService(Dio(), context: context, token: value.getString('token')),
creditGoods(),
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;
List<Goods> gooods = [];
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(() {});
}
}
4 years ago
creditGoods() async {
4 years ago
var param = {
"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);
4 years ago
if (baseData != null && baseData.isSuccess) {
page = PageInfo.fromJson(baseData.data);
if (pageNum == 1) {
gooods.clear();
}
gooods.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
}
_refresh() {
pageNum = 1;
creditGoods();
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) {
sortItems.add(DropdownMenuItem(value: element, child: Text(element)));
});
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,
onLoading: creditGoods,
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(
length: 4,
child:Stack(
children: [
Padding(padding: EdgeInsets.only(top: 39),child:Divider(
// indent: 0.0,
thickness:1.5,
color: Color(0xffE6E6E6),
)),
TabBar(
isScrollable: true, //可滚动
indicatorColor: Color(0xff39B54A),
labelColor: Color(0xff32A060),
labelStyle: TextStyle(fontSize: 14),
unselectedLabelStyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
),
// controller: tabController,
//未选中文字颜色
unselectedLabelColor: Color(0xff262626),
indicatorSize: TabBarIndicatorSize.label,
//指示器与文字等宽
tabs: <Widget>[
Tab(text: "全部"),
Tab(text: "饮食"),
Tab(text: "生活娱乐"),
Tab(text: "办公"),
],
),
],
),
),
),
GridView.builder(
itemCount: gooods.length,
4 years ago
padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 16.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,
//子Widget宽高比例
4 years ago
//0.59
childAspectRatio: 166.w / 281.h,
),
itemBuilder: (contetx, index) {
return GestureDetector(
onTap: () {
_toDetails(index);
},
child: buildItem(gooods[index]),
);
})
],
),
4 years ago
),
),
);
}
_toDetails(index) async {
await Navigator.of(context).pushNamed('/router/integral_store_page',
arguments: {"goodsId": gooods[index].id});
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString("token");
if (token != null)
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;
}
creditGoods();
setState(() {
_itemText = item;
});
}
Widget buildItem(Goods goods) {
4 years ago
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(4),
),
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(
children: [
4 years ago
MImage(
goods.mainImgPath,
aspectRatio: 1,
radius: BorderRadius.only(
topLeft: Radius.circular(2),
topRight: Radius.circular(2),
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
),
Container(
margin:
4 years ago
EdgeInsets.only(left: 12.w, right: 12.w, bottom: 10.h, top: 10.h),
4 years ago
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
4 years ago
Text(
goods.name,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 16.sp,
4 years ago
),
4 years ago
),
SizedBox(
4 years ago
height: 8.h,
4 years ago
),
Container(
4 years ago
height: 34.h,
4 years ago
child: Text(
goods.description,
4 years ago
maxLines: 2,
style: TextStyle(
color: Color(0xFF727272),
4 years ago
fontSize: 12.sp,
4 years ago
),
),
),
SizedBox(
4 years ago
height: 4.h,
4 years ago
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
// 兑换数量
// Text(
// S.of(context).yiduihuanjian("${goods.sales}万"),
// style: TextStyle(
// color: Color(0xFFA29E9E),
// fontSize: 10.sp,
// ),
// ),
4 years ago
Text(
4 years ago
S.of(context).yuan_(goods.worth),
4 years ago
style: TextStyle(
color: Color(0xFF585858),
decoration: TextDecoration.lineThrough,
decorationColor: Color(0xFF585858),
fontSize: 12.sp,
4 years ago
),
),
Text(
S.of(context).jifen_(goods.price),
style: TextStyle(
color: Color(0xFF32A060),
fontSize: 14.sp,
4 years ago
fontWeight: FontWeight.bold,
),
),
4 years ago
],
),
],
),
),
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,
),
4 years ago
),
),
4 years ago
],
),
);
}
Widget userItem() {
return InkWell(
onTap: () {
SharedPreferences.getInstance().then((value) {
if (value.getString("token") == null) {
SmartDialog.show(
widget: LoginTips(
click: () {
value.clear();
Navigator.of(context).pushNamed('/router/login_page', arguments: {"login":"login"});
},
),
clickBgDismissTemp: false,
);
}
});
},
child:Container(
margin: EdgeInsets.all(16),
child: Row(
children: [
MImage(
userinfo != null ? userinfo.headimg : "",
width: 50,
height: 50,
radius: BorderRadius.all(Radius.circular(2)),
fit: BoxFit.cover,
errorSrc: "assets/image/default_1.png",
fadeSrc: "assets/image/default_1.png",
),
Expanded(
child: Container(
margin: EdgeInsets.only(left: 15),
height: 50.h,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
4 years ago
children: [
Text(
userinfo != null ? userinfo.nickname : S.of(context).qudenglu,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Color(0xFF353535)),
),
Row(
children: [
Image.asset(
"assets/image/icon_an_crown.png",
width: 14,
height: 14,
),
SizedBox(
width: 5,
),
Text(
(userinfo != null && userinfo.memberRankVo != null)
? userinfo.memberRankVo.rankName
: "",
style:
4 years ago
TextStyle(fontSize: 12, color: Color(0xFF353535)),
),
],
4 years ago
),
],
),
),
flex: 1,
),
Container(
margin: EdgeInsets.only(left: 15),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
S.of(context).yiyoujifen,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Color(0xFF4C4C4C)),
),
Text(
(userinfo != null) ? "${userinfo.points}" : "",
style: TextStyle(
fontSize: 16,
color: Color(0xFFF8BA61),
fontWeight: FontWeight.bold),
),
],
4 years ago
),
)
],
),
),
4 years ago
);
}
banner() {
return Container(
margin: EdgeInsets.only(top: 16, bottom: 16),
child: AspectRatio(
aspectRatio: 2.0,
child: Swiper(
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,
4 years ago
loop: false,
4 years ago
itemBuilder: (context, position) {
4 years ago
return InkWell(
onTap: () {
bannerClick(bannerData[position]);
},
child: Container(
margin: EdgeInsets.only(bottom: 40),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.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
},
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) {
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
}