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> {
final _focusNode = FocusNode();
double _bottomPadding = 0;
@override
void initState() {
final state = context.read<CommentsState>();
@ -41,6 +43,14 @@ class _CommentsState extends State<Comments> {
@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();
},
),

View File

@ -15,6 +15,7 @@ class CommentsState extends CoreProvier {
String text = '';
int? commentId;
UserOverview? replyingTo;
bool showReplyBox = false;
late VoidCallback onCommentAdded;
final List<CommentData> comments = [];
@ -62,8 +63,7 @@ class CommentsState extends CoreProvier {
Future<void> addComment() async {
final user = DesignConfig.context.read<UserProvider>().user;
if (replyingTo != null) {
final comment = comments.firstWhere((comment) => comment.id == commentId);
comment.replies.add(
comments.firstWhere((comment) => comment.id == commentId).replies.add(
Reply(
id: 0,
text: text,
@ -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'];
}
}
update();
commentId = null;
replyingTo = null;
}
}
}