From 82d153533ac2745ac703f2ac3d970fd61caeb9f2 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Mon, 21 Sep 2020 12:47:16 +0200 Subject: [PATCH] fix bug where scores would show as 1 too many or 1 too few --- lib/widgets/comment.dart | 9 ++++++--- lib/widgets/post.dart | 8 ++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/widgets/comment.dart b/lib/widgets/comment.dart index 8345c03..3511cf8 100644 --- a/lib/widgets/comment.dart +++ b/lib/widgets/comment.dart @@ -23,6 +23,8 @@ class Comment extends HookWidget { final int postCreatorId; final CommentTree commentTree; + final bool wasVoted; + static const colors = [ Colors.pink, Colors.green, @@ -35,7 +37,8 @@ class Comment extends HookWidget { this.commentTree, { this.indent = 0, @required this.postCreatorId, - }); + }) : wasVoted = + (commentTree.comment.myVote ?? VoteType.none) != VoteType.none; _showCommentInfo(BuildContext context) { final com = commentTree.comment; @@ -343,8 +346,8 @@ class Comment extends HookWidget { size: Size.square(16), child: CircularProgressIndicator()) else - Text(compactNumber( - comment.score + myVote.value.value)), + Text(compactNumber(comment.score + + (wasVoted ? 0 : myVote.value.value))), Text(' ยท '), Text(timeago.format(comment.published)), ], diff --git a/lib/widgets/post.dart b/lib/widgets/post.dart index 2c31391..28a6725 100644 --- a/lib/widgets/post.dart +++ b/lib/widgets/post.dart @@ -435,7 +435,10 @@ class Post extends HookWidget { class _Voting extends HookWidget { final PostView post; - _Voting(this.post); + final bool wasVoted; + + _Voting(this.post) + : wasVoted = (post.myVote ?? VoteType.none) != VoteType.none; @override Widget build(BuildContext context) { @@ -477,7 +480,8 @@ class _Voting extends HookWidget { if (loading.loading) SizedBox(child: CircularProgressIndicator(), width: 20, height: 20) else - Text(NumberFormat.compact().format(post.score + myVote.value.value)), + Text(NumberFormat.compact() + .format(post.score + (wasVoted ? 0 : myVote.value.value))), IconButton( icon: Icon( Icons.arrow_downward,