import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/community/community_view/community_dynamic.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:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; class CommunityChildPage extends StatefulWidget { final String typeStr; CommunityChildPage(this.typeStr); @override State createState() { return _CommunityChildPage(); } } class _CommunityChildPage extends State { RefreshController refreshController = RefreshController(); ApiService apiService; int pageNum = 0; String userId; List comments = []; @override void initState() { super.initState(); _onRefresh(); } _onRefresh() async { pageNum = 0; setState(() {}); } ///动态列表 queryCommunity() async { if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); userId = value.getString('userId'); apiService = ApiService( Dio(), context: context, token: value.getString("token"), ); } pageNum += 1; BaseData> baseData = await apiService.trendList({ "onlyFollow": widget.typeStr == "关注" ? true : false, "onlyMe": false, "pageNum": pageNum, "pageSize": 10, "searchKey": "" }).catchError((error) { refreshController.refreshFailed(); refreshController.loadFailed(); }); refreshController.refreshCompleted(); refreshController.loadComplete(); if (baseData.isSuccess) { if (pageNum == 1) { comments.clear(); } comments.addAll(baseData.data.list); // comments.sort((a,b)=>b.createTime.compareTo(a.createTime)); print("comments: ${comments.length}"); if (int.tryParse(baseData.data.total) < (pageNum * 10)) { refreshController.loadNoData(); } } } @override Widget build(BuildContext context) { return FutureBuilder( future: queryCommunity(), 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: () { setState(() {}); }, child: ListView.builder( physics: NeverScrollableScrollPhysics(), itemBuilder: (context, position) { return InkWell( child: CommunityDynamic( comments[position], 0, userId: userId, isList: true, exitFull: () { setState(() { _onRefresh(); }); }, ), onTap: () { Navigator.of(context).pushNamed( '/router/community_details', arguments: { "comment": comments[position], "userId": userId, }, ); }, ); }, itemCount: comments.length, ), ); }, ); } }