You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

271 lines
8.1 KiB

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dating_touchme_app/config/emoji_config.dart';
import 'package:dating_touchme_app/extension/ex_widget.dart';
import 'package:dating_touchme_app/generated/assets.dart';
import 'package:dating_touchme_app/model/home/post_data.dart';
import 'package:dating_touchme_app/pages/home/timeline_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import '../../network/home_api.dart';
class RealHomeTimelineItem extends StatefulWidget {
final Records item;
const RealHomeTimelineItem({super.key, required this.item});
@override
State<RealHomeTimelineItem> createState() => _RealHomeTimelineItemState();
}
class _RealHomeTimelineItemState extends State<RealHomeTimelineItem> {
/// 构建输入框内容(文本+表情)
List<Widget> buildInputContentWidgets() {
final List<Widget> widgets = [];
final text = widget.item.content ?? "";
final RegExp emojiRegex = RegExp(r'\[emoji:(\d+)\]');
int lastMatchEnd = 0;
final matches = emojiRegex.allMatches(text);
for (final match in matches) {
// 添加表情之前的文本
if (match.start > lastMatchEnd) {
final textPart = text.substring(lastMatchEnd, match.start);
widgets.add(
Text(
textPart,
style: TextStyle(fontSize: 14.sp, color: Colors.black),
),
);
}
// 添加表情图片
final emojiId = match.group(1);
if (emojiId != null) {
final emoji = EmojiConfig.getEmojiById(emojiId);
if (emoji != null) {
widgets.add(
Padding(
padding: EdgeInsets.symmetric(horizontal: 0),
child: Image.asset(
emoji.path,
width: 24.w,
height: 24.w,
fit: BoxFit.contain,
),
),
);
}
}
lastMatchEnd = match.end;
}
// 添加剩余的文本
if (lastMatchEnd < text.length) {
final textPart = text.substring(lastMatchEnd);
widgets.add(
Text(
textPart,
style: TextStyle(fontSize: 14.sp, color: Colors.black),
),
);
}
return widgets;
}
List<String> imgList = [];
late final HomeApi _homeApi;
@override
void initState() {
super.initState();
_homeApi = Get.find<HomeApi>();
getImgList();
}
getImgList(){
if(widget.item.mediaUrls != null && widget.item.mediaUrls != ""){
imgList = widget.item.mediaUrls!.split(",");
setState(() {
});
}
}
likePost() async {
try {
final response = await _homeApi.userLikePost({
"id": widget.item.id,
"isLiked": !(widget.item.isLiked ?? false),
});
if (response.data.isSuccess) {
if(widget.item.isLiked ?? false){
SmartDialog.showToast('取消点赞成功');
widget.item.likeCount = widget.item.likeCount! - 1;
} else {
SmartDialog.showToast('点赞成功');
widget.item.likeCount = widget.item.likeCount! + 1;
}
widget.item.isLiked = !(widget.item.isLiked ?? false);
setState(() {
});
} else {
// 响应失败,抛出异常
throw Exception(response.data.message ?? '获取数据失败');
}
} catch(e){
print('帖子发布失败: $e');
SmartDialog.showToast('帖子发布失败');
rethrow;
}
}
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8.w)),
child: Container(
width: 170.w,
height: 205.w,
color: Colors.white,
child: Column(
children: [
Stack(
children: [
if(imgList.isNotEmpty) CachedNetworkImage(
imageUrl: imgList[0],
width: 170.w,
height: 170.w,
fit: BoxFit.cover,
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
errorWidget: (context, url, error) => Image.asset(
Assets.imagesDefaultTimelineImg,
width: 170.w,
height: 170.w,
fit: BoxFit.cover,
),
),
if(imgList.isEmpty)Image.asset(
Assets.imagesDefaultTimelineImg,
width: 170.w,
height: 170.w,
),
Positioned(
left: 0,
bottom: 0,
child: Container(
width: 170.w,
padding: EdgeInsets.symmetric(horizontal: 10.w),
child: Text(
"${widget.item.content}",
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 11.w,
color: Colors.white
),
),
),
)
],
),
Container(
width: 170.w,
height: 35.w,
color: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 10.w),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(23.w)),
child: CachedNetworkImage(
imageUrl: widget.item.profilePhoto ?? "",
width: 23.w,
height: 23.w,
fit: BoxFit.cover,
),
),
SizedBox(width: 3.w,),
Expanded(
child: Text(
"${widget.item.nickName ?? ""}",
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.w,
fontWeight: FontWeight.w400
),
),
)
],
),
),
Row(
children: [
Row(
children: [
Image.asset(
(widget.item.isLiked ?? false) ? Assets.imagesLikeActive : Assets.imagesLikeIcon,
width: 14.w,
height: 12.w,
),
SizedBox(width: 6.w,),
Text(
"${widget.item.likeCount ?? 0}",
style: TextStyle(
fontSize: 11.w,
color: const Color.fromRGBO(144, 144, 144, .6)
),
)
],
).onTap((){
likePost();
})
],
)
],
),
)
],
),
).onTap((){
Get.to(() => TimelineInfo(id: widget.item.id ?? "",))?.then((e){
widget.item.likeCount = e.likeCount;
widget.item.isLiked = e.isLiked;
widget.item.commentCount = e.commentCount;
setState(() {
});
});
}),
);
}
}