import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/community/community_view/community_dynamic.dart'; import 'package:huixiang/retrofit/data/article.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/comunity_comment.dart'; import 'package:huixiang/retrofit/data/page.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/view_widget/classic_header.dart'; import 'package:huixiang/view_widget/my_footer.dart'; import 'package:huixiang/view_widget/no_data_view.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class CommunityChildPage extends StatefulWidget { final String typeStr; final Function onScroll; CommunityChildPage(Key key, this.typeStr,this.onScroll): super(key: key); @override State createState() { return CommunityChildPageState(); } } class CommunityChildPageState extends State with AutomaticKeepAliveClientMixin { RefreshController refreshController = RefreshController(); ApiService apiService; int pageNum = 1; String userId; bool isLoadMore = false; bool isRefresh = true; bool isLoadingData = false; ScrollController sc = ScrollController(); List
articles = []; @override void initState() { super.initState(); sc.addListener(() { widget.onScroll(); }); onRefresh(); } onRefresh() async { setState(() {}); } ///动态列表 queryCommunity(String searchKey) async { if(!isRefresh){ isRefresh = true; return; } if(isLoadingData){ return; } isLoadingData = true; if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); userId = value.getString('userId'); apiService = ApiService( Dio(), context: context, token: value.getString("token"), showLoading: false ); } if(isLoadMore){ pageNum += 1; isLoadMore = false; } else if(searchKey == null)pageNum = 1; BaseData> baseData = await apiService.trendList({ "mid":"", "onlyFollow": widget.typeStr == "关注" ? true : false, "onlyMe": false, "pageNum": searchKey == null?pageNum:1, "pageSize": 10, "searchKey": searchKey??"" }).catchError((error) { if(searchKey == null) { refreshController.refreshFailed(); refreshController.loadFailed(); } }); if(searchKey == null) { refreshController.refreshCompleted(); refreshController.loadComplete(); } if (baseData.isSuccess) { if(searchKey != null){ if(baseData?.data?.list != null && baseData.data.list.isNotEmpty) articles.forEach((element) { if(element.id == searchKey){ element.content = jsonEncode(baseData.data.list[0].subjectInfo); element.mainTitle =baseData.data.list[0].subject; element.followed = baseData.data.list[0].selfFollow; element.authorHeadImg = baseData.data.list[0].memberInfo?.avatar; element.authorName = baseData.data.list[0].memberInfo?.nickname; element.location = baseData.data.list[0].location; element.createTime = baseData.data.list[0].createTime; element.author = baseData.data.list[0].memberInfo?.mid; element.viewers = baseData.data.list[0]?.viewers; element.likes = baseData.data.list[0]?.likes; element.comments = baseData.data.list[0]?.comments; this.isRefresh = false; setState(() { }); } }); }else{ if (pageNum == 1) { articles.clear(); } baseData.data.list.forEach((element) { var article = Article(); article.id = element.id; article.content = jsonEncode(element.subjectInfo); article.mainTitle =element.subject; article.followed = element.selfFollow; article.authorHeadImg = element.memberInfo?.avatar; article.authorName = element.memberInfo?.nickname; article.location = element.location; article.createTime = element.createTime; article.author = element.memberInfo?.mid; article.viewers = element?.viewers; article.likes = element?.likes; article.comments = element?.comments; articles.add(article); }); // comments.sort((a,b)=>b.createTime.compareTo(a.createTime)); // print("comments: ${comments.length}"); if (int.tryParse(baseData.data.total) < (pageNum * 10)) { refreshController.loadNoData(); } } } isLoadingData = false; } @override Widget build(BuildContext context) { super.build(context); return FutureBuilder( future: queryCommunity(null), builder: (context, position) { return SmartRefresher( controller: refreshController, enablePullDown: true, enablePullUp: true, physics: BouncingScrollPhysics(), header: MyHeader(), footer: CustomFooter( builder: (context, mode) { return MyFooter(mode); }, ), onRefresh: onRefresh, onLoading: () { isLoadMore = true; setState(() {}); }, child: (articles == null || articles.length == 0)? NoDataView( src: "assets/image/guan_zhu.webp", isShowBtn: false, text: "目前暂无添加关注,可在推荐中关注自己喜欢的人哦~", fontSize: 16.sp, margin: EdgeInsets.only(top: 120.h,left: 60.w,right: 60.w), ):ListView.builder( controller: sc, itemBuilder: (context, position) { return InkWell( child: CommunityDynamic( articles[position], 0, userId: userId, isList: true, exitFull: () { // setState(() { // onRefresh(); // }); }, ), onTap: () { Navigator.of(context).pushNamed( '/router/community_details', arguments: { "businessId": articles[position].id, "userId": userId, }, ).then((value) { queryCommunity(articles[position].id); // onRefresh(); // setState(() {}); }); // setState(() {}); }, ); }, itemCount: articles.length, ), ); }, ); } @override bool get wantKeepAlive => true; }