From 1c744e3f592b42fd2d74653c2dbc973273dd82ba Mon Sep 17 00:00:00 2001 From: guozhigq Date: Mon, 24 Jul 2023 20:01:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=84=E8=AE=BA=E7=82=B9=E8=B5=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/http/api.dart | 3 + lib/http/reply.dart | 28 +++++++ lib/pages/dynamics/deatil/controller.dart | 1 - .../detail/reply/widgets/reply_item.dart | 26 +----- lib/pages/video/detail/reply/widgets/zan.dart | 82 +++++++++++++++++++ 5 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 lib/pages/video/detail/reply/widgets/zan.dart diff --git a/lib/http/api.dart b/lib/http/api.dart index 06c9fd9b..65b70e11 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -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'; diff --git a/lib/http/reply.dart b/lib/http/reply.dart index e69e6599..9c40a357 100644 --- a/lib/http/reply.dart +++ b/lib/http/reply.dart @@ -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'], + }; + } + } } diff --git a/lib/pages/dynamics/deatil/controller.dart b/lib/pages/dynamics/deatil/controller.dart index 22022ce2..773bf1b2 100644 --- a/lib/pages/dynamics/deatil/controller.dart +++ b/lib/pages/dynamics/deatil/controller.dart @@ -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; diff --git a/lib/pages/video/detail/reply/widgets/reply_item.dart b/lib/pages/video/detail/reply/widgets/reply_item.dart index 656eab23..f15a1eee 100644 --- a/lib/pages/video/detail/reply/widgets/reply_item.dart +++ b/lib/pages/video/detail/reply/widgets/reply_item.dart @@ -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) ], ); diff --git a/lib/pages/video/detail/reply/widgets/zan.dart b/lib/pages/video/detail/reply/widgets/zan.dart new file mode 100644 index 00000000..ee125abc --- /dev/null +++ b/lib/pages/video/detail/reply/widgets/zan.dart @@ -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 createState() => _ZanButtonState(); +} + +class _ZanButtonState extends State { + // 评论点赞 + 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 animation) { + return ScaleTransition(scale: animation, child: child); + }, + child: Text(widget.replyItem!.like.toString(), + key: ValueKey(widget.replyItem!.like!), + style: TextStyle( + color: widget.replyItem!.action == 1 ? primary : color, + fontSize: + Theme.of(context).textTheme.labelSmall!.fontSize)), + ), + ], + ), + onPressed: () => onLikeReply(), + ), + ); + } +}