From f7da8885595862c48a549a884fb2b473908490c2 Mon Sep 17 00:00:00 2001 From: MohammadTaha Basiri Date: Wed, 26 Jan 2022 00:16:55 +0330 Subject: [PATCH] comments bug fixed --- lib/pages/home/comments/comments.dart | 11 ++++++++--- lib/pages/home/comments/comments_state.dart | 16 ++++++++++------ .../home/comments/widgets/comment_item.dart | 3 +++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/pages/home/comments/comments.dart b/lib/pages/home/comments/comments.dart index 9aafbac..25389f7 100644 --- a/lib/pages/home/comments/comments.dart +++ b/lib/pages/home/comments/comments.dart @@ -25,6 +25,7 @@ class Comments extends StatefulWidget { } class _CommentsState extends State { + final _focusNode = FocusNode(); @override void initState() { final state = context.read(); @@ -61,6 +62,7 @@ class _CommentsState extends State { childCount: state.comments.length, placeholder: const _CommentPlaceholder(), builder: (context, state, index) => Comment( + focusNode: _focusNode, comment: state.comments[index], ), ), @@ -68,10 +70,10 @@ class _CommentsState extends State { ], ), Positioned( - bottom: MediaQuery.of(context).viewInsets.bottom, left: 0, right: 0, - child: const _MessageBox(), + bottom: MediaQuery.of(context).viewInsets.bottom, + child: _MessageBox(focusNode: _focusNode), ), ], ), @@ -80,7 +82,8 @@ class _CommentsState extends State { } class _MessageBox extends StatefulWidget { - const _MessageBox({Key? key}) : super(key: key); + final FocusNode focusNode; + const _MessageBox({Key? key, required this.focusNode}) : super(key: key); @override State<_MessageBox> createState() => _MessageBoxState(); @@ -150,7 +153,9 @@ class _MessageBoxState extends State<_MessageBox> { ), Expanded( child: TextField( + focusNode: widget.focusNode, controller: _controller, + keyboardType: TextInputType.multiline, textInputAction: TextInputAction.send, style: Theme.of(context).textTheme.bodyText2, onEditingComplete: () {}, diff --git a/lib/pages/home/comments/comments_state.dart b/lib/pages/home/comments/comments_state.dart index d079c57..48a7eba 100644 --- a/lib/pages/home/comments/comments_state.dart +++ b/lib/pages/home/comments/comments_state.dart @@ -1,5 +1,3 @@ -import 'dart:math'; - import 'package:didvan/config/design_config.dart'; import 'package:didvan/models/comment/comment.dart'; import 'package:didvan/models/comment/feedback.dart'; @@ -64,10 +62,10 @@ class CommentsState extends CoreProvier { Future addComment() async { final user = DesignConfig.context.read().user; if (replyingTo != null) { - final coment = comments.firstWhere((comment) => comment.id == commentId); - coment.replies.add( + final comment = comments.firstWhere((comment) => comment.id == commentId); + comment.replies.add( Reply( - id: Random().nextInt(1000), + id: 0, text: text, createdAt: DateTime.now().toString(), liked: false, @@ -85,7 +83,7 @@ class CommentsState extends CoreProvier { comments.insert( 0, CommentData( - id: Random().nextInt(1000), + id: 0, text: text, createdAt: DateTime.now().toString(), liked: false, @@ -121,6 +119,12 @@ class CommentsState extends CoreProvier { replyingTo = null; update(); await service.post(); + if (service.isSuccess) { + if (replyingTo != null) { + comments.firstWhere((comment) => comment.id == commentId).id = + service.result['comment']['id']; + } + } update(); } } diff --git a/lib/pages/home/comments/widgets/comment_item.dart b/lib/pages/home/comments/widgets/comment_item.dart index 96e0a33..07a92af 100644 --- a/lib/pages/home/comments/widgets/comment_item.dart +++ b/lib/pages/home/comments/widgets/comment_item.dart @@ -13,9 +13,11 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class Comment extends StatefulWidget { + final FocusNode focusNode; final CommentData comment; const Comment({ Key? key, + required this.focusNode, required this.comment, }) : super(key: key); @@ -113,6 +115,7 @@ class CommentState extends State { state.commentId = _comment.id; state.replyingTo = comment.user; state.update(); + widget.focusNode.requestFocus(); }, child: DidvanText( 'پاسخ',