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.
 
 
 
 
 
 

330 lines
10 KiB

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/home/home_view/coupon.dart';
import 'package:huixiang/home/home_view/featured_acticvity.dart';
import 'package:huixiang/home/home_view/home_banner.dart';
import 'package:huixiang/home/home_view/home_integral_store.dart';
import 'package:huixiang/home/home_view/quick_order.dart';
import 'package:huixiang/home/home_view/sign_view.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/main.dart';
import 'package:huixiang/retrofit/data/article.dart';
import 'package:huixiang/retrofit/data/banner.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/brand.dart';
import 'package:huixiang/retrofit/data/founder.dart';
import 'package:huixiang/retrofit/data/goods.dart';
import 'package:huixiang/retrofit/data/goods_category.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/event_type.dart';
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class HomePage extends StatefulWidget {
final GestureTapCallback callback;
HomePage(this.callback);
@override
State<StatefulWidget> createState() {
return _HomePage(callback);
}
}
class _HomePage extends State<HomePage> with AutomaticKeepAliveClientMixin {
ApiService apiService;
final GestureTapCallback callback;
_HomePage(this.callback);
@override
void initState() {
super.initState();
eventBus.on<EventType>().listen((event) {
if (event.type < 3) {
setState(() {});
}
});
}
final SwiperController controller = SwiperController();
String categoryId;
int pageNum = 1;
//排序类型枚举:1-自然排序,2-销量,3-价格
int orderType = 1;
//是否降序排列
bool orderDesc = true;
List<Goods> goods = [];
List<GoodsCategory> gooodsCategorys = [];
List<BannerData> bannerData = [];
List<Brand> brandData = [];
List<Article> articles = [];
List<Goods> gooods = [];
Founder founder;
queryHome() async {
EasyLoading.show(status: S.of(context).zhengzaijiazai);
final SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService(Dio(),
context: context, token: value.getString('token'), showLoading: false);
final BaseData brand =
await apiService.queryHomeBrand().catchError((onError) {
refreshController.refreshFailed();
});
if (brand != null && brand.isSuccess) {
brandData.clear();
brandData.addAll((brand.data["brandList"] as List<dynamic>)
.map((e) => Brand.fromJson(e))
.toList());
founder = Founder.fromJson(brand.data["founder"]);
}
final BaseData<PageInfo<Article>> article = await apiService.queryArticle({
"pageNum": 1,
"pageSize": 3,
"searchKey": "",
"state": 1,
"type": 2
}).catchError((onError) {
refreshController.refreshFailed();
});
articles.clear();
if (article != null && article.isSuccess) {
articles.addAll(article.data.list);
}
final BaseData<PageInfo<Goods>> goodsData = await apiService.creditGoods({
"orderDesc": true,
"orderType": 1,
"pageNum": 1,
"pageSize": 10,
"state": 1
}).catchError((onError) {
refreshController.refreshFailed();
});
if (goodsData != null && goodsData.isSuccess) {
gooods.clear();
gooods.addAll(goodsData.data.list);
}
BaseData<PageInfo<GoodsCategory>> dataCategory =
await apiService.goodsCategory({
"current": 1,
"map": {},
"model": {"pageNum": 1, "pageSize": 20, "searchKey": ""},
"order": "descending",
"size": 20,
"sort": "sortOrder"
}).catchError((onError) {
refreshController.loadFailed();
refreshController.refreshFailed();
});
if (dataCategory != null &&
dataCategory.isSuccess &&
dataCategory.data != null &&
dataCategory.data.records != null &&
dataCategory.data.records.length > 0) {
gooodsCategorys.clear();
gooodsCategorys.add(GoodsCategory(name: S.of(context).quanbu));
gooodsCategorys.addAll(dataCategory.data.records);
}
var param = {
"categoryId": categoryId ?? "",
"orderDesc": orderDesc,
"orderType": orderType,
"pageNum": pageNum,
"pageSize": 10,
"state": 1
};
BaseData<PageInfo<Goods>> pageGoods =
await apiService.creditGoods(param).catchError((onError) {
refreshController.loadFailed();
refreshController.refreshFailed();
});
EasyLoading.dismiss();
if (pageGoods != null && pageGoods.isSuccess) {
if (pageNum == 1) {
goods.clear();
}
goods.addAll(pageGoods.data.list);
refreshController.refreshCompleted();
refreshController.loadComplete();
if (pageGoods.data.pageNum == pageGoods.data.pages) {
refreshController.loadNoData();
} else {
pageNum += 1;
}
} else {
refreshController.loadFailed();
refreshController.refreshFailed();
}
final BaseData<PageInfo<BannerData>> baseData =
await apiService.queryBanner({
"model": {"type": "HOME_PAGE"},
}).catchError((onError) {
refreshController.refreshFailed();
});
if (baseData != null && baseData.isSuccess) {
bannerData.clear();
bannerData.addAll(baseData.data.records);
refreshController.refreshCompleted();
if (bannerData.length > 0) controller.move(0, animation: false);
} else {
refreshController.refreshFailed();
}
EasyLoading.dismiss();
}
@override
void dispose() {
super.dispose();
if (refreshController != null) refreshController.dispose();
}
final RefreshController refreshController = RefreshController();
@override
Widget build(BuildContext context) {
super.build(context);
return Column(
children: [
MyAppBar(
title: "首页",
leading: false,
actions: [
Container(
margin: EdgeInsets.only(right: 12.w),
child: GestureDetector(
onTap: () {
// Navigator.of(context).pushNamed('/router/system_msg_page');
},
child: SvgPicture.asset(
"assets/svg/tixing.svg",
width: 24,
height: 24,
),
),
),
Container(
margin: EdgeInsets.only(right: 16.w),
child: GestureDetector(
onTap: () {
// Navigator.of(context).pushNamed('/router/system_msg_page');
},
child: SvgPicture.asset(
"assets/svg/saoyisao.svg",
width: 24,
height: 24,
),
),
),
],
),
Expanded(
child: Container(
child: SmartRefresher(
controller: refreshController,
enablePullDown: true,
enablePullUp: false,
header: MyHeader(),
physics: BouncingScrollPhysics(),
onRefresh: () {
setState(() {});
},
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: FutureBuilder(
future: queryHome(),
builder: (context, snapshot) {
return Column(
children: [
///banner
HomeBanner(bannerData, controller),
///签到
SignView(),
// ///热门文章
// HotArticle(articles),
// ///创始人故事
// FounderStore(founder),
// ///品牌介绍
// BrandView(brandData),
///快捷下单
QuickOrder(),
CouponView(),
///积分商城
HomeIntegralStore(gooods, callback),
FeaturedActivity(),
///积分商品头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);
},
),
],
);
},
),
),
),
),
),
SizedBox(
height: 76.h,
),
],
);
}
_toDetails(index) async {
Navigator.of(context).pushNamed(
'/router/integral_store_page',
arguments: {"goodsId": goods[index].id},
);
}
@override
bool get wantKeepAlive => true;
}