feat: 评论点赞

This commit is contained in:
guozhigq
2023-07-24 20:01:10 +08:00
parent 7a3f6cf5ee
commit 1c744e3f59
5 changed files with 116 additions and 24 deletions

View File

@ -104,6 +104,9 @@ class Api {
// 楼中楼
static const String replyReplyList = '/x/v2/reply/reply';
// 评论点赞
static const String likeReply = '/x/v2/reply/action';
// 发表评论
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/comment/action.md
static const String replyAdd = '/x/v2/reply/add';

View File

@ -70,4 +70,32 @@ class ReplyHttp {
};
}
}
// 评论点赞
static Future likeReply({
required int type,
required int oid,
required int rpid,
required int action,
}) async {
var res = await Request().post(
Api.likeReply,
queryParameters: {
'type': type,
'oid': oid,
'rpid': rpid,
'action': action,
'csrf': await Request.getCsrf(),
},
);
if (res.data['code'] == 0) {
return {'status': true, 'data': res.data['data']};
} else {
return {
'status': false,
'date': [],
'msg': res.data['message'],
};
}
}
}

View File

@ -42,7 +42,6 @@ class DynamicDetailController extends GetxController {
sort: sortType.index,
);
if (res['status']) {
res['data'] = ReplyData.fromJson(res['data']);
acount.value = res['data'].page.acount;
if (res['data'].replies.isNotEmpty) {
currentPage = currentPage + 1;

View File

@ -9,6 +9,8 @@ import 'package:pilipala/pages/video/detail/controller.dart';
import 'package:pilipala/pages/video/detail/replyNew/index.dart';
import 'package:pilipala/utils/utils.dart';
import 'zan.dart';
class ReplyItem extends StatelessWidget {
ReplyItem({
super.key,
@ -282,29 +284,7 @@ class ReplyItem extends StatelessWidget {
},
),
),
SizedBox(
height: 32,
child: TextButton(
child: Row(
children: [
Icon(
FontAwesomeIcons.thumbsUp,
size: 16,
color: color,
),
const SizedBox(width: 4),
Text(
replyItem!.like.toString(),
style: TextStyle(
color: color,
fontSize:
Theme.of(context).textTheme.labelSmall!.fontSize),
),
],
),
onPressed: () {},
),
),
ZanButton(replyItem: replyItem, replyType: replyType),
const SizedBox(width: 5)
],
);

View File

@ -0,0 +1,82 @@
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:pilipala/http/reply.dart';
import 'package:pilipala/models/common/reply_type.dart';
import 'package:pilipala/models/video/reply/item.dart';
class ZanButton extends StatefulWidget {
ZanButton({
super.key,
this.replyItem,
this.replyType,
});
ReplyItemModel? replyItem;
final ReplyType? replyType;
@override
State<ZanButton> createState() => _ZanButtonState();
}
class _ZanButtonState extends State<ZanButton> {
// 评论点赞
onLikeReply() async {
ReplyItemModel replyItem = widget.replyItem!;
int oid = replyItem.oid!;
int rpid = replyItem.rpid!;
// 1 已点赞 2 不喜欢 0 未操作
int action = replyItem.action == 0 ? 1 : 0;
var res = await ReplyHttp.likeReply(
type: widget.replyType!.index, oid: oid, rpid: rpid, action: action);
if (res['status']) {
SmartDialog.showToast(replyItem.action == 0 ? '点赞成功' : '取消赞');
if (action == 1) {
replyItem.like = replyItem.like! + 1;
replyItem.action = 1;
} else {
replyItem.like = replyItem.like! - 1;
replyItem.action = 0;
}
setState(() {});
} else {
SmartDialog.showToast(res['msg']);
}
}
@override
Widget build(BuildContext context) {
var color = Theme.of(context).colorScheme.outline;
var primary = Theme.of(context).colorScheme.primary;
return SizedBox(
height: 32,
child: TextButton(
child: Row(
children: [
Icon(
widget.replyItem!.action == 1
? FontAwesomeIcons.solidThumbsUp
: FontAwesomeIcons.thumbsUp,
size: 16,
color: widget.replyItem!.action == 1 ? primary : color,
),
const SizedBox(width: 4),
AnimatedSwitcher(
duration: const Duration(milliseconds: 400),
transitionBuilder: (Widget child, Animation<double> animation) {
return ScaleTransition(scale: animation, child: child);
},
child: Text(widget.replyItem!.like.toString(),
key: ValueKey<int>(widget.replyItem!.like!),
style: TextStyle(
color: widget.replyItem!.action == 1 ? primary : color,
fontSize:
Theme.of(context).textTheme.labelSmall!.fontSize)),
),
],
),
onPressed: () => onLikeReply(),
),
);
}
}