comments final

This commit is contained in:
MohammadTaha Basiri 2022-01-29 14:06:24 +03:30
parent 293e759499
commit f6fb9082ce
2 changed files with 42 additions and 23 deletions

View File

@ -26,6 +26,8 @@ class Comments extends StatefulWidget {
class _CommentsState extends State<Comments> { class _CommentsState extends State<Comments> {
final _focusNode = FocusNode(); final _focusNode = FocusNode();
double _bottomPadding = 0;
@override @override
void initState() { void initState() {
final state = context.read<CommentsState>(); final state = context.read<CommentsState>();
@ -41,6 +43,14 @@ class _CommentsState extends State<Comments> {
@override @override
Widget build(BuildContext context) { 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( return Material(
child: Stack( child: Stack(
children: [ children: [
@ -99,7 +109,7 @@ class _MessageBoxState extends State<_MessageBox> {
children: [ children: [
AnimatedVisibility( AnimatedVisibility(
duration: DesignConfig.lowAnimationDuration, duration: DesignConfig.lowAnimationDuration,
isVisible: state.replyingTo != null, isVisible: state.showReplyBox,
child: Container( child: Container(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
decoration: BoxDecoration( decoration: BoxDecoration(
@ -127,6 +137,7 @@ class _MessageBoxState extends State<_MessageBox> {
onPressed: () { onPressed: () {
state.commentId = null; state.commentId = null;
state.replyingTo = null; state.replyingTo = null;
state.showReplyBox = false;
state.update(); state.update();
}, },
), ),

View File

@ -15,6 +15,7 @@ class CommentsState extends CoreProvier {
String text = ''; String text = '';
int? commentId; int? commentId;
UserOverview? replyingTo; UserOverview? replyingTo;
bool showReplyBox = false;
late VoidCallback onCommentAdded; late VoidCallback onCommentAdded;
final List<CommentData> comments = []; final List<CommentData> comments = [];
@ -62,23 +63,22 @@ class CommentsState extends CoreProvier {
Future<void> addComment() async { Future<void> addComment() async {
final user = DesignConfig.context.read<UserProvider>().user; final user = DesignConfig.context.read<UserProvider>().user;
if (replyingTo != null) { if (replyingTo != null) {
final comment = comments.firstWhere((comment) => comment.id == commentId); comments.firstWhere((comment) => comment.id == commentId).replies.add(
comment.replies.add( Reply(
Reply( id: 0,
id: 0, text: text,
text: text, createdAt: DateTime.now().toString(),
createdAt: DateTime.now().toString(), liked: false,
liked: false, disliked: false,
disliked: false, feedback: const FeedbackData(like: 0, dislike: 0),
feedback: const FeedbackData(like: 0, dislike: 0), toUser: replyingTo!,
toUser: replyingTo!, user: UserOverview(
user: UserOverview( id: user.id,
id: user.id, fullName: user.fullName,
fullName: user.fullName, photo: user.photo,
photo: user.photo, ),
), ),
), );
);
} else { } else {
comments.insert( comments.insert(
0, 0,
@ -109,22 +109,30 @@ class CommentsState extends CoreProvier {
if (replyingTo != null) { if (replyingTo != null) {
body.addAll({'replyUserId': replyingTo!.id}); body.addAll({'replyUserId': replyingTo!.id});
} }
showReplyBox = false;
update();
body.addAll({'text': text}); body.addAll({'text': text});
final service = RequestService( final service = RequestService(
isRadar isRadar
? RequestHelper.addRadarComment(itemId) ? RequestHelper.addRadarComment(itemId)
: RequestHelper.addNewsComment(itemId), : RequestHelper.addNewsComment(itemId),
body: body); body: body);
commentId = null;
replyingTo = null;
update();
await service.post(); await service.post();
if (service.isSuccess) { if (service.isSuccess) {
if (replyingTo != null) { 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']; service.result['comment']['id'];
} }
commentId = null;
replyingTo = null;
} }
update();
} }
} }