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_swiper/flutter_swiper.dart'; import 'package:huixiang/generated/l10n.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/sign_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/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:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'home_view/brand_view.dart'; import 'home_view/founder_store.dart'; import 'home_view/hot_article.dart'; class HomePage extends StatefulWidget { final GestureTapCallback callback; HomePage(this.callback); @override State createState() { return _HomePage(callback); } } class _HomePage extends State with AutomaticKeepAliveClientMixin { ApiService apiService; final GestureTapCallback callback; _HomePage(this.callback); @override void initState() { super.initState(); eventBus.on().listen((event) { if (event.type < 3) { setState(() {}); } }); } final SwiperController controller = SwiperController(); List bannerData = []; List brandData = []; List
articles = []; List 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) .map((e) => Brand.fromJson(e)) .toList()); founder = Founder.fromJson(brand.data["founder"]); } final BaseData> 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> 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); } final BaseData> 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 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), ///积分商城 HomeIntegralStore(gooods, callback), ], ); }, ), ), ), ); } @override bool get wantKeepAlive => true; }