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.

162 lines
4.7 KiB

import 'dart:convert';
3 years ago
import 'package:dio/dio.dart';
3 years ago
import 'package:flutter/material.dart';
3 years ago
import 'package:huixiang/community/community_view/community_dynamic.dart';
import 'package:huixiang/retrofit/data/article.dart';
3 years ago
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';
3 years ago
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';
3 years ago
import 'package:shared_preferences/shared_preferences.dart';
3 years ago
class CommunityChildPage extends StatefulWidget {
3 years ago
final String typeStr;
CommunityChildPage(this.typeStr);
3 years ago
@override
State<StatefulWidget> createState() {
return _CommunityChildPage();
}
}
3 years ago
class _CommunityChildPage extends State<CommunityChildPage> with AutomaticKeepAliveClientMixin {
3 years ago
RefreshController refreshController = RefreshController();
3 years ago
ApiService apiService;
int pageNum = 1;
3 years ago
String userId;
bool isLoadMore = false;
3 years ago
List<Article> articles = [];
3 years ago
3 years ago
@override
3 years ago
void initState() {
super.initState();
3 years ago
_onRefresh();
3 years ago
}
3 years ago
_onRefresh() async {
3 years ago
setState(() {});
3 years ago
}
3 years ago
///动态列表
3 years ago
queryCommunity() async {
if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
3 years ago
userId = value.getString('userId');
3 years ago
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
}
if(isLoadMore){
pageNum += 1;
isLoadMore = false;
}
else pageNum = 1;
3 years ago
BaseData<PageInfo<ComunityComment>> baseData = await apiService.trendList({
3 years ago
"onlyFollow": widget.typeStr == "关注" ? true : false,
3 years ago
"onlyMe": false,
"pageNum": pageNum,
"pageSize": 10,
"searchKey": ""
}).catchError((error) {
refreshController.refreshFailed();
refreshController.loadFailed();
3 years ago
});
3 years ago
refreshController.refreshCompleted();
refreshController.loadComplete();
if (baseData.isSuccess) {
if (pageNum == 1) {
articles.clear();
3 years ago
}
baseData.data.list.forEach((element) {
var article = Article();
article.id = element.id;
article.content = jsonEncode(element.subjectInfo);
article.mainTitle =element.subject;
article.liked = element.selfFollow;
article.authorHeadImg = element.memberInfo?.avatar;
article.authorName = element.memberInfo?.nickname;
article.createTime = element.createTime;
article.updateUser = element.memberInfo?.mid;
article.viewers = element?.viewers;
article.likes = element?.likes;
article.comments = element?.comments;
articles.add(article);
});
3 years ago
// comments.sort((a,b)=>b.createTime.compareTo(a.createTime));
// print("comments: ${comments.length}");
3 years ago
if (int.tryParse(baseData.data.total) < (pageNum * 10)) {
refreshController.loadNoData();
}
}
3 years ago
}
3 years ago
3 years ago
@override
Widget build(BuildContext context) {
3 years ago
super.build(context);
3 years ago
return FutureBuilder(
3 years ago
future: queryCommunity(),
3 years ago
builder: (context, position) {
return SmartRefresher(
3 years ago
controller: refreshController,
enablePullDown: true,
enablePullUp: true,
physics: BouncingScrollPhysics(),
header: MyHeader(),
footer: CustomFooter(
builder: (context, mode) {
return MyFooter(mode);
3 years ago
},
3 years ago
),
onRefresh: _onRefresh,
onLoading: () {
isLoadMore = true;
3 years ago
setState(() {});
},
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, position) {
return InkWell(
child: CommunityDynamic(
articles[position],
3 years ago
0,
3 years ago
userId: userId,
3 years ago
isList: true,
3 years ago
exitFull: () {
setState(() {
_onRefresh();
});
},
3 years ago
),
onTap: () {
Navigator.of(context).pushNamed(
'/router/community_details',
arguments: {
"businessId": articles[position].id,
3 years ago
"userId": userId,
3 years ago
},
).then((value) {
_onRefresh();
});
3 years ago
},
);
},
itemCount: articles.length,
3 years ago
),
);
3 years ago
},
3 years ago
);
}
3 years ago
@override
bool get wantKeepAlive => true;
3 years ago
}