From f6fb9082ce9e18e748a165d4230357e857011c11 Mon Sep 17 00:00:00 2001 From: MohammadTaha Basiri Date: Sat, 29 Jan 2022 14:06:24 +0330 Subject: [PATCH] comments final --- lib/pages/home/comments/comments.dart | 13 +++++- lib/pages/home/comments/comments_state.dart | 52 ++++++++++++--------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/pages/home/comments/comments.dart b/lib/pages/home/comments/comments.dart index 25389f7..b13e31c 100644 --- a/lib/pages/home/comments/comments.dart +++ b/lib/pages/home/comments/comments.dart @@ -26,6 +26,8 @@ class Comments extends StatefulWidget { class _CommentsState extends State { final _focusNode = FocusNode(); + double _bottomPadding = 0; + @override void initState() { final state = context.read(); @@ -41,6 +43,14 @@ class _CommentsState extends State { @override Widget build(BuildContext context) { + final bottomViewInset = MediaQuery.of(context).viewInsets.bottom; + if (bottomViewInset == 0) { + if (_bottomPadding != 0) { + FocusScope.of(context).unfocus(); + _bottomPadding = 0; + } + } + _bottomPadding = bottomViewInset; return Material( child: Stack( children: [ @@ -99,7 +109,7 @@ class _MessageBoxState extends State<_MessageBox> { children: [ AnimatedVisibility( duration: DesignConfig.lowAnimationDuration, - isVisible: state.replyingTo != null, + isVisible: state.showReplyBox, child: Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( @@ -127,6 +137,7 @@ class _MessageBoxState extends State<_MessageBox> { onPressed: () { state.commentId = null; state.replyingTo = null; + state.showReplyBox = false; state.update(); }, ), diff --git a/lib/pages/home/comments/comments_state.dart b/lib/pages/home/comments/comments_state.dart index 48a7eba..7cb276c 100644 --- a/lib/pages/home/comments/comments_state.dart +++ b/lib/pages/home/comments/comments_state.dart @@ -15,6 +15,7 @@ class CommentsState extends CoreProvier { String text = ''; int? commentId; UserOverview? replyingTo; + bool showReplyBox = false; late VoidCallback onCommentAdded; final List comments = []; @@ -62,23 +63,22 @@ class CommentsState extends CoreProvier { Future addComment() async { final user = DesignConfig.context.read().user; if (replyingTo != null) { - final comment = comments.firstWhere((comment) => comment.id == commentId); - comment.replies.add( - Reply( - id: 0, - text: text, - createdAt: DateTime.now().toString(), - liked: false, - disliked: false, - feedback: const FeedbackData(like: 0, dislike: 0), - toUser: replyingTo!, - user: UserOverview( - id: user.id, - fullName: user.fullName, - photo: user.photo, - ), - ), - ); + comments.firstWhere((comment) => comment.id == commentId).replies.add( + Reply( + id: 0, + text: text, + createdAt: DateTime.now().toString(), + liked: false, + disliked: false, + feedback: const FeedbackData(like: 0, dislike: 0), + toUser: replyingTo!, + user: UserOverview( + id: user.id, + fullName: user.fullName, + photo: user.photo, + ), + ), + ); } else { comments.insert( 0, @@ -109,22 +109,30 @@ class CommentsState extends CoreProvier { if (replyingTo != null) { body.addAll({'replyUserId': replyingTo!.id}); } + + showReplyBox = false; + update(); body.addAll({'text': text}); final service = RequestService( isRadar ? RequestHelper.addRadarComment(itemId) : RequestHelper.addNewsComment(itemId), body: body); - commentId = null; - replyingTo = null; - update(); + await service.post(); if (service.isSuccess) { if (replyingTo != null) { - comments.firstWhere((comment) => comment.id == commentId).id = + comments + .firstWhere((comment) => comment.id == commentId) + .replies + .firstWhere((reply) => reply.id == 0) + .id = service.result['comment']['id']; + } else { + comments.firstWhere((comment) => comment.id == 0).id = service.result['comment']['id']; } + commentId = null; + replyingTo = null; } - update(); } }