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.

187 lines
5.7 KiB

4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
3 years ago
import 'package:flutter_easyloading/flutter_easyloading.dart';
4 years ago
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/home_view/home_banner.dart';
import 'package:huixiang/home/home_view/home_integral_store.dart';
import 'package:huixiang/home/home_view/sign_view.dart';
4 years ago
import 'package:huixiang/main.dart';
4 years ago
import 'package:huixiang/retrofit/data/article.dart';
import 'package:huixiang/retrofit/data/banner.dart';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
4 years ago
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/page.dart';
4 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
4 years ago
import 'package:huixiang/utils/event_type.dart';
4 years ago
import 'package:huixiang/utils/flutter_utils.dart';
3 years ago
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/border_text.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:huixiang/view_widget/custom_image.dart';
4 years ago
import 'package:huixiang/view_widget/hot_item.dart';
import 'package:huixiang/view_widget/item_title.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
import 'package:huixiang/view_widget/login_tips.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
4 years ago
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
3 years ago
import 'home_view/brand_view.dart';
import 'home_view/founder_store.dart';
import 'home_view/hot_article.dart';
4 years ago
class HomePage extends StatefulWidget {
final GestureTapCallback callback;
4 years ago
HomePage(this.callback);
4 years ago
4 years ago
@override
State<StatefulWidget> createState() {
return _HomePage(callback);
4 years ago
}
}
4 years ago
class _HomePage extends State<HomePage> with AutomaticKeepAliveClientMixin {
4 years ago
ApiService apiService;
final GestureTapCallback callback;
4 years ago
_HomePage(this.callback);
4 years ago
@override
void initState() {
super.initState();
4 years ago
eventBus.on<EventType>().listen((event) {
4 years ago
if (event.type < 3) {
setState(() {});
4 years ago
}
4 years ago
});
4 years ago
}
4 years ago
SwiperController controller = SwiperController();
4 years ago
List<BannerData> bannerData = [];
List<Brand> brandData = [];
List<Article> articles = [];
List<Goods> gooods = [];
Founder founder;
queryHome() async {
3 years ago
EasyLoading.show(status: S.of(context).zhengzaijiazai);
final SharedPreferences value = await SharedPreferences.getInstance();
3 years ago
apiService = ApiService(Dio(),
context: context, token: value.getString('token'), showLoading: false);
3 years ago
4 years ago
BaseData brand = await apiService.queryHomeBrand().catchError((onError) {
refreshController.refreshFailed();
});
if (brand != null && brand.isSuccess) {
4 years ago
brandData.clear();
brandData.addAll((brand.data["brandList"] as List<dynamic>)
.map((e) => Brand.fromJson(e))
.toList());
founder = Founder.fromJson(brand.data["founder"]);
}
3 years ago
BaseData<PageInfo<Article>> article = await apiService.queryArticle({
4 years ago
"pageNum": 1,
3 years ago
"pageSize": 3,
4 years ago
"searchKey": "",
4 years ago
"state": 1,
4 years ago
"type": 2
}).catchError((onError) {
refreshController.refreshFailed();
});
articles.clear();
if (article != null && article.isSuccess) {
3 years ago
articles.addAll(article.data.list);
4 years ago
}
3 years ago
BaseData<PageInfo<Goods>> goodsData = await apiService.creditGoods({
4 years ago
"orderDesc": true,
"orderType": 1,
"pageNum": 1,
"pageSize": 10,
"state": 1
3 years ago
}).catchError((onError) {
refreshController.refreshFailed();
4 years ago
});
if (goodsData != null && goodsData.isSuccess) {
gooods.clear();
3 years ago
gooods.addAll(goodsData.data.list);
4 years ago
}
3 years ago
BaseData<PageInfo<BannerData>> baseData = await apiService.queryBanner({
"model": {"type": "HOME_PAGE"},
}).catchError((onError) {
refreshController.refreshFailed();
});
if (baseData != null && baseData.isSuccess) {
3 years ago
bannerData.clear();
bannerData.addAll(baseData.data.records);
4 years ago
refreshController.refreshCompleted();
3 years ago
if (bannerData.length > 0) controller.move(0, animation: false);
4 years ago
} else {
refreshController.refreshFailed();
}
3 years ago
EasyLoading.dismiss();
4 years ago
}
4 years ago
@override
void dispose() {
super.dispose();
refreshController.dispose();
}
3 years ago
final RefreshController refreshController = RefreshController();
4 years ago
4 years ago
@override
Widget build(BuildContext context) {
4 years ago
super.build(context);
4 years ago
return Container(
4 years ago
child: SmartRefresher(
controller: refreshController,
enablePullDown: true,
enablePullUp: false,
header: MyHeader(),
4 years ago
physics: BouncingScrollPhysics(),
3 years ago
onRefresh: () {
3 years ago
setState(() {});
},
child: SingleChildScrollView(
4 years ago
physics: NeverScrollableScrollPhysics(),
3 years ago
child: FutureBuilder(
future: queryHome(),
builder: (context, snapshot) {
return Column(
children: [
3 years ago
///banner
HomeBanner(bannerData, controller),
///签到
SignView(),
///热门文章
HotArticle(articles),
///创始人故事
FounderStore(founder),
///品牌介绍
BrandView(brandData),
///积分商城
HomeIntegralStore(gooods, callback),
3 years ago
],
);
},
),
4 years ago
),
4 years ago
),
);
}
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}