import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:huixiang/generated/l10n.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:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/hot_item.dart'; import 'package:huixiang/view_widget/item_title.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:huixiang/view_widget/login_tips.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; class HomePage extends StatefulWidget { @override State createState() { return _HomePage(); } } class _HomePage extends State with AutomaticKeepAliveClientMixin { ApiService apiService; @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService(Dio(), context: context, token: value.getString('token'), showLoading: false), queryHome(), }); eventBus.on().listen((event) { if (event.type < 3) { setState(() {}); } }); } loginTips() { SmartDialog.show( widget: LoginTips( click: () { SharedPreferences.getInstance().then((value) => value.clear()); Navigator.of(context) .pushNamed('/router/login_page', arguments: {"login": "login"}); }, ), clickBgDismissTemp: false, ); } List bannerData = []; List brandData = []; List
articles = []; List gooods = []; Founder founder; queryHome() async { SmartDialog.showLoading(msg: S.of(context).zhengzaijiazai); 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"] as List) .map((e) => BannerData.fromJson(e)) .toList()); setState(() {}); } BaseData brand = await apiService.queryHomeBrand().catchError((onError) { refreshController.refreshFailed(); }).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"]); setState(() {}); } BaseData article = await apiService.queryArticle({ "pageNum": 1, "pageSize": 10, "searchKey": "", "state": 0, "type": 2 }).catchError((onError) { refreshController.refreshFailed(); }); articles.clear(); if (article != null && article.isSuccess) { PageInfo pageInfo = PageInfo.fromJson(article.data); articles.addAll(pageInfo.list.map((e) => Article.fromJson(e)).toList()); } BaseData goodsData = await apiService.creditGoods({ "orderDesc": true, "orderType": 1, "pageNum": 1, "pageSize": 10, "state": 1 }); if (goodsData != null && goodsData.isSuccess) { PageInfo page = PageInfo.fromJson(goodsData.data); gooods.clear(); gooods.addAll(page.list.map((e) => Goods.fromJson(e))); setState(() {}); } Future.delayed(Duration(milliseconds: 800), () { SmartDialog.dismiss(); }); if (baseData != null && baseData.isSuccess) { refreshController.refreshCompleted(); if (mounted) setState(() {}); } else { refreshController.refreshFailed(); } } @override void dispose() { super.dispose(); refreshController.dispose(); } 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: queryHome, child: SingleChildScrollView( physics: NeverScrollableScrollPhysics(), child: Column( children: [ banner(), ItemTitle( text: S.of(context).jinrihuiyuanrenwu, imgPath: "assets/image/icon_today_task.png", moreText: S.of(context).renwuzhongxin, onTap: () { SharedPreferences.getInstance().then((value) { if (value.getString('token') == null) { loginTips(); } else { Navigator.of(context).pushNamed('/router/integral_page'); } }); }, ), InkWell( onTap: () { SharedPreferences.getInstance().then((value) { if (value.getString('token') == null) { loginTips(); } else { Navigator.of(context).pushNamed('/router/integral_page'); } }); }, child: signIn(), ), ItemTitle( text: S.of(context).remenwenzhangshipin, imgPath: "assets/image/icon_today_video.png", moreText: S.of(context).chakangengduo, onTap: () { Navigator.of(context).pushNamed('/router/hot_article_page'); }, ), hotList(), ItemTitle( text: S.of(context).chuangshirendegushi, imgPath: "assets/image/icon_founder.png", ), GestureDetector( onTap: () { Navigator.of(context).pushNamed('/router/founder_story_page'); }, child: founderStore(), ), ItemTitle( text: S.of(context).pinpaijieshao, imgPath: "assets/image/icon_brand_introduction.png", ), brands(), ItemTitle( text: S.of(context).jifenshangcheng, imgPath: "assets/image/icon_points_mall.png", ), integralStore(), ], ), ), ), ); } Widget hotList() { return AspectRatio( aspectRatio: 2.47, child: Container( height: 151.h, child: Swiper( key: UniqueKey(), itemCount: articles != null ? articles.length : 0, viewportFraction: 0.95, scale: 1, loop: false, physics: BouncingScrollPhysics(), itemBuilder: (context, position) { return Container( margin: EdgeInsets.symmetric(vertical: 15.h, horizontal: 4.5.w), child: HotArticleItem(article: articles[position]), ); }, ), ), ); } Widget founderStore() { return Container( margin: EdgeInsets.only(left: 16, right: 16, top: 22, bottom: 30), decoration: BoxDecoration( boxShadow: [ BoxShadow( color: Colors.black.withAlpha(25), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Stack( alignment: Alignment.bottomCenter, clipBehavior: Clip.hardEdge, children: [ MImage( founder != null ? founder.imgUrl : "", aspectRatio: 2, radius: BorderRadius.circular(4), fit: BoxFit.cover, errorSrc: "assets/image/default_2_1.png", fadeSrc: "assets/image/default_2_1.png", ), Container( padding: EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.black.withAlpha(153), borderRadius: BorderRadius.only( bottomLeft: Radius.circular(4), bottomRight: Radius.circular(4), ), ), child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, children: [ Expanded( child: Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, children: [ Text( founder != null ? founder.name : "", style: TextStyle( fontSize: 14.sp, fontWeight: FontWeight.bold, color: Colors.white, ), ), SizedBox( width: 10.w, ), Text( S.of(context).jituanchuangshiren, style: TextStyle( fontSize: 8.sp, color: Colors.white, ), ), SizedBox( width: 20.w, ), Expanded( child: Text( S.of(context).jianjie( "${founder != null ? founder.description : ""}"), maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 10.sp, color: Colors.white, ), ), flex: 1, ) ], ), flex: 1, ), Icon( Icons.keyboard_arrow_right, size: 12, color: Colors.white, ), ], ), ), ], ), ); } Widget integralStore() { return GridView.builder( itemCount: gooods != null ? gooods.length : 0, padding: EdgeInsets.only(left: 13, right: 13, top: 13, bottom: 13), shrinkWrap: true, physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( //一行的Widget数量 crossAxisCount: 3, //水平子Widget之间间距 crossAxisSpacing: 13, //垂直子Widget之间间距 mainAxisSpacing: 16, //子Widget宽高比例 childAspectRatio: 0.67, ), itemBuilder: (contetx, index) { return GestureDetector( onTap: () { Navigator.of(context).pushNamed('/router/integral_store_page', arguments: {"goodsId": gooods[index].id}); }, child: buildItem(gooods[index]), ); }, ); } Widget buildItem(Goods goods) { return Container( alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.all( Radius.circular(4), ), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], color: Colors.white), child: Column( children: [ MImage( goods.mainImgPath, aspectRatio: 1, radius: BorderRadius.only( topLeft: Radius.circular(4), topRight: Radius.circular(4), ), fit: BoxFit.fill, errorSrc: "assets/image/default_1.png", fadeSrc: "assets/image/default_1.png", ), SizedBox( height: 4, ), Container( margin: EdgeInsets.only(left: 8, right: 8), child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( goods.name, overflow: TextOverflow.ellipsis, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 12.sp, ), ), SizedBox( height: 4.h, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: Text( S .of(context) .yuan_(double.tryParse(goods.worth).toInt()), overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xFF585858), decoration: TextDecoration.lineThrough, decorationColor: Color(0xFF585858), fontSize: 10.sp, ), ), flex: 1, ), Text( S.of(context).jifen_(goods.price), overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xFF32A060), fontSize: 14.sp, fontWeight: FontWeight.bold, ), ), ], ), ], ), ) ], ), ); } Widget signIn() { return Container( padding: EdgeInsets.only(left: 12.w, top: 8.h, bottom: 8.h, right: 12.w), margin: EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], borderRadius: BorderRadius.circular(4), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Image.asset( "assets/image/icon_calendar_today_sharp.png", width: 20, height: 20, ), SizedBox( width: 8, ), Text( S.of(context).qiandaolingqujinfen, style: TextStyle( color: Colors.black, fontSize: 14, ), ), ], ), Text( S.of(context).quqiandao, style: TextStyle( color: Color(0xFF727272), fontSize: 12, ), ), ], ), ); } // SwiperController swiperController = SwiperController(); brands() { return Container( height: 90.h, margin: EdgeInsets.only(bottom: 18.h), child: ListView.builder( padding: EdgeInsets.only(left: 10.w, right: 10.w), scrollDirection: Axis.horizontal, physics: ClampingScrollPhysics(), itemBuilder: (context, position) { return Container( height: 60.h, width: 120.w, margin: EdgeInsets.symmetric(horizontal: 3.w, vertical: 15.h), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black.withAlpha(37), offset: Offset(0, 2), blurRadius: 5, spreadRadius: 0, ) ], borderRadius: BorderRadius.circular(8), ), child: MImage( brandData != null ? brandData[position].image : "", radius: BorderRadius.circular(8), fit: BoxFit.cover, errorSrc: "assets/image/default_2_1.png", fadeSrc: "assets/image/default_2_1.png", ), ); }, itemCount: (brandData != null && brandData.length > 0) ? brandData.length : 0, ), // child: AspectRatio( // aspectRatio: 4.16, // child: Swiper( // scrollDirection: Axis.horizontal, // controller: swiperController, // loop: false, // viewportFraction: 0.32, // scale: 0.70, // index: (brandData != null && brandData.length > 0) ? 1 : 0, // itemBuilder: (context, position) { // if (brandData == null || brandData.length == 0) { // return Container(); // } // return Container( // margin: EdgeInsets.symmetric(horizontal: 3.w, vertical: 15.h), // decoration: BoxDecoration( // color: Colors.white, // boxShadow: [ // BoxShadow( // color: Colors.black.withAlpha(37), // offset: Offset(0, 2), // blurRadius: 5, // spreadRadius: 0, // ) // ], // borderRadius: BorderRadius.circular(8), // ), // child: MImage( // brandData != null ? brandData[position].image : "", // radius: BorderRadius.circular(8), // fit: BoxFit.cover, // errorSrc: "assets/image/default_2_1.png", // fadeSrc: "assets/image/default_2_1.png", // ), // ); // }, // itemCount: (brandData != null && brandData.length > 0) ? brandData.length : 1, // ), // ), ); } banner() { return Container( child: AspectRatio( aspectRatio: 2, child: Swiper( pagination: SwiperPagination( margin: EdgeInsets.only(bottom: 20), alignment: Alignment.bottomCenter, builder: DotSwiperPaginationBuilder( size: 8, activeSize: 8, space: 5, activeColor: Colors.white, color: Colors.white.withAlpha(76), ), ), physics: BouncingScrollPhysics(), loop: false, itemBuilder: (context, position) { return InkWell( onTap: () { bannerClick(bannerData[position]); }, child: Container( margin: EdgeInsets.all(16), decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(8))), child: MImage( (bannerData != null && position < bannerData.length) ? bannerData[position].imgUrl ?? "" : "", radius: BorderRadius.all(Radius.circular(8)), fit: BoxFit.cover, errorSrc: "assets/image/default_2_1.png", fadeSrc: "assets/image/default_2_1.png", ), ), ); }, itemCount: bannerData != null ? bannerData.length : 1), ), ); } /// 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; } } @override bool get wantKeepAlive => true; }