mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-23 04:00:28 +08:00
common dyn page
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -8,7 +8,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
|
||||
class ZanButtonGrpc extends StatefulWidget {
|
||||
class ZanButtonGrpc extends StatelessWidget {
|
||||
const ZanButtonGrpc({
|
||||
super.key,
|
||||
required this.replyItem,
|
||||
@@ -16,23 +16,24 @@ class ZanButtonGrpc extends StatefulWidget {
|
||||
|
||||
final ReplyInfo replyItem;
|
||||
|
||||
@override
|
||||
State<ZanButtonGrpc> createState() => _ZanButtonGrpcState();
|
||||
}
|
||||
|
||||
class _ZanButtonGrpcState extends State<ZanButtonGrpc> {
|
||||
bool get isLike => widget.replyItem.replyControl.action == $fixnum.Int64.ONE;
|
||||
bool get isDislike =>
|
||||
widget.replyItem.replyControl.action == $fixnum.Int64.TWO;
|
||||
|
||||
Future<void> onHateReply() async {
|
||||
Future<void> onHateReply(
|
||||
BuildContext context,
|
||||
bool isProcessing,
|
||||
VoidCallback onDone, {
|
||||
required bool isLike,
|
||||
required bool isDislike,
|
||||
}) async {
|
||||
if (isProcessing) {
|
||||
return;
|
||||
}
|
||||
isProcessing = true;
|
||||
feedBack();
|
||||
final int oid = widget.replyItem.oid.toInt();
|
||||
final int rpid = widget.replyItem.id.toInt();
|
||||
final int oid = replyItem.oid.toInt();
|
||||
final int rpid = replyItem.id.toInt();
|
||||
// 1 已点赞 2 不喜欢 0 未操作
|
||||
final int action = isDislike ? 0 : 2;
|
||||
final res = await ReplyHttp.hateReply(
|
||||
type: widget.replyItem.type.toInt(),
|
||||
type: replyItem.type.toInt(),
|
||||
action: action == 2 ? 1 : 0,
|
||||
oid: oid,
|
||||
rpid: rpid,
|
||||
@@ -41,28 +42,39 @@ class _ZanButtonGrpcState extends State<ZanButtonGrpc> {
|
||||
if (res['status']) {
|
||||
SmartDialog.showToast(isDislike ? '取消踩' : '点踩成功');
|
||||
if (action == 2) {
|
||||
if (isLike) widget.replyItem.like -= $fixnum.Int64.ONE;
|
||||
widget.replyItem.replyControl.action = $fixnum.Int64.TWO;
|
||||
if (isLike) replyItem.like -= $fixnum.Int64.ONE;
|
||||
replyItem.replyControl.action = $fixnum.Int64.TWO;
|
||||
} else {
|
||||
widget.replyItem.replyControl.action = $fixnum.Int64.ZERO;
|
||||
replyItem.replyControl.action = $fixnum.Int64.ZERO;
|
||||
}
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
if (context.mounted) {
|
||||
(context as Element?)?.markNeedsBuild();
|
||||
}
|
||||
} else {
|
||||
SmartDialog.showToast(res['msg']);
|
||||
}
|
||||
onDone();
|
||||
}
|
||||
|
||||
// 评论点赞
|
||||
Future<void> onLikeReply() async {
|
||||
Future<void> onLikeReply(
|
||||
BuildContext context,
|
||||
bool isProcessing,
|
||||
VoidCallback onDone, {
|
||||
required bool isLike,
|
||||
required bool isDislike,
|
||||
}) async {
|
||||
if (isProcessing) {
|
||||
return;
|
||||
}
|
||||
isProcessing = true;
|
||||
feedBack();
|
||||
final int oid = widget.replyItem.oid.toInt();
|
||||
final int rpid = widget.replyItem.id.toInt();
|
||||
final int oid = replyItem.oid.toInt();
|
||||
final int rpid = replyItem.id.toInt();
|
||||
// 1 已点赞 2 不喜欢 0 未操作
|
||||
final int action = isLike ? 0 : 1;
|
||||
final res = await ReplyHttp.likeReply(
|
||||
type: widget.replyItem.type.toInt(),
|
||||
type: replyItem.type.toInt(),
|
||||
oid: oid,
|
||||
rpid: rpid,
|
||||
action: action,
|
||||
@@ -70,36 +82,32 @@ class _ZanButtonGrpcState extends State<ZanButtonGrpc> {
|
||||
if (res['status']) {
|
||||
SmartDialog.showToast(isLike ? '取消赞' : '点赞成功');
|
||||
if (action == 1) {
|
||||
widget.replyItem
|
||||
replyItem
|
||||
..like += $fixnum.Int64.ONE
|
||||
..replyControl.action = $fixnum.Int64.ONE;
|
||||
} else {
|
||||
widget.replyItem
|
||||
replyItem
|
||||
..like -= $fixnum.Int64.ONE
|
||||
..replyControl.action = $fixnum.Int64.ZERO;
|
||||
}
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
if (context.mounted) {
|
||||
(context as Element?)?.markNeedsBuild();
|
||||
}
|
||||
} else {
|
||||
SmartDialog.showToast(res['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
bool isProcessing = false;
|
||||
Future<void> handleState(Future Function() action) async {
|
||||
if (!isProcessing) {
|
||||
isProcessing = true;
|
||||
await action();
|
||||
isProcessing = false;
|
||||
}
|
||||
onDone();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final ThemeData theme = Theme.of(context);
|
||||
final Color color = theme.colorScheme.outline;
|
||||
final Color primary = theme.colorScheme.primary;
|
||||
late bool isProcessing = false;
|
||||
final action = replyItem.replyControl.action;
|
||||
final isLike = action == $fixnum.Int64.ONE;
|
||||
final isDislike = action == $fixnum.Int64.TWO;
|
||||
final outline = theme.colorScheme.outline;
|
||||
final primary = theme.colorScheme.primary;
|
||||
final ButtonStyle style = TextButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
@@ -112,13 +120,19 @@ class _ZanButtonGrpcState extends State<ZanButtonGrpc> {
|
||||
height: 32,
|
||||
child: TextButton(
|
||||
style: style,
|
||||
onPressed: () => handleState(onHateReply),
|
||||
onPressed: () => onHateReply(
|
||||
context,
|
||||
isProcessing,
|
||||
() => isProcessing = false,
|
||||
isLike: isLike,
|
||||
isDislike: isDislike,
|
||||
),
|
||||
child: Icon(
|
||||
isDislike
|
||||
? FontAwesomeIcons.solidThumbsDown
|
||||
: FontAwesomeIcons.thumbsDown,
|
||||
size: 16,
|
||||
color: isDislike ? primary : color,
|
||||
color: isDislike ? primary : outline,
|
||||
semanticLabel: isDislike ? '已踩' : '点踩',
|
||||
),
|
||||
),
|
||||
@@ -127,30 +141,29 @@ class _ZanButtonGrpcState extends State<ZanButtonGrpc> {
|
||||
height: 32,
|
||||
child: TextButton(
|
||||
style: style,
|
||||
onPressed: () => handleState(onLikeReply),
|
||||
onPressed: () => onLikeReply(
|
||||
context,
|
||||
isProcessing,
|
||||
() => isProcessing = false,
|
||||
isLike: isLike,
|
||||
isDislike: isDislike,
|
||||
),
|
||||
child: Row(
|
||||
spacing: 4,
|
||||
children: [
|
||||
Icon(
|
||||
isLike
|
||||
? FontAwesomeIcons.solidThumbsUp
|
||||
: FontAwesomeIcons.thumbsUp,
|
||||
size: 16,
|
||||
color: isLike ? primary : color,
|
||||
color: isLike ? primary : outline,
|
||||
semanticLabel: isLike ? '已赞' : '点赞',
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 400),
|
||||
transitionBuilder:
|
||||
(Widget child, Animation<double> animation) {
|
||||
return ScaleTransition(scale: animation, child: child);
|
||||
},
|
||||
child: Text(
|
||||
NumUtil.numFormat(widget.replyItem.like.toInt()),
|
||||
style: TextStyle(
|
||||
color: isLike ? primary : color,
|
||||
fontSize: theme.textTheme.labelSmall!.fontSize,
|
||||
),
|
||||
Text(
|
||||
NumUtil.numFormat(replyItem.like.toInt()),
|
||||
style: TextStyle(
|
||||
color: isLike ? primary : outline,
|
||||
fontSize: theme.textTheme.labelSmall!.fontSize,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user