Files
Regroup/lib/Main_Screens/Community_HomePage/PostDetailScreen/View/PostDetailScreen.dart
2024-08-20 20:01:30 +05:30

714 lines
33 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:regroup/Common/base_manager.dart';
import 'package:regroup/Common/controller/NormalPostCard.dart';
import 'package:regroup/Global.dart';
import 'package:regroup/Main_Screens/Community/Model/CommonDatumObjModel.dart';
import 'package:regroup/Main_Screens/Community/Model/fetchicons.dart';
import 'package:regroup/Main_Screens/Community_HomePage/PostDetailScreen/ViewModel/CommentsHelper.dart';
import 'package:regroup/Main_Screens/Community_HomePage/PostDetailScreen/ViewModel/CommentsRepository.dart';
import 'package:regroup/Main_Screens/Community_HomePage/PostDetailScreen/ViewModel/PostDetailApi.dart';
import 'package:regroup/Main_Screens/ProfileTab/view_model/profilePostmethod.dart';
import 'package:regroup/Utils/Common/CommonAppbar.dart';
import 'package:regroup/Utils/Common/CustomTextformfield.dart';
import 'package:regroup/Utils/Common/sized_box.dart';
import 'package:regroup/Utils/dialogs.dart';
import 'package:regroup/Utils/texts.dart';
import 'package:async/async.dart';
class PostDetailsScreen extends StatefulWidget {
const PostDetailsScreen({super.key});
@override
State<PostDetailsScreen> createState() => _PostDetailsScreenState();
}
class _PostDetailsScreenState extends State<PostDetailsScreen> {
int? followunfollowid;
followUnfollowUploadata() async {
// utils.loader();
Map<String, dynamic> newupdata = {
"following_iam_principal_xid": followunfollowid,
};
final data = await Profilepostmethod().postunfollowuser(newupdata);
if (data.status == ResponseStatus.SUCCESS) {
PostDetailApi().getPostDetail(mainpostid).then((value) {});
} else {
Get.back();
return utils.showToast(data.message);
}
}
List<bool> hideReplies = [];
FutureGroup futureGroup = FutureGroup();
final FocusNode _focusNode = FocusNode();
final TextEditingController _controllerComments = TextEditingController();
final CommentsHelper _commentsHelper = Get.put(CommentsHelper());
String? mainpostid;
List<ManageTagPopular>? tags;
String? create_at;
CommonDatumObjModelData? commonObj;
String? formWhichTab;
List<ReactionData>? reactions;
Map<int, ReactionData?>? selectedReactions;
int? currentIndex;
@override
void initState() {
mainpostid = Get.arguments['postId'].toString();
tags = Get.arguments['tagsList'];
create_at = Get.arguments['created_at'];
commonObj = Get.arguments['commonObj'];
formWhichTab = Get.arguments['fromWhichTab'];
reactions = Get.arguments['reactions'];
selectedReactions = Get.arguments['selectedReactions'];
currentIndex = Get.arguments['currentIndex'];
futureGroup.add(CommentsRepository().getAllComments(mainpostid));
futureGroup.add(PostDetailApi().getPostDetail(mainpostid));
futureGroup.close();
super.initState();
}
bool isOnce = true;
_setViewMoreList() {
if (isOnce) {
hideReplies = List.filled(nestedComments!.data.length, false);
isOnce = false;
}
}
_toggleReplies(index) {
setState(() {
hideReplies[index] = !hideReplies[index];
});
}
void _focusTextField() {
FocusScope.of(context).requestFocus(_focusNode);
}
_postComments() async {
var updata = {
"manage_posts_xid": mainpostid,
"comment": _controllerComments.text,
};
final result = await CommentsRepository().postComments(updata);
if (result.status == ResponseStatus.SUCCESS) {
CommentsRepository()
.getAllComments(mainpostid)
.then((value) => setState(() {
isOnce = true;
_commentsHelper.isLoading.value = false;
_controllerComments.clear();
}));
}
}
String repliedCommentId = "";
_postRepliesOnComment(repliedCommentId) async {
var updata = {
"manage_posts_xid": mainpostid,
"posts_master_comment_xid": repliedCommentId,
"comment": _controllerComments.text,
};
final result = await CommentsRepository().postRepliesOnComments(updata);
if (result.status == ResponseStatus.SUCCESS) {
CommentsRepository()
.getAllComments(mainpostid)
.then((value) => setState(() {
isOnce = true;
_commentsHelper.isLoading.value = false;
_controllerComments.clear();
}));
}
}
// _setComments(index) {
// List<Comment> repliedCommentsList = [];
// for (var i = 0;
// i < nestedComments!.data[index].repliedComment.length;
// i++) {
// repliedCommentsList.add(Comment(
// avatar: nestedComments!
// .data[index].repliedComment[i].userDetail?.profilePhoto,
// userName: nestedComments!
// .data[index].repliedComment[i].userDetail?.userName,
// content: nestedComments!.data[index].repliedComment[i].comment));
// }
// return repliedCommentsList;
// }
// bool _hideReplies = true;
_deleteComment(String commentId) async {
var updata = {"id": commentId};
final result = await CommentsRepository().deleteComments(updata);
if (result.status == ResponseStatus.SUCCESS) {
CommentsRepository()
.getAllComments(mainpostid)
.then((value) => setState(() {
isOnce = true;
}));
}
}
_deleteRepliedComment(String commentId) async {
var updata = {"id": commentId};
final result = await CommentsRepository().deleteRepliedComments(updata);
if (result.status == ResponseStatus.SUCCESS) {
CommentsRepository()
.getAllComments(mainpostid)
.then((value) => setState(() {
isOnce = true;
}));
}
}
//reacttionlikelogic
@override
Widget build(BuildContext context) {
return KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
return WillPopScope(
onWillPop: () async {
Get.back(result: true);
return false; // Prevent default back button action
},
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: const Color(0xFF222935),
extendBody: true,
appBar: const CommonAppbar(
customBack: true,
// customRouteName: RouteName.communityScreen,
titleTxt: "Post",
),
body: FutureBuilder(
future: futureGroup.future,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(
color: Colors.blue,
),
);
} else if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18.spMin),
),
);
} else if (snapshot.connectionState == ConnectionState.done) {
_setViewMoreList();
return _buildBody(isKeyboardVisible);
} else {
return const Center(child: Text('Something went wrong'));
}
},
)),
);
});
}
_buildBody(isKeyboardVisible) {
return Stack(children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill)),
),
SingleChildScrollView(
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
// sizedBoxHeight(35.h),
NormalCardTile(
tags: tags!,
createAt: create_at!,
commonObj: commonObj!,
forWhichTab: formWhichTab!,
reactions: reactions!,
selectedReactions: selectedReactions!,
currentIndex: currentIndex!,
),
// ListView.builder(
// physics: const NeverScrollableScrollPhysics(),
// shrinkWrap: true,
// itemCount: nestedComments!.data.length,
// itemBuilder: (context, index) {
// List<Comment> _repliedCommentsSorted = _setComments(index);
// return Container(
// padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
// child: CommentTreeWidget<Comment, Comment>(
// Comment(
// avatar:
// nestedComments!.data[index].userDetail!.profilePhoto,
// userName: nestedComments!.data[index].userDetail!.userName,
// content: nestedComments!.data[index].comment),
// [],
// // _repliedCommentsSorted.isNotEmpty ? _repliedCommentsSorted : [],
// treeThemeData: const TreeThemeData(
// lineColor: Color(0xFFD90B2E), lineWidth: 2.5),
// avatarRoot: (context, data) => PreferredSize(
// preferredSize: const Size.fromRadius(18),
// child: CircleAvatar(
// radius: 18,
// backgroundColor: Colors.grey,
// backgroundImage: NetworkImage(
// data.avatar ??
// "https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png?20150327203541",
// ),
// ),
// ),
// avatarChild: (context, data) => PreferredSize(
// preferredSize: const Size.fromRadius(12),
// child: CircleAvatar(
// radius: 12,
// backgroundColor: Colors.grey,
// backgroundImage: NetworkImage(
// data.avatar ??
// "https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png?20150327203541",
// ),
// ),
// ),
// contentChild: (context, data) {
// return data.userName! == "Ram" && _hideReplies[index]
// ? GestureDetector(
// onTap: () {
// setState(() {
// _hideReplies[index] = false;
// });
// },
// child: Container(
// child: const Text(
// "View more",
// style: TextStyle(color: Colors.white),
// ),
// ),
// )
// : Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Container(
// padding: const EdgeInsets.symmetric(
// vertical: 8, horizontal: 8),
// decoration: BoxDecoration(
// color: Colors.grey[100],
// borderRadius: BorderRadius.circular(12)),
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// '${data.userName}',
// style: Theme.of(context)
// .textTheme
// .bodySmall
// ?.copyWith(
// fontWeight: FontWeight.w600,
// color: Colors.black),
// ),
// const SizedBox(
// height: 4,
// ),
// Text(
// '${data.content}',
// style: Theme.of(context)
// .textTheme
// .bodySmall
// ?.copyWith(
// fontWeight: FontWeight.w300,
// color: Colors.black),
// ),
// ],
// ),
// ),
// DefaultTextStyle(
// style: Theme.of(context)
// .textTheme
// .bodySmall!
// .copyWith(
// color: Colors.grey[700],
// fontWeight: FontWeight.bold),
// child: const Padding(
// padding: EdgeInsets.only(top: 4),
// child: Row(
// children: [
// SizedBox(
// width: 8,
// ),
// Text('Like'),
// SizedBox(
// width: 24,
// ),
// Text('Reply'),
// ],
// ),
// ),
// )
// ],
// );
// },
// contentRoot: (context, data) {
// return Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Container(
// padding: const EdgeInsets.symmetric(
// vertical: 8, horizontal: 8),
// decoration: BoxDecoration(
// color: Colors.grey[100],
// borderRadius: BorderRadius.circular(12)),
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// 'dangngocduc',
// style: Theme.of(context)
// .textTheme
// .bodySmall!
// .copyWith(
// fontWeight: FontWeight.w600,
// color: Colors.black),
// ),
// const SizedBox(
// height: 4,
// ),
// Text(
// '${data.content}',
// style: Theme.of(context)
// .textTheme
// .bodySmall!
// .copyWith(
// fontWeight: FontWeight.w300,
// color: Colors.black),
// ),
// ],
// ),
// ),
// DefaultTextStyle(
// style: Theme.of(context).textTheme.bodySmall!.copyWith(
// color: Colors.grey[700],
// fontWeight: FontWeight.bold),
// child: const Padding(
// padding: EdgeInsets.only(top: 4),
// child: Row(
// children: [
// SizedBox(
// width: 8,
// ),
// Text('Like'),
// SizedBox(
// width: 24,
// ),
// Text('Reply'),
// ],
// ),
// ),
// )
// ],
// );
// },
// ),
// );
// },
// ),
sizedBoxHeight(35.h),
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: nestedComments?.data.length,
itemBuilder: (context, index) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: Column(
children: [
GestureDetector(
onLongPressStart: (details) {
nestedComments?.data[index].userDetail!.id
.toString() !=
userIdGlobal
? null
: showMenu(
context: context,
position: RelativeRect.fromLTRB(
details.globalPosition.dx,
details.globalPosition.dy,
details.globalPosition.dx + 1,
details.globalPosition.dy + 1,
),
items: [
const PopupMenuItem<int>(
value: 0,
child: Text("Delete"),
),
],
elevation: 8.0,
).then((value) {
if (value == 0) {
_deleteComment(nestedComments!
.data[index].id
.toString());
}
});
},
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
radius: 20.r,
foregroundImage: NetworkImage(
nestedComments!
.data[index].userDetail!.profilePhoto ??
"https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png?20150327203541",
),
),
sizedBoxWidth(15.w),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
text12w400_FCFCFC_blur(nestedComments!
.data[index].userDetail!.userName ??
""),
text14w400_FCFCFC(
nestedComments!.data[index].comment ??
""),
SizedBox(height: 10.h),
GestureDetector(
onTap: () {
_focusTextField();
_controllerComments.text =
"@${nestedComments!.data[index].userDetail!.userName!}";
setState(() {
repliedCommentId = nestedComments!
.data[index].id
.toString();
});
},
child: text10w400_FCFCFC_blur("Reply")),
SizedBox(height: 20.h),
nestedComments!
.data[index].repliedComment.isEmpty
? const SizedBox()
: hideReplies[index]
? GestureDetector(
onTap: () {
_toggleReplies(index);
},
child: text10w400_FCFCFC_blur(
"Hide replies"),
)
: GestureDetector(
onTap: () {
_toggleReplies(index);
},
child: text10w400_FCFCFC_blur(
"View ${nestedComments!.data[index].repliedComment.length} more replies"),
),
SizedBox(height: 10.h),
!hideReplies[index]
? const SizedBox()
: ListView.builder(
physics:
const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: nestedComments!.data[index]
.repliedComment.length,
itemBuilder: (context, index2) {
return GestureDetector(
onLongPressStart:
(LongPressStartDetails
details) {
nestedComments
?.data[index]
.repliedComment[
index2]
.userDetail
?.id
.toString() !=
userIdGlobal
? null
: showMenu(
context: context,
position: RelativeRect
.fromLTRB(
details.globalPosition
.dx,
details.globalPosition
.dy,
details.globalPosition
.dx +
1,
details.globalPosition
.dy +
1,
),
items: [
const PopupMenuItem<
int>(
value: 0,
child:
Text("Delete"),
),
],
elevation: 8.0,
).then((value) {
if (value == 0) {
_deleteRepliedComment(
nestedComments!
.data[index]
.repliedComment[
index2]
.id
.toString());
}
});
},
child: ListTile(
leading: Column(
mainAxisAlignment:
MainAxisAlignment.start,
children: [
CircleAvatar(
radius: 13.r,
foregroundImage:
NetworkImage(
nestedComments!
.data[index]
.repliedComment[
index2]
.userDetail
?.profilePhoto ??
"https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png?20150327203541",
),
),
],
),
title: text12w400_FCFCFC_blur(
nestedComments!
.data[index]
.repliedComment[
index2]
.userDetail
?.userName ??
""),
subtitle: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
text14w400_FCFCFC(
nestedComments!
.data[index]
.repliedComment[
index2]
.comment ??
""),
SizedBox(height: 10.h),
GestureDetector(
onTap: () {
_focusTextField();
_controllerComments
.text =
"@${nestedComments!.data[index].repliedComment[index2].userDetail!.userName!}";
setState(() {
repliedCommentId =
nestedComments!
.data[index]
.repliedComment[
index2]
.postsMasterCommentXid
.toString();
});
},
child:
text10w400_FCFCFC_blur(
"Reply")),
],
),
),
);
})
],
),
),
],
),
),
sizedBoxHeight(20.h),
//sizedBoxHeight(85.h)
],
));
}),
sizedBoxHeight(50.h),
])),
Positioned.fill(
top: 625.h - MediaQuery.of(context).viewInsets.bottom,
// isKeyboardVisible ? 270.h : 625.h,
right: 16.0,
left: 16.0,
child: Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(top: 15, bottom: 15),
child: Row(
children: <Widget>[
Obx(() {
return Expanded(
child: CustomTextFormField(
textEditingController: _controllerComments,
focusNode: _focusNode,
hintText: "Add comment",
suffixIcon: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
if (_controllerComments.text.isEmpty) {
print("Enter some text");
} else {
_commentsHelper.isLoading.value = true;
if (repliedCommentId != "") {
_postRepliesOnComment(repliedCommentId);
} else {
_postComments();
}
}
},
child: _commentsHelper.isLoading.value
? Container(
padding: const EdgeInsets.all(10),
height: 10.h,
width: 10.w,
child: const CircularProgressIndicator())
: SizedBox(
height: 20.h,
width: 25.w,
child: Center(
child: Image.asset(
"assets/images/png/iconoir_send.png",
height: 20.h,
width: 25.w,
),
),
),
),
),
);
})
],
),
)),
),
]);
}
}