comments bug fixed

This commit is contained in:
MohammadTaha Basiri 2022-01-26 00:16:55 +03:30
parent 9884ae72f6
commit f7da888559
3 changed files with 21 additions and 9 deletions

View File

@ -25,6 +25,7 @@ class Comments extends StatefulWidget {
}
class _CommentsState extends State<Comments> {
final _focusNode = FocusNode();
@override
void initState() {
final state = context.read<CommentsState>();
@ -61,6 +62,7 @@ class _CommentsState extends State<Comments> {
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<Comments> {
],
),
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<Comments> {
}
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: () {},

View File

@ -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<void> addComment() async {
final user = DesignConfig.context.read<UserProvider>().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();
}
}

View File

@ -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<Comment> {
state.commentId = _comment.id;
state.replyingTo = comment.user;
state.update();
widget.focusNode.requestFocus();
},
child: DidvanText(
'پاسخ',