import 'dart:convert'; import 'package:farmer_client/pages/home/widgets/home/widgets/headers.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_easyrefresh/easy_refresh.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:get/get.dart'; import '../../models/app.dart'; import '../../models/entities/CardListModel.dart'; import '../../models/entities/CardTypeModel.dart'; import '../../services/homeAPI.dart'; import '../../widgets/CarsCard.dart'; import '../../widgets/NullCard.dart'; import '../../widgets/Search.dart'; class MoreCars extends StatefulWidget { const MoreCars({Key? key}) : super(key: key); @override State createState() => _MoreCarsState(); } class _MoreCarsState extends State with TickerProviderStateMixin { late TabController _controller; late EasyRefreshController _controllerList; final tabText = Rx>([]); final classId = Rx>([]); final cardTypeList = Rx>([]); final newCardTypeList = Rx>([]); var pageIndex = 1; //页数 var count = 10; //每页10条 late int maxSum; //最多条数 String value=''; //最多条数 int _count = 0; String? location = AppController.t.locationStr; bool hasTopLoad = false; int index = 0; String typeId=''; Map params = {'pageNum': 1,'location':AppController.t.locationStr,'typeId':''}; @override void initState() { // TODO: implement initState super.initState(); _controller = TabController(length: tabText.value.length, vsync: this); _controllerList = EasyRefreshController(); // _controller.addListener(() { // setState(() => _selectedIndex = _controller.index); // print("liucheng-> ${_controller.indexIsChanging}"); // }); _geList(); getNewData(params); // _getAllCarsList(); } @override void dispose() { super.dispose(); _controller.dispose(); _controllerList.dispose(); } void getNewData(Map? params) { pageIndex = 1; params!['pageNum'] = pageIndex; print(params['pageNum']); getMachinery(params).then((value) { maxSum = value['total']; List list = []; value['records'].forEach((item) { list.add(CardListModel.fromJson(item)); }); cardTypeList(list); newCardTypeList(list); _count = cardTypeList.value.length; }); } void _geList() { getMachineryType().then((value) { EasyLoading.show(status: '数据加载中...'); final list = []; List classtypeId = []; List newTabs = [ CardTypeModel.fromJson({'name': '全部'}) ]; value['records'].forEach((item) { list.add(CardTypeModel.fromJson(item)); }); list.insertAll(0, newTabs); tabText(list); tabText().forEach((item) { classtypeId.add(item.typeId); }); classId(classtypeId); hasTopLoad = true; _controller = TabController(length: tabText.value.length, vsync: this); print(hasTopLoad); EasyLoading.dismiss(); }); } void getMoreData(Map? params) { pageIndex++; params!['pageNum'] = pageIndex; print(params['pageNum']); searchMachinery(params).then((value) { maxSum = value['total']; List list = []; List Newlist = []; value['records'].forEach((item) { list.add(CardListModel.fromJson(item)); }); newCardTypeList.value.addAll(list); Newlist.addAll(newCardTypeList.value); cardTypeList(Newlist); _count = cardTypeList.value.length; }); } void _handleTabChange(i) { index = i; if (index == 0) { Map paramss = {'pageNum': 1,'location':location}; getNewData(paramss); } else { // final typeId = classId.value[index]; print('typeId+$typeId'); typeId=classId.value[index]; print('typeId+$typeId'); // Map params = {'pageNum': 1,'location':location,'typeId':typeId.toString()}; params['typeId']=classId.value[index]; getNewData(params); } } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: true, appBar: AppBar( centerTitle: true, backgroundColor: Colors.transparent, foregroundColor: const Color(0xFF333333), elevation: 0, toolbarHeight: 44.w, title: Text( '更多', style: TextStyle( color: const Color(0xFF333333), fontWeight: FontWeight.bold, fontSize: 17.sp, letterSpacing: 2), ), ), body: SafeArea( child: Container( alignment: Alignment.center, decoration: BoxDecoration(color: Colors.white), child: !hasTopLoad ? Column( children: [ ListView( shrinkWrap: true, physics: NeverScrollableScrollPhysics(), children: [ Container( padding: EdgeInsets.fromLTRB(0, 15, 0, 15), width: 350.w, child: TypeHeader( type: false, ), ), Obx(() => TabBar( labelStyle: TextStyle( fontSize: ScreenUtil().setSp(19), fontWeight: FontWeight.bold), //选中的样式 unselectedLabelStyle: TextStyle(fontSize: ScreenUtil().setSp(16)), //未选中的样式 controller: _controller, isScrollable: true, //可滚动 indicatorColor: Colors.black, //指示器的颜色 labelColor: Colors.black, //选中文字颜色 unselectedLabelColor: Color(0xffadadad), // indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽 tabs: tabText.value .map((e) => Tab(text: e.name.toString())) .toList(), onTap: (int i) { _handleTabChange(i); }, )), ], ), Expanded( child: _buildListView(context), ) ], ) : Text('加载中...'), ))); } Widget _buildListView(BuildContext context) { return EasyRefresh( controller: _controllerList, firstRefresh: true, onRefresh: () async { await Future.delayed(Duration(seconds: 1), () { print("下拉刷新-----"); // Map params = {'pageNum': pageIndex,'location':AppController.t.locationStr,'q':value}; getNewData(params); _controllerList.resetLoadState(); }); }, onLoad: () async { await Future.delayed(Duration(seconds: 1), () { if (_count == maxSum) { Fluttertoast.showToast(msg: '暂无更多数据哦'); } else { // Map params = {'pageNum': pageIndex,'location':AppController.t.locationStr,'q':value}; getMoreData(params); } _controllerList.finishLoad(noMore: _count >= maxSum); }); }, child: Obx( () => cardTypeList.value.length > 0 ? Column( mainAxisSize: MainAxisSize.min, children: cardTypeList.value .map((item) => CarsCard(item: item)) .toList(), ): NullCard(text: '暂无农机信息!'), ), ); } }