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.

537 lines
17 KiB

4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.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/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_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:flutter_screenutil/flutter_screenutil.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
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HomePage();
}
}
4 years ago
class _HomePage extends State<HomePage> with AutomaticKeepAliveClientMixin {
4 years ago
ApiService apiService;
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) => {
4 years ago
apiService = ApiService(Dio(),
context: context, token: value.getString('token')),
4 years ago
queryHome(),
});
4 years ago
eventBus.on<EventType>().listen((event) {
setState(() {});
});
4 years ago
}
List<BannerData> bannerData = [];
List<Brand> brandData = [];
List<Article> articles = [];
List<Goods> gooods = [];
Founder founder;
queryHome() async {
BaseData baseData = await apiService.queryBanner({
4 years ago
"model": {"type": "HOME_PAGE"},
4 years ago
}).catchError((onError) {
refreshController.refreshFailed();
});
if (baseData != null && baseData.isSuccess) {
4 years ago
bannerData.clear();
bannerData.addAll((baseData.data["records"] as List<dynamic>)
.map((e) => BannerData.fromJson(e))
.toList());
}
BaseData brand = await apiService.queryHomeBrand().catchError((onError) {
refreshController.refreshFailed();
}).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"]);
}
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) {
4 years ago
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) {
4 years ago
refreshController.refreshCompleted();
if(mounted)
setState(() {});
4 years ago
} else {
refreshController.refreshFailed();
}
}
RefreshController refreshController = RefreshController();
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(),
4 years ago
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,
4 years ago
onTap: () {
Navigator.of(context)
.pushNamed('/router/integral_page');
},
4 years ago
),
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(),
),
4 years ago
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(),
],
),
4 years ago
),
);
}
Widget hotList() {
return AspectRatio(
aspectRatio: 2.47,
child: Container(
height: 151,
child: Swiper(
4 years ago
key: UniqueKey(),
4 years ago
itemCount: articles != null ? articles.length : 0,
4 years ago
viewportFraction: 0.95,
scale: 1,
loop: false,
physics: BouncingScrollPhysics(),
itemBuilder: (context, position) {
return Container(
margin: EdgeInsets.symmetric(vertical: 15, horizontal: 4.5),
4 years ago
child: HotArticleItem(article: articles[position]),
4 years ago
);
},
),
),
);
}
4 years ago
Widget founderStore() {
4 years ago
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: [
4 years ago
MImage(
founder != null ? founder.imgUrl : "",
4 years ago
aspectRatio: 2.1375,
4 years ago
radius: BorderRadius.circular(4),
fit: BoxFit.cover,
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
4 years ago
),
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(
4 years ago
text: founder != null ? founder.name : "",
4 years ago
style: TextStyle(
fontSize: 14.sp,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
TextSpan(
4 years ago
text: " 集团创始人",
4 years ago
style: TextStyle(
fontSize: 8.sp,
color: Colors.white,
),
),
TextSpan(
4 years ago
text:
" 简介:${founder != null ? founder.description : ""}",
4 years ago
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(
4 years ago
itemCount: gooods != null ? gooods.length : 0,
4 years ago
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宽高比例
4 years ago
childAspectRatio: 0.67,
4 years ago
),
itemBuilder: (contetx, index) {
return GestureDetector(
onTap: () {
4 years ago
Navigator.of(context).pushNamed('/router/integral_store_page',
arguments: {"goodsId": gooods[index].id});
4 years ago
},
4 years ago
child: buildItem(gooods[index]),
4 years ago
);
},
);
}
4 years ago
Widget buildItem(Goods goods) {
4 years ago
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(4),
),
boxShadow: [
BoxShadow(
4 years ago
color: Colors.black.withAlpha(12),
offset: Offset(0, 3),
blurRadius: 14,
spreadRadius: 0,
)
4 years ago
],
color: Colors.white),
child: Column(
children: [
4 years ago
MImage(
goods.mainImgPath,
aspectRatio: 1,
radius: BorderRadius.only(
topLeft: Radius.circular(4),
topRight: Radius.circular(4),
4 years ago
),
4 years ago
fit: BoxFit.fill,
errorSrc: "assets/image/default_1.png",
fadeSrc: "assets/image/default_1.png",
4 years ago
),
4 years ago
SizedBox(
height: 4,
),
4 years ago
Container(
4 years ago
margin: EdgeInsets.only(left: 8, right: 8),
4 years ago
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
4 years ago
goods.name,
4 years ago
overflow: TextOverflow.ellipsis,
4 years ago
style: TextStyle(
4 years ago
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 12.sp,
),
),
SizedBox(
height: 4,
4 years ago
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
4 years ago
Expanded(
child: Text(
4 years ago
S
.of(context)
4 years ago
.yuan_(double.tryParse(goods.worth).toInt()),
4 years ago
overflow: TextOverflow.ellipsis,
4 years ago
style: TextStyle(
4 years ago
color: Color(0xFF585858),
decoration: TextDecoration.lineThrough,
decorationColor: Color(0xFF585858),
fontSize: 10.sp,
),
4 years ago
),
flex: 1,
4 years ago
),
4 years ago
Text(
S.of(context).jifen_(goods.price),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Color(0xFF32A060),
fontSize: 14.sp,
fontWeight: FontWeight.bold,
),
),
4 years ago
],
),
],
),
)
],
),
);
}
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: [
4 years ago
Image.asset(
"assets/image/icon_calendar_today_sharp.png",
width: 20,
height: 20,
4 years ago
),
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,
),
4 years ago
),
4 years ago
],
),
);
}
brands() {
return Container(
margin: EdgeInsets.only(bottom: 18),
child: AspectRatio(
aspectRatio: 4.16,
child: Swiper(
4 years ago
key: UniqueKey(),
4 years ago
loop: false,
viewportFraction: 0.32,
scale: 0.70,
4 years ago
index: (brandData != null && brandData.length > 1) ? 1 : 0,
4 years ago
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)),
),
4 years ago
child: MImage(
4 years ago
brandData != null ? brandData[position].image : "",
4 years ago
radius: BorderRadius.all(Radius.circular(8)),
fit: BoxFit.cover,
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
4 years ago
),
);
},
4 years ago
itemCount: brandData != null ? brandData.length : 0),
4 years ago
),
);
}
banner() {
return Container(
child: AspectRatio(
4 years ago
aspectRatio: 2,
4 years ago
child: Swiper(
pagination: SwiperPagination(
4 years ago
margin: EdgeInsets.only(bottom: 20),
4 years ago
alignment: Alignment.bottomCenter,
builder: DotSwiperPaginationBuilder(
size: 8,
activeSize: 8,
space: 5,
activeColor: Colors.white,
color: Colors.white.withAlpha(76),
),
),
4 years ago
physics: BouncingScrollPhysics(),
loop: false,
4 years ago
itemBuilder: (context, position) {
return Container(
4 years ago
margin: EdgeInsets.all(16),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8))),
child: MImage(
4 years ago
(bannerData != null && position < bannerData.length)
4 years ago
? bannerData[position].imgUrl ?? ""
4 years ago
: "",
4 years ago
radius: BorderRadius.all(Radius.circular(8)),
fit: BoxFit.cover,
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
),
);
4 years ago
},
4 years ago
itemCount: bannerData != null ? bannerData.length : 1),
4 years ago
),
);
}
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}