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.
 
 
 
 
 
 

305 lines
10 KiB

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_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:huixiang/home/help_farmers/farmers_tab.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/my_footer.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../generated/l10n.dart';
import '../../retrofit/data/base_data.dart';
import '../../retrofit/data/farmers_list.dart';
import '../../utils/flutter_utils.dart';
import '../../utils/font_weight.dart';
import '../../view_widget/custom_image.dart';
import '../../view_widget/my_tab.dart';
import '../../view_widget/no_data_view.dart';
class HelpFarmersPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HelpFarmersPage();
}
}
class _HelpFarmersPage extends State<HelpFarmersPage> {
ApiService apiService;
final RefreshController refreshController = RefreshController();
List<FarmersList> farmersList = [];
int tabIndex = 0;
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) => {
apiService = ApiService(
Dio(),
context: context,
token: value.getString('token'),
// showLoading: true
),
queryConfig(),
});
}
///助农列表
queryConfig() async {
try{
if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
}
BaseData<List<FarmersList>> baseData =
await apiService.getConfig().catchError((error) {
SmartDialog.showToast(AppUtils.dioErrorTypeToString(error.type),
alignment: Alignment.center);
refreshController.refreshFailed();
});
if (baseData != null && baseData.isSuccess) {
farmersList.clear();
farmersList.addAll(baseData.data);
refreshController.refreshCompleted();
} else {
refreshController.refreshFailed();
}
}finally{
setState(() {});
EasyLoading.dismiss();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: NestedScrollView(
headerSliverBuilder: (context, inner) {
return [
SliverAppBar(
pinned: true,
backgroundColor: Colors.white,
elevation: 0,
leading: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Container(
alignment: Alignment.centerRight,
margin: EdgeInsets.only(left: 10.w),
padding: EdgeInsets.all(6),
child: Icon(
Icons.arrow_back_ios,
color: Colors.white,
size: 24,
),
),
),
///去掉返回按钮
// automaticallyImplyLeading: false,
centerTitle: true,
flexibleSpace: FlexibleSpaceBar(
background: MImage(
(farmersList.isNotEmpty)?farmersList[tabIndex]?.agriculturaListImg : "",
fit: BoxFit.fill,
width: double.infinity,
height: 354.h,
errorSrc: "assets/image/default_1.webp",
fadeSrc: "assets/image/default_1.webp",
)),
expandedHeight: 354.h,
bottom: PreferredSize(
preferredSize: Size(double.infinity, 0),
child: DefaultTabController(
length: farmersList.length,
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(
top: Radius.circular(16),
),
color: Color(0xFF32A060),
),
padding: EdgeInsets.only(top:7.h,bottom: 7.h),
child: TabBar(
isScrollable: true,
//可滚动
indicatorColor: Colors.white,
labelColor: Colors.white,
labelStyle: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.bold,
),
unselectedLabelStyle: TextStyle(
fontSize: 14.sp,
fontWeight: MyFontWeight.regular,
),
indicator: FarmersTab(),
// controller: tabController,
//未选中文字颜色
unselectedLabelColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
//指示器与文字等宽
tabs: farmersList == null
? []
: farmersList
.map((e) => MyTab(text: e.typeName))
.toList(),
onTap: (index) {
// queryConfig("AgriculturalList");
tabIndex = index;
queryConfig();
setState(() {});
},
),
),
),
),
),
];
},
body: recommend(),
),
);
}
///为你推荐
Widget recommend() {
return Container(
color: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 10.w),
child: (farmersList.isNotEmpty && farmersList[tabIndex].goodList.length == 0)
? NoDataView(
src: "assets/image/xiao_fei.webp",
isShowBtn: false,
text: "当前分类暂无商品",
fontSize: 16.sp,
margin: EdgeInsets.only(top: 120.h, left: 60.w, right: 60.w),
)
: StaggeredGridView.countBuilder(
crossAxisCount: 2,
shrinkWrap: true,
itemCount: (farmersList != null && farmersList.isNotEmpty) ? farmersList[tabIndex].goodList.length : 0,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
physics: BouncingScrollPhysics(),
itemBuilder: (context, position) {
return GestureDetector(
onTap: () {
Navigator.of(context)
.pushNamed('/router/shop_details_page', arguments: {
"id": farmersList[tabIndex].goodList[position].id,
"storeId": farmersList[tabIndex].goodList[position].storeId,
});
},
child: recommendItem(farmersList[tabIndex].goodList[position]),
);
},
staggeredTileBuilder: (position) {
return StaggeredTile.fit(1);
},
),
);
}
Widget recommendItem(GoodList goodList) {
return Container(
width: 173,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
boxShadow: [
BoxShadow(
color: Color(0x1A213303),
offset: Offset(0, 2),
blurRadius: 4,
spreadRadius: 0,
)
],
color: Colors.white,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MImage(
goodList.productImg ?? "",
fit: BoxFit.fill,
width:double.infinity,
height: 174,
radius: BorderRadius.only(
topLeft: Radius.circular(6),
topRight: Radius.circular(6),
),
errorSrc: "assets/image/default_1.webp",
fadeSrc: "assets/image/default_1.webp",
),
Padding(
padding: EdgeInsets.only(top: 7.h, left: 10.w, bottom: 8.h),
child: Text(
goodList?.productName ?? "",
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14.sp,
height: 1.3.h,
fontWeight: MyFontWeight.medium,
color: Color(0xFF0D0D0D),
),
),
),
Padding(
padding: EdgeInsets.only(left: 10.w, bottom: 12.h),
child:Row(
children: [
Text(
AppUtils.calculateDouble(double.tryParse(goodList?.price) ?? 0),
style: TextStyle(
fontSize:24.sp,
fontFamily: 'APPHT',
fontWeight: MyFontWeight.medium,
color: Color(0xFF32A060),
),
),
Container(
margin: EdgeInsets.only(left: 8.w),
padding: EdgeInsets.symmetric(vertical:3.h,horizontal: 9.w),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
boxShadow: [
BoxShadow(
color: Color(0x1A213303),
offset: Offset(0, 2),
blurRadius: 4,
spreadRadius: 0,
)
],
color: Color(0xFF32A060),
),
child:
Text(
S.of(context).zhunongjifen,
style: TextStyle(
fontSize:12.sp,
fontWeight: MyFontWeight.medium,
color: Colors.white,
),
),
)
],
),
),
],
),
);
}
}