import 'dart:convert'; import 'dart:ui'; 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/banner.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/goods.dart'; import 'package:huixiang/retrofit/data/page.dart'; import 'package:huixiang/retrofit/data/user_info.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/event_type.dart'; import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/view_widget/classic_header.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/item_title.dart'; import 'package:huixiang/view_widget/login_tips.dart'; import 'package:huixiang/view_widget/my_footer.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:dio/dio.dart'; import 'package:shared_preferences/shared_preferences.dart'; class PointsMallPage extends StatefulWidget { @override State createState() { return _PointsMallPage(); } } class _PointsMallPage extends State with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin { var _itemText = S.current.morenpaixu; ApiService client; RefreshController _refreshController = RefreshController(initialRefresh: false); List sortString = [ S.current.morenpaixu, // S.current.duihuanlianggaodaodi, // S.current.duihuanliangdidaogao, S.current.jifengaodaodi, S.current.jifendidaogao, ]; @override void dispose() { super.dispose(); _refreshController.dispose(); } @override void initState() { super.initState(); refreshUserInfo(); eventBus.on().listen((event) { if (event.type < 3) { setState(() {}); } if (event.type == 3) { refreshUserInfo(); } }); } refreshUserInfo() { SharedPreferences.getInstance().then((value) => { client = ApiService(Dio(), context: context, token: value.getString('token')), creditGoods(categoryId), queryUser(), }); } int pageNum = 1; //排序类型枚举:1-自然排序,2-销量,3-价格 int orderType = 1; //是否降序排列 bool orderDesc = true; PageInfo page; List gooods = []; UserInfo userinfo; List bannerData = []; queryUser() async { BaseData banner = await client.queryBanner({ "model": {"type": "CREDIT_INDEX"}, }); if (banner != null) { bannerData.clear(); bannerData.addAll((banner.data["records"] as List) .map((e) => BannerData.fromJson(e)) .toList()); setState(() {}); } BaseData baseData = await client.queryInfo(); if (baseData != null && baseData.isSuccess) { userinfo = UserInfo.fromJson(baseData.data); SharedPreferences.getInstance().then((value) => { value.setString('user', jsonEncode(baseData.data)), }); setState(() {}); } } creditGoods(categoryId) async { var param = { "categoryId": categoryId, "orderDesc": orderDesc, "orderType": orderType, "pageNum": pageNum, "pageSize": 10, "state": 1 }; BaseData baseData = await client.creditGoods(param); if (baseData != null && baseData.isSuccess) { page = PageInfo.fromJson(baseData.data); if (pageNum == 1) { gooods.clear(); } gooods.addAll(page.list.map((e) => Goods.fromJson(e))); setState(() { _refreshController.refreshCompleted(); _refreshController.loadComplete(); if (page.pageNum == page.pages) { _refreshController.loadNoData(); } else { pageNum += 1; } }); } else { _refreshController.loadFailed(); _refreshController.refreshFailed(); } } int categoryId; _refresh() { pageNum = 1; creditGoods(categoryId); queryUser(); } @override Widget build(BuildContext context) { super.build(context); List> sortItems = []; sortString.forEach((element) { sortItems.add(DropdownMenuItem(value: element, child: Text(element))); }); return SmartRefresher( enablePullDown: true, enablePullUp: true, header: MyHeader(), physics: BouncingScrollPhysics(), footer: CustomFooter( loadStyle: LoadStyle.ShowWhenLoading, builder: (BuildContext context, LoadStatus mode) { return MyFooter(mode); }, ), controller: _refreshController, onRefresh: _refresh, onLoading: creditGoods(categoryId), child: SingleChildScrollView( child: Container( color: Color(0xFFFAFAFA), child: Column( children: [ banner(), userItem(), ItemTitle( text: S.of(context).jifenshangcheng, imgPath: "assets/image/icon_points_mall.png", moreText: _itemText, moreType: 1, items: sortItems, onChanged: _sortChange, ), Container( alignment: Alignment.centerLeft, child: DefaultTabController( length: 4, child: TabBar( isScrollable: true, //可滚动 indicatorColor: Color(0xff39B54A), labelColor: Color(0xff32A060), labelStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold), unselectedLabelStyle: TextStyle( fontSize: 14, fontWeight: FontWeight.w400, ), // controller: tabController, //未选中文字颜色 unselectedLabelColor: Color(0xff4D4D4D), indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽 tabs: [ Tab(text: S.current.quanbu), Tab(text: S.current.yinshi), Tab(text: S.current.shenghuoyule), Tab(text: S.current.bangong), ], onTap: (index) { categoryId = index == 0 ? null : index; pageNum = 1; creditGoods(categoryId); }, ), ), ), GridView.builder( itemCount: gooods.length, padding: EdgeInsets.only( left: 16.w, right: 16.w, top: 16.h, bottom: 16.h), shrinkWrap: true, physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( //一行的Widget数量 crossAxisCount: 2, //水平子Widget之间间距 crossAxisSpacing: 11.w, //垂直子Widget之间间距 mainAxisSpacing: 16.w, //子Widget宽高比例 0.59 childAspectRatio: 166.w / (281.h / 2 + (281.h / 2) * AppUtils.textScale(context)), ), itemBuilder: (contetx, index) { return GestureDetector( onTap: () { _toDetails(index); }, child: buildItem(gooods[index]), ); }) ], ), ), ), ); } _toDetails(index) async { await Navigator.of(context).pushNamed('/router/integral_store_page', arguments: {"goodsId": gooods[index].id}); SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); String token = sharedPreferences.getString("token"); if (token != null) queryUser(); } _sortChange(item) { var index = sortString.indexOf(item); switch (index) { case 0: orderType = 1; break; //兑换量 // case 1: // orderType = 2; // orderDesc = true; // break; // case 2: // orderType = 2; // orderDesc = false; // break; case 1: orderType = 3; orderDesc = true; break; case 2: orderType = 3; orderDesc = false; break; } creditGoods(categoryId); setState(() { _itemText = item; }); } Widget buildItem(Goods goods) { return Container( alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.circular(4), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], color: Colors.white), child: Stack( alignment: AlignmentDirectional.topEnd, fit: StackFit.loose, children: [ Column( mainAxisSize: MainAxisSize.max, children: [ MImage( goods.mainImgPath, aspectRatio: 1, radius: BorderRadius.only( topLeft: Radius.circular(2), topRight: Radius.circular(2), ), fit: BoxFit.cover, errorSrc: "assets/image/default_1.png", fadeSrc: "assets/image/default_1.png", ), Expanded( child: Container( margin: EdgeInsets.only(left: 12.w, right: 12.w, top: 10.h,), child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( goods.name, overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xff353535), fontWeight: FontWeight.w600, fontSize: 16.sp, ), ), SizedBox( height: 8.h, ), Container( height: 30.h * AppUtils.textScale(context), child: Text( goods.description, maxLines: 2, style: TextStyle( color: Color(0xFF727272), fontSize: 12.sp, ), ), ), SizedBox( height: 4.h, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ // 兑换数量 // Text( // S.of(context).yiduihuanjian("${goods.sales}万"), // style: TextStyle( // color: Color(0xFFA29E9E), // fontSize: 10.sp, // ), // ), Text( S.of(context).yuan_(goods.worth), style: TextStyle( color: Color(0xFF585858), decoration: TextDecoration.lineThrough, decorationColor: Color(0xFF585858), fontSize: 12.sp, ), ), Text( S.of(context).jifen_(goods.price), style: TextStyle( color: Color(0xFF32A060), fontSize: 14.sp, fontWeight: FontWeight.bold, ), ), ], ), ], ), ), flex: 1, ), SizedBox( height: 10.h, ), ], ), Visibility( visible: goods.isHot, child: ClipRRect( borderRadius: BorderRadius.only(topRight: Radius.circular(4)), child: Image.asset( "assets/image/icon_hot_right_top.png", width: 36.w, height: 36.h, ), ), ), ], ), ); } Widget userItem() { return InkWell( onTap: () { SharedPreferences.getInstance().then((value) { if (value.getString("token") == null) { SmartDialog.show( widget: LoginTips( click: () { value.clear(); Navigator.of(context).pushNamed('/router/login_page', arguments: {"login": "login"}); }, ), clickBgDismissTemp: false, ); } }); }, child: Container( margin: EdgeInsets.all(16), child: Row( children: [ MImage( userinfo != null ? userinfo.headimg : "", width: 50, height: 50, radius: BorderRadius.all(Radius.circular(2)), fit: BoxFit.cover, errorSrc: "assets/image/default_1.png", fadeSrc: "assets/image/default_1.png", ), Expanded( child: Container( margin: EdgeInsets.only(left: 15), height: 50.h, child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( userinfo != null ? userinfo.nickname : S.of(context).qudenglu, style: TextStyle( fontSize: 14, fontWeight: FontWeight.bold, color: Color(0xFF353535)), ), Row( children: [ Image.asset( "assets/image/icon_an_crown.png", width: 14, height: 14, ), SizedBox( width: 5, ), Text( (userinfo != null && userinfo.memberRankVo != null) ? userinfo.memberRankVo.rankName : "", style: TextStyle(fontSize: 12, color: Color(0xFF353535)), ), ], ), ], ), ), flex: 1, ), Container( margin: EdgeInsets.only(left: 15), height: 50.h, child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( S.of(context).yiyoujifen, style: TextStyle( fontSize: 12, fontWeight: FontWeight.bold, color: Color(0xFF4C4C4C)), ), Text( (userinfo != null) ? "${userinfo.points}" : "", style: TextStyle( fontSize: 16, color: Color(0xFFF8BA61), fontWeight: FontWeight.bold), ), ], ), ) ], ), ), ); } banner() { return Container( margin: EdgeInsets.only(top: 16, bottom: 16), child: AspectRatio( aspectRatio: 2.0, child: Swiper( pagination: SwiperPagination( alignment: Alignment.bottomCenter, builder: DotSwiperPaginationBuilder( size: 8, activeSize: 8, space: 5, activeColor: Colors.black, color: Colors.black.withAlpha(76))), viewportFraction: 0.7, scale: 0.7, loop: false, itemBuilder: (context, position) { return InkWell( onTap: () { bannerClick(bannerData[position]); }, child: Container( margin: EdgeInsets.only(bottom: 40), decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(8))), child: MImage( bannerData != null && position < bannerData.length ? bannerData[position].imgUrl : "", radius: BorderRadius.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; }