forked from eatvio/eatvio_app
266 lines
8.7 KiB
Dart
266 lines
8.7 KiB
Dart
import 'package:eatvio_app/core/app_config.dart';
|
|
import 'package:eatvio_app/core/comments/presentation/comments_actions_bottom_sheet.dart';
|
|
import 'package:eatvio_app/core/enums/complained_content.dart';
|
|
import 'package:eatvio_app/core/locator_service.dart';
|
|
import 'package:eatvio_app/core/utils/date_time_utils.dart';
|
|
import 'package:eatvio_app/core/utils/url_utils.dart';
|
|
import 'package:eatvio_app/core/utils/toasts.dart';
|
|
import 'package:eatvio_app/features/bottom_navigation_bar/presentation/bloc/profile_avatar_cubit/profile_avatar_cubit.dart';
|
|
import 'package:eatvio_app/features/bottom_navigation_bar/presentation/bloc/profile_avatar_cubit/profile_avatar_state.dart';
|
|
import 'package:eatvio_app/features/complaining/presentation/complaining_bottom_sheet.dart';
|
|
import 'package:eatvio_app/features/feed/models/comment_model.dart';
|
|
import 'package:eatvio_app/core/comments/comments_cubit/comments_cubit.dart';
|
|
import 'package:eatvio_app/generated/l10n.dart';
|
|
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
enum CommentSourceType { blogPost, recipe, diet, plan }
|
|
|
|
class CommentsWidget extends StatefulWidget {
|
|
final String id;
|
|
final CommentSourceType commentSourceType;
|
|
final CommentModel comment;
|
|
final int? replyId;
|
|
final int? highlightedCommentId;
|
|
|
|
const CommentsWidget({
|
|
super.key,
|
|
required this.id,
|
|
required this.commentSourceType,
|
|
required this.comment,
|
|
this.replyId,
|
|
this.highlightedCommentId,
|
|
});
|
|
|
|
@override
|
|
State<CommentsWidget> createState() => _CommentsWidgetState();
|
|
}
|
|
|
|
class _CommentsWidgetState extends State<CommentsWidget> {
|
|
final Map<int, GlobalKey> commentKeys = {};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.highlightedCommentId != null) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_scrollToHighlightedComment();
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(CommentsWidget oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.highlightedCommentId != null &&
|
|
widget.highlightedCommentId != oldWidget.highlightedCommentId) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_scrollToHighlightedComment();
|
|
});
|
|
} else if (widget.comment != oldWidget.comment &&
|
|
widget.highlightedCommentId != null) {
|
|
// Comments might have just loaded
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_scrollToHighlightedComment();
|
|
});
|
|
}
|
|
}
|
|
|
|
void _scrollToHighlightedComment() {
|
|
final key = commentKeys[widget.highlightedCommentId];
|
|
if (key != null && key.currentContext != null) {
|
|
Scrollable.ensureVisible(
|
|
key.currentContext!,
|
|
duration: const Duration(milliseconds: 500),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = S.of(context);
|
|
return Column(
|
|
children: [
|
|
for (int i = 0; i < widget.comment.data.length; i++) ...[
|
|
_comment(s, widget.comment.data[i]),
|
|
if (widget.comment.data[i].childrenComments.isNotEmpty)
|
|
const SizedBox(height: 8),
|
|
for (
|
|
int j = 0;
|
|
j < widget.comment.data[i].childrenComments.length;
|
|
j++
|
|
)
|
|
Padding(
|
|
padding: const EdgeInsets.only(left: 40),
|
|
child: _comment(
|
|
s,
|
|
widget.comment.data[i].childrenComments[j],
|
|
16,
|
|
true,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
],
|
|
const SizedBox(height: 48),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _comment(
|
|
S s,
|
|
Comment comment, [
|
|
double radius = 18,
|
|
bool isChild = false,
|
|
]) {
|
|
final key = commentKeys.putIfAbsent(comment.id, () => GlobalKey());
|
|
final isMe =
|
|
(sl<ProfileAvatarCubit>().state as ProfileAvatarStateLoaded)
|
|
.profile
|
|
.slug ==
|
|
comment.authorSlug;
|
|
return GestureDetector(
|
|
key: key,
|
|
onTap: () {
|
|
showCommentsActionsBottomSheet(
|
|
context,
|
|
commentText: comment.body,
|
|
reply: !isChild
|
|
? () {
|
|
context.read<CommentsCubit>().setReplyId(comment.id);
|
|
}
|
|
: null,
|
|
copy: () {
|
|
Clipboard.setData(ClipboardData(text: comment.body));
|
|
showInfoToast(s.text_was_copied);
|
|
},
|
|
edit: (String text) {
|
|
context.read<CommentsCubit>().updateComment(
|
|
id: comment.id,
|
|
body: text,
|
|
);
|
|
},
|
|
delete: () {
|
|
context.read<CommentsCubit>().deleteComment(id: comment.id);
|
|
},
|
|
complain: () {
|
|
showModalBottomSheet(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
builder: (context) => ComplainingBottomSheet(
|
|
complainedContentType: ComplainedContent.comment,
|
|
id: comment.id,
|
|
),
|
|
);
|
|
},
|
|
isMyComment: isMe,
|
|
);
|
|
},
|
|
child: Container(
|
|
width: MediaQuery.of(context).size.width,
|
|
decoration: BoxDecoration(
|
|
color:
|
|
(widget.replyId == comment.id ||
|
|
widget.highlightedCommentId == comment.id)
|
|
? appTheme.blueA400.withValues(alpha: 0.1)
|
|
: Colors.white,
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () {
|
|
GoRouter.of(
|
|
context,
|
|
).pushNamed('profileScreen', extra: comment.authorSlug);
|
|
},
|
|
child: CircleAvatar(
|
|
foregroundImage: NetworkImage(
|
|
joinBaseUrl(
|
|
sl<AppConfig>().baseUrl,
|
|
comment.authorProfileIcon,
|
|
),
|
|
),
|
|
backgroundImage: const AssetImage(
|
|
'assets/images/no_avatar.png',
|
|
),
|
|
radius: radius,
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
comment.authorName,
|
|
style: TextStyle(
|
|
height: 1,
|
|
color: appTheme.gray,
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
SizedBox(
|
|
width: isChild
|
|
? MediaQuery.of(context).size.width * 0.72
|
|
: MediaQuery.of(context).size.width * 0.8,
|
|
child: Text(
|
|
comment.body,
|
|
style: TextStyle(
|
|
height: 1,
|
|
color: appTheme.black,
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Row(
|
|
children: [
|
|
Text(
|
|
comment.createdAt.format('dd MMMM', 'ru'),
|
|
style: TextStyle(
|
|
height: 1,
|
|
color: appTheme.gray,
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
const SizedBox(width: 16),
|
|
if (!isChild)
|
|
GestureDetector(
|
|
onTap: () {
|
|
context.read<CommentsCubit>().setReplyId(
|
|
comment.id,
|
|
);
|
|
},
|
|
child: Text(
|
|
s.answer,
|
|
style: TextStyle(
|
|
height: 1,
|
|
color: appTheme.gray,
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|