import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.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_swiper_null_safety/flutter_swiper_null_safety.dart'; import 'package:flutter_screenutil/flutter_screenutil.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')), queryHome(), }); eventBus.on().listen((event) { setState(() {}); }); } List bannerData = []; List brandData = []; List
articles = []; List gooods = []; Founder founder; queryHome() async { 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()); } 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"]); } 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))); } if (baseData != null && baseData.isSuccess) { refreshController.refreshCompleted(); if(mounted) setState(() {}); } else { refreshController.refreshFailed(); } } 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: ListView( physics: BouncingScrollPhysics(), children: [ banner(), ItemTitle( text: S.of(context).jinrihuiyuanrenwu, imgPath: "assets/image/icon_today_task.png", moreText: S.of(context).renwuzhongxin, onTap: () { Navigator.of(context) .pushNamed('/router/integral_page'); }, ), InkWell( onTap: () { Navigator.of(context).pushNamed('/router/integral_page'); }, child: signIn(), ), ItemTitle( text: S.of(context).remenwenzhangshipin, imgPath: "assets/image/icon_today_task.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_today_task.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, 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, horizontal: 4.5), child: HotArticleItem(article: articles[position]), ); }, ), ), ); } Widget founderStore() { return Container( margin: EdgeInsets.only(left: 16, right: 16, top: 22, bottom: 30), 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: Stack( alignment: Alignment.bottomCenter, clipBehavior: Clip.hardEdge, children: [ MImage( founder != null ? founder.imgUrl : "", aspectRatio: 2.1375, 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( children: [ Expanded( child: Text.rich( TextSpan(children: [ TextSpan( text: founder != null ? founder.name : "", style: TextStyle( fontSize: 14.sp, fontWeight: FontWeight.bold, color: Colors.white, ), ), TextSpan( text: " 集团创始人", style: TextStyle( fontSize: 8.sp, color: Colors.white, ), ), TextSpan( text: " 简介:${founder != null ? founder.description : ""}", style: TextStyle( fontSize: 10.sp, color: Colors.white, ), ), ]), maxLines: 1, overflow: TextOverflow.ellipsis, ), ), 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, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: Text( S .of(context) .yuan(double.tryParse(goods.worth).toInt()), 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, top: 8, bottom: 8, right: 12), 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.all(Radius.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, ), ), ], ), ); } brands() { return Container( margin: EdgeInsets.only(bottom: 18), child: AspectRatio( aspectRatio: 4.16, child: Swiper( key: UniqueKey(), loop: false, viewportFraction: 0.32, scale: 0.70, index: (brandData != null && brandData.length > 1) ? 1 : 0, itemBuilder: (context, position) { return Container( margin: EdgeInsets.symmetric(horizontal: 3, vertical: 15), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black.withAlpha(37), offset: Offset(0, 2), blurRadius: 5, spreadRadius: 0) ], borderRadius: BorderRadius.all(Radius.circular(8)), ), child: MImage( brandData != null ? brandData[position].image : "", radius: BorderRadius.all(Radius.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), ), ); } 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 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), ), ); } @override bool get wantKeepAlive => true; }