推荐视频评论和点赞关注

This commit is contained in:
cuiyouliang 2025-08-25 18:07:40 +08:00
parent 212fdea5a0
commit 26b8b5abf3
2 changed files with 375 additions and 258 deletions

View File

@ -195,7 +195,14 @@ class MyPageState extends State<Vloger> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
return PopScope(
canPop: true,
onPopInvokedWithResult: (bool didPop, Object? result) {
if (didPop) {
print('User navigated back');
}
},
child:Scaffold(
backgroundColor: const Color(0xFFFAF6F9),
body: Obx(() {
return NestedScrollViewPlus(
@ -211,6 +218,16 @@ class MyPageState extends State<Vloger> with SingleTickerProviderStateMixin {
collapsedHeight: 120.0,
pinned: true,
stretch: true,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Get.back(result: {
'returnTo': '/',
'vlogerId': args['memberId'],
'followStatus': (followed.value == 1 || followed.value == 3)?true:false,
});
},
),
onStretchTrigger: () async {
logger.i('触发 stretch 拉伸');
//
@ -283,6 +300,7 @@ class MyPageState extends State<Vloger> with SingleTickerProviderStateMixin {
),
);
}),
),
);
}

View File

@ -10,6 +10,7 @@ import 'package:get/get.dart';
import 'package:loopin/IM/controller/chat_controller.dart';
import 'package:loopin/IM/im_core.dart';
import 'package:loopin/IM/im_message.dart';
import 'package:loopin/IM/im_service.dart' hide logger;
import 'package:loopin/api/video_api.dart';
import 'package:loopin/components/my_toast.dart';
import 'package:loopin/components/network_or_asset_image.dart';
@ -46,9 +47,14 @@ class RecommendModule extends StatefulWidget {
State<RecommendModule> createState() => _RecommendModuleState();
}
class CommentBottomSheet extends StatefulWidget {
final int videoId;
final String videoId;
final Function(int) onCommentCountChanged; //
const CommentBottomSheet({super.key, required this.videoId});
const CommentBottomSheet({
super.key,
required this.videoId,
required this.onCommentCountChanged, //
});
@override
_CommentBottomSheetState createState() => _CommentBottomSheetState();
@ -61,9 +67,15 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
bool isLoadingMoreComments = false;
bool hasMoreComments = true;
List<Map<String, dynamic>> commentList = [];
int replyingCommentId = 0;
String replyingCommentId = '';
String replyingCommentUser = '';
//
Map<String, dynamic> expandedReplies = {}; // {commentId: true/false}
Map<String, List<Map<String, dynamic>>> replyData = {}; // {commentId: [replies]}
Map<String, int> replyPage = {}; //
Map<String, bool> isLoadingReplies = {}; //
@override
void initState() {
super.initState();
@ -83,7 +95,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
hasMoreComments = true;
commentList.clear();
}
logger.d('入参vlogId-------------------->: ${widget.videoId}');
try {
final res = await Http.post(VideoApi.videoCommentList, data: {
'vlogId': widget.videoId,
@ -91,7 +103,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
'size': commentPageSize,
});
logger.d('评论接口返回: ${json.encode(res)}');
logger.d('评论接口列表返回: ${json.encode(res)}');
if (res['code'] == 200 && res['data'] != null) {
final data = res['data'];
@ -108,7 +120,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
hasMoreComments = commentList.length < total;
commentPage++;
});
widget.onCommentCountChanged(total);
logger.d('成功加载 ${newComments.length} 条评论,总共 $total');
}
} catch (e) {
@ -120,17 +132,33 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
}
}
Future<void> postComment(String content, {int parentCommentId = 0}) async {
Future<void> postComment(String content, {String parentCommentId = ''}) async {
try {
final res = await Http.post(VideoApi.doVideoComment, data: {
'vlogId': widget.videoId,
'content': content,
'fatherCommentId': parentCommentId,
'fatherCommentId': parentCommentId.isNotEmpty ? parentCommentId : null,
});
if (res['code'] == 0) {
//
if (res['code'] == 200) {
//
if (parentCommentId.isNotEmpty) {
fetchReplies(parentCommentId, false);
//
setState(() {
final comment = commentList.firstWhere(
(c) => c['id'] == parentCommentId,
orElse: () => <String, dynamic>{}
);
if (comment != null && comment.isNotEmpty) {
comment['childCount'] = (comment['childCount'] ?? 0) + 1;
}
});
} else {
//
fetchComments(false);
}
widget.onCommentCountChanged(commentList.length + 1);
MyToast().tip(
title: '评论成功',
position: 'center',
@ -147,6 +175,49 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
}
}
//
Future<void> fetchReplies(String commentId, bool loadMore) async {
if (isLoadingReplies[commentId] == true && !loadMore) return;
setState(() {
isLoadingReplies[commentId] = true;
});
if (!loadMore) {
replyPage[commentId] = 1;
replyData[commentId] = [];
}
try {
final res = await Http.post(VideoApi.videoCommentList, data: {
'fatherCommentId': commentId,
'current': replyPage[commentId],
'size': commentPageSize,
});
if (res['code'] == 200 && res['data'] != null) {
final data = res['data'];
final List<Map<String, dynamic>> newReplies =
List<Map<String, dynamic>>.from(data['records'] ?? []);
setState(() {
if (loadMore) {
replyData[commentId]!.addAll(newReplies);
} else {
replyData[commentId] = newReplies;
}
replyPage[commentId] = replyPage[commentId]! + 1;
});
}
} catch (e) {
logger.e('获取子评论异常: $e');
} finally {
setState(() {
isLoadingReplies[commentId] = false;
});
}
}
@override
Widget build(BuildContext context) {
return Material(
@ -227,7 +298,14 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
}
final comment = commentList[index];
return ListTile(
final hasReplies = comment['childCount'] > 0;
final isExpanded = expandedReplies[comment['id']] == true;
final replies = replyData[comment['id']] ?? [];
return Column(
children: [
//
ListTile(
isThreeLine: true,
leading: ClipRRect(
borderRadius: BorderRadius.circular(50.0),
@ -277,12 +355,15 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
),
),
),
if (comment['childCount'] > 0)
if (hasReplies)
GestureDetector(
onTap: () {
setState(() {
replyingCommentId = comment['id'];
replyingCommentUser = comment['commentUserNickname'] ?? '未知用户';
expandedReplies[comment['id']] = !isExpanded;
if (expandedReplies[comment['id']] == true &&
(replyData[comment['id']] == null || replyData[comment['id']]!.isEmpty)) {
fetchReplies(comment['id'], false);
}
});
},
child: Container(
@ -298,8 +379,8 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
style: TextStyle(fontSize: 12.0),
),
Icon(
Icons.arrow_forward_ios,
size: 10.0,
isExpanded ? Icons.arrow_drop_up : Icons.arrow_drop_down,
size: 16.0,
)
]),
),
@ -310,26 +391,107 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
),
],
),
),
//
if (isExpanded && replies.isNotEmpty)
Padding(
padding: EdgeInsets.only(left: 40.0),
child: Column(
children: [
...replies.map((reply) => ListTile(
leading: ClipRRect(
borderRadius: BorderRadius.circular(25.0),
child: NetworkOrAssetImage(
imageUrl: reply['commentUserFace'] ?? 'assets/images/avatar/default.png',
width: 25.0,
height: 25.0,
fit: BoxFit.cover,
),
),
title: Row(
children: [
Expanded(
child: Text(
reply['commentUserNickname'] ?? '未知用户',
style: TextStyle(
color: Colors.grey,
fontSize: 11.0,
),
),
),
GestureDetector(
onTap: () {
setState(() {
replyingCommentId = comment['id'];
replyingCommentUser = reply['commentUserNickname'] ?? '未知用户';
});
},
child: Icon(
Icons.reply,
color: Colors.black54,
size: 14.0,
),
),
],
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 3.0),
child: Text(
reply['content'] ?? '',
style: TextStyle(fontSize: 13.0),
),
),
Text(
'${reply['createTime']?.toString().substring(0, 10) ?? ''}',
style: TextStyle(color: Colors.grey, fontSize: 11.0),
),
],
),
)).toList(),
//
if (replies.length < comment['childCount'])
Center(
child: TextButton(
onPressed: () => fetchReplies(comment['id'], true),
child: isLoadingReplies[comment['id']] == true
? CircularProgressIndicator()
: Text('加载更多回复'),
),
),
],
),
),
Divider(height: 1, color: Colors.grey[200]),
],
);
},
),
),
),
if (replyingCommentId > 0)
//
if (replyingCommentId.isNotEmpty)
Container(
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
color: Colors.grey[100],
child: Row(
children: [
Text(
Expanded(
child: Text(
'回复 @$replyingCommentUser',
style: TextStyle(fontSize: 12.0, color: Colors.blue),
overflow: TextOverflow.ellipsis,
),
),
Spacer(),
GestureDetector(
onTap: () {
setState(() {
replyingCommentId = 0;
replyingCommentId = '';
replyingCommentUser = '';
});
},
@ -356,7 +518,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
),
SizedBox(width: 5.0),
Text(
replyingCommentId > 0 ? '回复 @$replyingCommentUser' : '说点什么...',
replyingCommentId.isNotEmpty ? '回复 @$replyingCommentUser' : '说点什么...',
style: TextStyle(color: Colors.black54, fontSize: 14.0),
),
],
@ -364,7 +526,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
),
onTap: () {
Navigator.push(context, FadeRoute(child: PopupReply(
hintText: replyingCommentId > 0 ? '回复 @$replyingCommentUser' : '说点什么...',
hintText: replyingCommentId.isNotEmpty ? '回复 @$replyingCommentUser' : '说点什么...',
onChanged: (value) {
debugPrint('评论内容: $value');
},
@ -372,7 +534,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
if (value.isNotEmpty) {
postComment(value, parentCommentId: replyingCommentId);
setState(() {
replyingCommentId = 0;
replyingCommentId = '';
replyingCommentUser = '';
});
Navigator.pop(context);
@ -389,7 +551,7 @@ class _CommentBottomSheetState extends State<CommentBottomSheet> {
class _RecommendModuleState extends State<RecommendModule> {
VideoModuleController videoModuleController = Get.put(VideoModuleController());
final ChatController chatController = Get.find<ChatController>();
final RxMap<String, dynamic> lastReturnData = RxMap({});
//
int page = 1;
final int pageSize = 10;
@ -421,21 +583,15 @@ class _RecommendModuleState extends State<RecommendModule> {
bool hasMoreComments = true;
List<Map<String, dynamic>> commentList = [];
int currentVideoId = 0;
int replyingCommentId = 0; // ID
String replyingCommentId = ''; // ID
String replyingCommentUser = ''; //
//
List shareList = [
{'icon': 'assets/images/share-wx.png', 'label': '好友'},
{'icon': 'assets/images/share-wx.png', 'label': '微信'},
{'icon': 'assets/images/share-pyq.png', 'label': '朋友圈'},
{'icon': 'assets/images/share-link.png', 'label': '复制链接'},
{'icon': 'assets/images/share-download.png', 'label': '下载'},
{'icon': 'assets/images/share-download.png', 'label': '下载'},
{'icon': 'assets/images/share-download.png', 'label': '下载'},
{'icon': 'assets/images/share-download.png', 'label': '下载'},
{'icon': 'assets/images/share-download.png', 'label': '下载下载下载下载下载下载下载下载下载下载下载下载'},
{'icon': 'assets/images/share-download.png', 'label': '下载下载下载下载下载下载下载下载下载下载下载下载'},
];
@override
@ -545,7 +701,7 @@ class _RecommendModuleState extends State<RecommendModule> {
if (videos.isNotEmpty) {
page++;
}
logger.i('获取新的视频数据了');
logger.i('视频数据列表------------------->$videoList');
//
player.open(
@ -569,91 +725,6 @@ class _RecommendModuleState extends State<RecommendModule> {
}
}
// fetchComments
Future<void> fetchComments(int videoId, {bool loadMore = false}) async {
//
if (isLoadingMoreComments && !loadMore) return;
setState(() {
isLoadingMoreComments = true;
});
try {
final res = await Http.post(VideoApi.videoCommentList, data: {
'vlogId': videoId,
'current': loadMore ? commentPage : 1,
'size': commentPageSize,
});
logger.d('评论接口返回: ${json.encode(res)}');
if (res['code'] == 200 && res['data'] != null) {
final data = res['data'];
final List<Map<String, dynamic>> newComments =
List<Map<String, dynamic>>.from(data['records'] ?? []);
final int total = data['total'] ?? 0;
setState(() {
if (loadMore) {
commentList.addAll(newComments);
} else {
commentList = newComments;
}
hasMoreComments = commentList.length < total;
commentPage = loadMore ? commentPage + 1 : 2; //
});
logger.d('成功加载 ${newComments.length} 条评论,总共 $total');
} else {
logger.e('获取评论失败: ${res['msg']}');
MyToast().tip(
title: '获取评论失败',
position: 'center',
type: 'error',
);
}
} catch (e) {
logger.e('获取评论异常: $e');
MyToast().tip(
title: '网络异常',
position: 'center',
type: 'error',
);
} finally {
setState(() {
isLoadingMoreComments = false;
});
}
}
//
Future<void> postComment(String content, {int parentCommentId = 0}) async {
try {
final res = await Http.post(VideoApi.doVideoComment, data: {
'vlogId': currentVideoId,
'content': content,
'fatherCommentId': parentCommentId,
});
if (res['code'] == 0) {
//
fetchComments(currentVideoId, loadMore: false);
MyToast().tip(
title: '评论成功',
position: 'center',
type: 'success',
);
}
} catch (e) {
logger.i('发布评论失败: $e');
MyToast().tip(
title: '评论失败',
position: 'center',
type: 'error',
);
}
}
//
Future<void> doUnLikeVideo(item) async {
logger.d('点击了点赞按钮$item');
@ -688,8 +759,7 @@ Future<void> fetchComments(int videoId, {bool loadMore = false}) async {
//
void handleComment(index) {
final videoIdStr = videoList[index]['id'].toString();
final videoId = int.tryParse(videoIdStr) ?? 0;
final videoId = videoList[index]['id'];
logger.i('点击了评论按钮$videoId');
showModalBottomSheet(
@ -703,7 +773,17 @@ void handleComment(index) {
),
context: context,
builder: (context) {
return CommentBottomSheet(videoId: videoId);
return CommentBottomSheet(
videoId: videoId,
onCommentCountChanged: (newCount) {
//
setState(() {
if (index < videoList.length) {
videoList[index]['commentsCounts'] = newCount;
}
});
}
);
},
);
}
@ -974,9 +1054,20 @@ void handleComment(index) {
height: 55.0,
width: 48.0,
child: GestureDetector(
onTap: () {
onTap: ()async {
player.pause();
Get.toNamed('/vloger', arguments: videoList[videoModuleController.videoPlayIndex.value]);
// Vloger
final result = await Get.toNamed(
'/vloger',
arguments: videoList[videoModuleController
.videoPlayIndex.value]);
if (result != null) {
//
print('返回的数据: ${result['followStatus']}');
player.play();
videoList[index]['doIFollowVloger'] = result['followStatus'];
};
},
child: UnconstrainedBox(
alignment: Alignment.topCenter,
@ -1013,10 +1104,18 @@ void handleComment(index) {
size: 14.0,
),
),
onTap: () {
onTap: () async {
final vlogerId = videoList[index]['memberId'];
final doIFollowVloger = videoList[index]['doIFollowVloger'];
//
if(doIFollowVloger == false){
final res = await ImService.instance.followUser(userIDList: [vlogerId]);
if (res.success) {
setState(() {
videoList[index]['doIFollowVloger'] = !videoList[index]['doIFollowVloger'];
});
}
}
},
),
),