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.

315 lines
9.4 KiB

3 years ago
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/community/community_view/community_dynamic.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
3 years ago
import 'package:huixiang/retrofit/data/article.dart';
3 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/member_comment_list.dart';
3 years ago
import 'package:huixiang/retrofit/data/page.dart';
3 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
3 years ago
import 'package:huixiang/view_widget/my_appbar.dart';
3 years ago
import 'package:huixiang/view_widget/tips_dialog.dart';
3 years ago
import 'package:huixiang/web/web_view/comment_list.dart';
3 years ago
import 'package:huixiang/web/web_view/input_comment.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CommunityDetails extends StatefulWidget {
3 years ago
final Map<String, dynamic> arguments;
CommunityDetails({this.arguments});
3 years ago
@override
State<StatefulWidget> createState() {
return _CommunityDetails();
}
}
3 years ago
class _CommunityDetails extends State<CommunityDetails> with WidgetsBindingObserver {
3 years ago
double height = 0;
double commentHeight = 60.h;
// ComunityComment comunity;
3 years ago
String parenId = "0";
final GlobalKey commentKey = GlobalKey();
final ScrollController scrollController = ScrollController();
3 years ago
List<MemberCommentList> memberList = [];
ApiService apiService;
var commentFocus = FocusNode();
String hintText = S.current.liuxianinjingcaidepinglunba;
bool isKeyBoardShow = false;
3 years ago
int commentTotal = 0;
3 years ago
Article article;
String businessId;
3 years ago
3 years ago
@override
void didChangeMetrics() {
WidgetsBinding.instance.addPostFrameCallback((_) {
3 years ago
if (!mounted) return;
3 years ago
if (MediaQuery
.of(context)
.viewInsets
.bottom == 0) {
3 years ago
if (isKeyBoardShow) {
FocusScope.of(context).requestFocus(FocusNode());
3 years ago
if (mounted)
setState(() {
hintText = S.current.liuxianinjingcaidepinglunba;
isKeyBoardShow = false;
});
3 years ago
}
} else {
3 years ago
if (mounted)
setState(() {
isKeyBoardShow = true;
});
3 years ago
}
});
}
@override
void initState() {
super.initState();
// comunity = widget.arguments["comment"];
businessId = widget.arguments["businessId"];
3 years ago
WidgetsBinding.instance.addObserver(this);
3 years ago
3 years ago
_queryMemberCommentList();
queryDetails(businessId);
3 years ago
}
///详情接口
queryDetails(id) async {
3 years ago
SharedPreferences value = await SharedPreferences.getInstance();
if (apiService == null)
3 years ago
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
BaseData<Article> baseData = await apiService.informationInfo(id)
3 years ago
.catchError((onError) {
3 years ago
debugPrint(onError.toString());
3 years ago
});
if (baseData != null && baseData.isSuccess) {
setState(() {
article = baseData.data;
});
}
3 years ago
}
@override
Widget build(BuildContext context) {
3 years ago
return Scaffold(
3 years ago
appBar: MyAppBar(
title: "动态详情",
titleColor: Colors.black,
titleSize: 18.sp,
background: Colors.white,
leading: true,
leadingColor: Colors.black,
),
body: Container(
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
3 years ago
physics: BouncingScrollPhysics(),
3 years ago
child: Column(
children: [
if(article != null)
3 years ago
CommunityDynamic(
article,
3 years ago
0,
exitFull: () {
setState(() {});
},
userId:widget.arguments !=null ? widget.arguments["userId"] :widget.arguments["mid"],
3 years ago
itemCount: 3,
isDetails: true,
heightFun: (height) {
this.height = height +
3 years ago
MediaQuery
.of(context)
.padding
.top +
3 years ago
kToolbarHeight +
24;
if (mounted) setState(() {});
3 years ago
},
),
3 years ago
CommentList(
commentKey,
article?.likes ?? 0,
businessId,
4,
3 years ago
isKeyBoardShow,
_reply,
_delCommentTips,
12.sp,
3 years ago
requestApiFinish: (total){setState(() {
commentTotal = total;
});},
3 years ago
),
3 years ago
if (memberList == null || memberList.length == 0)
Container(
width: double.infinity,
alignment: Alignment.topCenter,
margin: EdgeInsets.only(top: 40),
padding: EdgeInsets.all(22.h),
child: Text(
S.of(context)
3 years ago
.zanwupinglun,
3 years ago
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Color(0xFFA0A0A0),
3 years ago
),
),
),
3 years ago
],
),
3 years ago
),
3 years ago
flex: 1,
3 years ago
),
3 years ago
/// 富文本评论的输入框
InputComment(
inputKey,
hintText,
isKeyBoardShow,
commentFocus,
commentTextController,
_toComment,
_queryMemberComment,
_queryInformationLikes,
isLike: article?.liked ?? false,
3 years ago
),
],
3 years ago
),
3 years ago
),
);
3 years ago
}
3 years ago
///给文章/活动点赞
3 years ago
_queryInformationLikes() async {
BaseData baseData = await apiService.informationLikes(businessId).catchError((onError) {});
3 years ago
if (baseData != null && baseData.isSuccess) {
3 years ago
commentKey.currentState.setState(() {});
3 years ago
setState(() {
if (article?.liked ?? false)
article?.likes -= 1;
3 years ago
else
article?.likes += 1;
article?.liked = !(article.liked ?? false);
3 years ago
});
3 years ago
} else {
3 years ago
// SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
3 years ago
}
}
///动态发布评论
3 years ago
_queryMemberComment(String content) async {
3 years ago
BaseData baseData = await apiService.memberComment({
"content": content,
"parentId": parenId,
"relationalId": businessId,
"relationalType":4
3 years ago
}).catchError((error) {});
if (baseData != null && baseData.isSuccess) {
3 years ago
CommentListState state = commentKey.currentState;
state.queryMemberCommentList();
3 years ago
commentTextController.text = "";
3 years ago
FocusScope.of(context).unfocus();
_queryMemberCommentList();
3 years ago
}
3 years ago
}
///滑动到评论列表
_toComment() {
3 years ago
if (commentKey.currentContext == null) return;
RenderBox firstRenderBox = commentKey.currentContext.findRenderObject();
Offset first = firstRenderBox.localToGlobal(Offset.zero);
scrollController.animateTo(
3 years ago
first.dy +
scrollController.offset -
3 years ago
(kToolbarHeight + MediaQuery
.of(context)
.padding
.top),
3 years ago
duration: Duration(milliseconds: 300),
curve: Curves.easeIn,
);
3 years ago
}
final GlobalKey inputKey = GlobalKey();
final TextEditingController commentTextController = TextEditingController();
contentHeight() {
3 years ago
double contentHeight = MediaQuery
.of(context)
.size
.height -
3 years ago
kToolbarHeight -
3 years ago
MediaQuery
.of(context)
.padding
.top -
3 years ago
160.h;
if ((contentHeight - 60.h) > (128.h * memberList.length)) {
commentHeight = contentHeight - (128.h * memberList.length);
}
}
///删除评论的提示
3 years ago
_delCommentTips(memberComment) {
3 years ago
SmartDialog.show(widget: Tips(() {
3 years ago
delComment(memberComment);
3 years ago
}));
}
///删除评论
3 years ago
delComment(memberComment) async {
BaseData baseData = await apiService.delComment(memberComment.id);
3 years ago
if (baseData != null && baseData.isSuccess) {
3 years ago
CommentListState state = commentKey.currentState;
state.queryMemberCommentList();
3 years ago
}
}
///评论 回复
_reply(memberComment) {
FocusScope.of(context).requestFocus(commentFocus);
3 years ago
parenId = memberComment.id;
3 years ago
hintText = S.of(context).huifu_("${memberComment.username}");
}
3 years ago
///评论列表
_queryMemberCommentList() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
if (apiService == null)
apiService = ApiService(
Dio(),
context: context,
token: sharedPreferences.getString("token"),
showLoading: false,
);
BaseData<PageInfo<MemberCommentList>> baseData =
3 years ago
await apiService.memberCommentList({
3 years ago
"pageNum": 1,
"pageSize": 100,
"relationalId": businessId,
"relationalType":4,
3 years ago
}).catchError((error) {});
if (baseData != null && baseData.isSuccess) {
commentTotal = baseData.data.size;
memberList = baseData.data.list;
contentHeight();
3 years ago
if (mounted) setState(() {});
3 years ago
}
}
3 years ago
}