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.

431 lines
15 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/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/retrofit/data/activity.dart';
import 'package:huixiang/retrofit/data/article.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/member_comment_list.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
3 years ago
import 'package:huixiang/utils/font_weight.dart';
3 years ago
import 'package:huixiang/view_widget/comment_menu.dart';
import 'package:huixiang/view_widget/custom_image.dart';
import 'package:huixiang/view_widget/login_tips_dialog.dart';
import 'package:like_button/like_button.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CommentList extends StatefulWidget {
final Map arguments;
final Activity activity;
final Article article;
final bool isKeyBoardShow;
final Function reply;
final Function delCommentTips;
3 years ago
final double fontSize;
3 years ago
CommentList(
Key key,
this.arguments,
this.activity,
this.article,
this.isKeyBoardShow,
this.reply,
this.delCommentTips,
3 years ago
this.fontSize
3 years ago
) : super(key: key);
@override
State<StatefulWidget> createState() {
return _CommentList();
}
}
class _CommentList extends State<CommentList> {
int commentTotal = 0;
List<MemberCommentList> memberList = [];
ApiService apiService;
//评论点赞
queryCommentLike(String id) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString("token");
if (token == null || token == "") {
LoginTipsDialog().show(context);
return;
}
3 years ago
BaseData baseData = await apiService.commentLike(id).catchError((error) {});
3 years ago
if (baseData != null && baseData.isSuccess) {
memberList.forEach((element) {
if (element.id == id) {
if (element.liked) {
element.likes -= 1;
element.liked = false;
} else {
element.likes += 1;
element.liked = true;
}
}
});
}
}
@override
void initState() {
3 years ago
super.initState();
3 years ago
queryMemberCommentList();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 2),
blurRadius: 14,
spreadRadius: 0,
),
],
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.all(16),
child: Row(
children: [
Text(
S.of(context).pinglun_(commentTotal.toString()),
style: TextStyle(
3 years ago
fontSize:widget.fontSize+4,
fontWeight: FontWeight.bold,
3 years ago
color: Color(0xff1A1A1A),
),
),
SizedBox(
width: 16.w,
),
Text(
S.of(context).xihuan_(
3 years ago
"${widget.article?.likes ?? widget.activity?.likes ?? "0"}"),
3 years ago
style: TextStyle(
3 years ago
fontSize:widget.fontSize+4,
fontWeight: FontWeight.bold,
3 years ago
color: Color(0xff1A1A1A),
),
),
],
),
),
if (memberList != null && memberList.length > 0)
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: memberList != null ? memberList.length : 0,
scrollDirection: Axis.vertical,
itemBuilder: (context, position) {
return Material(
color: Colors.white,
child: InkWell(
onTap: () {
showPressMenu(memberList[position].createUser,
memberList[position]);
},
3 years ago
child: commentItem(
memberList[position], position, memberList.length),
3 years ago
),
);
},
),
if (memberList != null && memberList.length > 0)
Container(
3 years ago
height: commentHeight,
3 years ago
decoration: BoxDecoration(
color: Color(0xFFF2F2F2),
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(12),
offset: Offset(0, 2),
blurRadius: 14,
spreadRadius: 0,
),
],
),
3 years ago
alignment: Alignment.topCenter,
padding: EdgeInsets.only(top: 22.h),
3 years ago
child: Text(
S.of(context).yixiansquanbupinglun,
style: TextStyle(
3 years ago
fontSize:widget.fontSize+2,
3 years ago
color: Color(0xff353535),
),
),
),
if (memberList == null || memberList.length == 0)
Container(
width: double.infinity,
3 years ago
height: MediaQuery.of(context).size.height -
kToolbarHeight -
MediaQuery.of(context).padding.top -
160.h,
alignment: Alignment.topCenter,
color: Color(0xFFF2F2F2),
padding: EdgeInsets.only(top: 22.h),
3 years ago
child: Text(
S.of(context).zanwupinglun,
style: TextStyle(
3 years ago
fontSize:widget.fontSize+2,
fontWeight: FontWeight.bold,
3 years ago
color: Color(0xFFA0A0A0),
),
),
),
],
),
),
],
);
}
3 years ago
double commentHeight = 60.h;
contentHeight() {
double contentHeight = MediaQuery.of(context).size.height -
kToolbarHeight -
MediaQuery.of(context).padding.top -
160.h;
if ((contentHeight - 60.h) > (128.h * memberList.length)) {
commentHeight = contentHeight - (128.h * memberList.length);
}
}
3 years ago
//评论列表
queryMemberCommentList() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
if (apiService == null)
3 years ago
apiService = ApiService(
Dio(),
context: context,
token: sharedPreferences.getString("token"),
);
BaseData<PageInfo<MemberCommentList>> baseData = await apiService.memberCommentList({
3 years ago
"pageNum": 1,
"pageSize": 100,
3 years ago
"relationalId": widget.arguments["activityId"] ?? widget.arguments["articleId"],
3 years ago
"relationalType": 1,
}).catchError((error) {});
if (baseData != null && baseData.isSuccess) {
commentTotal = baseData.data.size;
memberList = baseData.data.list;
3 years ago
contentHeight();
3 years ago
setState(() {});
}
}
showPressMenu(String userId, memberComment) {
if (widget.isKeyBoardShow) {
FocusScope.of(context).requestFocus(FocusNode());
return;
}
SharedPreferences.getInstance().then((value) {
SmartDialog.show(
3 years ago
widget: CommentMenu(
(type) {
3 years ago
SmartDialog.dismiss();
if (type == "huifu") {
widget.reply(memberComment);
} else if (type == "shanchu") {
widget.delCommentTips();
}
},
isSelf: userId == value.getString("userId"),
),
alignmentTemp: Alignment.bottomCenter,
isUseAnimationTemp: true,
animationDurationTemp: Duration(milliseconds: 300),
);
});
}
Widget commentItem(MemberCommentList memberList, int index, int max) {
return Container(
padding: EdgeInsets.symmetric(vertical: 8.w),
child: Column(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
MImage(
memberList.userAvatarUrl ?? "",
fit: BoxFit.cover,
isCircle: true,
width: 40,
height: 40,
fadeSrc: "assets/image/default_user.png",
errorSrc: "assets/image/default_user.png",
),
SizedBox(
width: 12.w,
),
Expanded(
child: Container(
3 years ago
// height: 60.h,
3 years ago
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text.rich(
TextSpan(
children: [
TextSpan(
text: memberList.username,
style: TextStyle(
3 years ago
fontWeight: FontWeight.bold,
fontSize:widget.fontSize+2,
3 years ago
color: Colors.black,
),
),
],
),
textDirection: TextDirection.ltr,
),
3 years ago
SizedBox(
height: 3.h,
),
3 years ago
Text(
memberList.createTime,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
3 years ago
fontSize:widget.fontSize,
3 years ago
color: Color(0xff808080),
),
),
],
),
),
flex: 1,
),
3 years ago
Tooltip(
message: S.of(context).dianzanxihuan_(S.of(context).pinglun),
child: Container(
alignment: Alignment.topRight,
child: LikeButton(
padding: EdgeInsets.all(10),
circleSize: 16,
circleColor: CircleColor(
start: Color(0xff00ddff),
end: Color(0xff0099cc),
),
bubblesColor: BubblesColor(
dotPrimaryColor: Color(0xff33b5e5),
dotSecondaryColor: Color(0xff0099cc),
),
bubblesSize: 15,
likeBuilder: (bool isLiked) {
return isLiked
? Image.asset(
"assets/image/icon_like.png",
width: 16,
height: 16,
)
: Image.asset(
"assets/image/icon_like_h.png",
width: 16,
height: 16,
);
},
isLiked: memberList.liked ?? false,
onTap: (isLiked) async {
await queryCommentLike(memberList.id);
return (memberList == null || memberList.liked == null)
? false
: memberList.liked;
},
likeCount: memberList.likes,
countBuilder: (int count, bool isLiked, String text) {
return Text(
text,
style: TextStyle(
color: Color(0xFF1A1A1A),
3 years ago
fontSize:widget.fontSize,
3 years ago
),
);
},
3 years ago
),
),
),
],
),
),
Container(
padding: EdgeInsets.only(
3 years ago
left: 68.w,
right: 16.w,
3 years ago
top: 16.h,
bottom: 16.h,
),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
memberList.content,
style: TextStyle(
3 years ago
fontSize:widget.fontSize+2,
3 years ago
color: Color(0xff1A1A1A),
),
),
),
),
SizedBox(
height: 12.h,
),
if (memberList.parentContent != null)
Container(
margin: EdgeInsets.only(left: 68.w, right: 16.w),
decoration: BoxDecoration(
color: Color(0xffF2F2F2),
borderRadius: BorderRadius.circular(2.0),
),
child: Padding(
padding: EdgeInsets.only(left: 4.w, top: 4.h, bottom: 4.h),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${memberList.parentUserName}:" ?? "",
style: TextStyle(
3 years ago
fontSize: widget.fontSize,
3 years ago
color: Color(0xff808080),
),
),
Expanded(
flex: 1,
child: Text(
memberList.parentContent ?? "",
style: TextStyle(
3 years ago
fontSize:widget.fontSize,
3 years ago
color: Color(0xff808080),
),
),
),
],
),
),
),
],
),
);
}
}