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.

247 lines
7.3 KiB

import 'dart:convert';
import 'dart:ui';
4 years ago
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/material.dart';
3 years ago
import 'package:flutter_easyloading/flutter_easyloading.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';
3 years ago
import 'package:huixiang/home/points_mall_view/point_mall_user.dart';
import 'package:huixiang/home/points_mall_view/points_goods_title.dart';
import 'package:huixiang/home/points_mall_view/points_goods_view.dart';
import 'package:huixiang/home/points_mall_view/points_mall_banner.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';
3 years ago
import 'package:huixiang/utils/font_weight.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
ApiService client;
3 years ago
final RefreshController _refreshController = RefreshController();
4 years ago
4 years ago
@override
void dispose() {
super.dispose();
_refreshController.dispose();
}
4 years ago
@override
void initState() {
super.initState();
eventBus.on<EventType>().listen((event) {
if (event.type < 3) {
setState(() {});
}
if (event.type == 3) {
3 years ago
creditGoods();
}
});
}
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;
4 years ago
List<Goods> goods = [];
List<GoodsCategory> gooodsCategorys = [];
3 years ago
UserInfo userInfo;
4 years ago
List<BannerData> bannerData = [];
queryUser() async {
3 years ago
BaseData<PageInfo<BannerData>> banner = await client.queryBanner({
4 years ago
"model": {"type": "CREDIT_INDEX"},
});
3 years ago
if (banner != null &&
banner.isSuccess &&
banner.data != null &&
banner.data.records != null &&
banner.data.records.length > 0) {
4 years ago
bannerData.clear();
3 years ago
bannerData.addAll(banner.data.records);
4 years ago
}
3 years ago
BaseData<UserInfo> baseData = await client.queryInfo();
4 years ago
if (baseData != null && baseData.isSuccess) {
3 years ago
userInfo = baseData.data;
SharedPreferences.getInstance().then((value) => {
value.setString('user', jsonEncode(baseData.data)),
});
}
}
3 years ago
creditGoods() async {
EasyLoading.show(status: S.current.zhengzaijiazai);
final SharedPreferences value = await SharedPreferences.getInstance();
client = ApiService(Dio(),
context: context, token: value.getString('token'), showLoading: false);
await queryUser();
3 years ago
BaseData<PageInfo<GoodsCategory>> dataCategory =
await client.goodsCategory({
4 years ago
"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();
});
3 years ago
if (dataCategory != null &&
dataCategory.isSuccess &&
dataCategory.data != null &&
dataCategory.data.records != null &&
dataCategory.data.records.length > 0) {
4 years ago
gooodsCategorys.clear();
gooodsCategorys.add(GoodsCategory(name: S.of(context).quanbu));
3 years ago
gooodsCategorys.addAll(dataCategory.data.records);
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
};
3 years ago
BaseData<PageInfo<Goods>> baseData = await client.creditGoods(param)
.catchError((onError) {
4 years ago
_refreshController.loadFailed();
_refreshController.refreshFailed();
});
3 years ago
EasyLoading.dismiss();
4 years ago
if (baseData != null && baseData.isSuccess) {
if (pageNum == 1) {
4 years ago
goods.clear();
}
3 years ago
goods.addAll(baseData.data.list);
3 years ago
_refreshController.refreshCompleted();
_refreshController.loadComplete();
if (baseData.data.pageNum == baseData.data.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;
4 years ago
@override
Widget build(BuildContext context) {
4 years ago
super.build(context);
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,
3 years ago
onRefresh: () {
pageNum = 1;
setState(() {});
},
4 years ago
onLoading: () {
3 years ago
setState(() {});
4 years ago
},
child: SingleChildScrollView(
child: Container(
3 years ago
color: Color(0xFFFAFAFA),
child: FutureBuilder(
future: creditGoods(),
builder: (context, snapshot) {
return Column(
children: [
///积分商城的banner
PointMallBanner(bannerData),
///积分商城的用户信息
PointMallUser(userInfo),
///积分商品头Tab
PointsGoodsTitle(
gooodsCategorys,
(orderType, orderDesc) {
this.orderType = orderType;
this.orderDesc = orderDesc;
setState(() {});
},
(index) {
categoryId = gooodsCategorys[index].id;
pageNum = 1;
setState(() {});
},
),
///积分商品列表
PointGoods(
goods,
(index) {
_toDetails(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();
}
4 years ago
@override
bool get wantKeepAlive => true;
3 years ago
4 years ago
}