2024-03-23 15:05:04 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-03-23 16:13:52 +00:00
|
|
|
import 'package:flutter_carousel_widget/flutter_carousel_widget.dart';
|
2024-03-23 15:05:04 +00:00
|
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
|
|
|
import 'package:solaragent/models/feed.dart';
|
2024-03-23 16:13:52 +00:00
|
|
|
import 'package:solaragent/widgets/image.dart';
|
2024-03-24 04:12:13 +00:00
|
|
|
import 'package:solaragent/widgets/posts/comments.dart';
|
2024-03-24 05:34:29 +00:00
|
|
|
import 'package:solaragent/widgets/posts/reactions.dart';
|
2024-03-23 15:05:04 +00:00
|
|
|
|
2024-03-24 05:34:29 +00:00
|
|
|
class FeedItem extends StatefulWidget {
|
2024-03-23 15:05:04 +00:00
|
|
|
final Feed item;
|
|
|
|
|
|
|
|
const FeedItem({super.key, required this.item});
|
|
|
|
|
2024-03-24 05:34:29 +00:00
|
|
|
@override
|
|
|
|
State<FeedItem> createState() => _FeedItemState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FeedItemState extends State<FeedItem> {
|
|
|
|
int reactionCount = 0;
|
|
|
|
Map<String, dynamic>? reactionList;
|
|
|
|
|
2024-03-24 04:12:13 +00:00
|
|
|
void viewComments(BuildContext context) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
isScrollControlled: true,
|
2024-03-24 05:34:29 +00:00
|
|
|
builder: (context) => CommentList(parent: widget.item),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void viewReactions(BuildContext context) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ReactionList(
|
|
|
|
parent: widget.item,
|
|
|
|
onReact: (symbol, num) {
|
|
|
|
setState(() {
|
|
|
|
if (!reactionList!.containsKey(symbol)) {
|
|
|
|
reactionList![symbol] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
reactionCount += num;
|
|
|
|
reactionList![symbol] += num;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2024-03-24 04:12:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-23 16:13:52 +00:00
|
|
|
bool hasAttachments() =>
|
2024-03-24 05:34:29 +00:00
|
|
|
widget.item.attachments != null && widget.item.attachments!.isNotEmpty;
|
2024-03-23 16:13:52 +00:00
|
|
|
|
2024-03-23 15:05:04 +00:00
|
|
|
String getDescription(String desc) =>
|
|
|
|
desc.isEmpty ? "No description yet." : desc;
|
|
|
|
|
2024-03-23 16:13:52 +00:00
|
|
|
String getFileUrl(String fileId) =>
|
|
|
|
'https://co.solsynth.dev/api/attachments/o/$fileId';
|
|
|
|
|
2024-03-24 05:34:29 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
reactionCount = widget.item.reactionCount;
|
|
|
|
reactionList = widget.item.reactionList ?? <String, dynamic>{};
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-03-23 15:05:04 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-03-23 17:22:01 +00:00
|
|
|
return Column(
|
|
|
|
children: [
|
2024-03-24 04:12:13 +00:00
|
|
|
// Author info
|
2024-03-23 17:22:01 +00:00
|
|
|
Container(
|
|
|
|
color: Colors.grey[50],
|
|
|
|
child: ListTile(
|
2024-03-24 05:34:29 +00:00
|
|
|
title: Text(widget.item.author.name),
|
2024-03-23 17:22:01 +00:00
|
|
|
leading: CircleAvatar(
|
2024-03-24 05:34:29 +00:00
|
|
|
backgroundImage: NetworkImage(widget.item.author.avatar),
|
2024-03-23 17:22:01 +00:00
|
|
|
),
|
|
|
|
subtitle: Text(
|
2024-03-24 05:34:29 +00:00
|
|
|
getDescription(widget.item.author.description),
|
2024-03-23 17:22:01 +00:00
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
softWrap: false,
|
2024-03-23 15:05:04 +00:00
|
|
|
),
|
|
|
|
),
|
2024-03-23 17:22:01 +00:00
|
|
|
),
|
2024-03-24 04:12:13 +00:00
|
|
|
// Content
|
2024-03-23 17:22:01 +00:00
|
|
|
Markdown(
|
2024-03-24 05:34:29 +00:00
|
|
|
data: widget.item.content,
|
2024-03-23 17:22:01 +00:00
|
|
|
shrinkWrap: true,
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
),
|
2024-03-24 04:12:13 +00:00
|
|
|
// Attachments view
|
2024-03-23 17:22:01 +00:00
|
|
|
hasAttachments()
|
|
|
|
? Container(
|
|
|
|
decoration: const BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
top: BorderSide(width: 0.3, color: Color(0xffdedede))),
|
|
|
|
),
|
|
|
|
child: FlutterCarousel(
|
|
|
|
options: CarouselOptions(
|
|
|
|
height: 240.0,
|
2024-03-24 03:20:59 +00:00
|
|
|
viewportFraction: 1.0,
|
2024-03-23 17:22:01 +00:00
|
|
|
showIndicator: true,
|
|
|
|
slideIndicator: const CircularSlideIndicator(),
|
2024-03-23 16:13:52 +00:00
|
|
|
),
|
2024-03-24 05:34:29 +00:00
|
|
|
items: widget.item.attachments?.map((x) {
|
2024-03-23 17:22:01 +00:00
|
|
|
return Builder(
|
|
|
|
builder: (BuildContext context) {
|
2024-03-24 03:20:59 +00:00
|
|
|
return SizedBox(
|
2024-03-23 17:22:01 +00:00
|
|
|
width: MediaQuery.of(context).size.width,
|
|
|
|
child: InkWell(
|
|
|
|
child: Image.network(
|
|
|
|
getFileUrl(x.fileId),
|
|
|
|
fit: BoxFit.cover,
|
2024-03-23 16:13:52 +00:00
|
|
|
),
|
2024-03-23 17:22:01 +00:00
|
|
|
onTap: () {
|
|
|
|
Navigator.push(context,
|
|
|
|
MaterialPageRoute(builder: (_) {
|
|
|
|
return ImageLightbox(
|
|
|
|
url: getFileUrl(x.fileId),
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}).toList(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: Container(),
|
2024-03-24 04:12:13 +00:00
|
|
|
// Actions
|
|
|
|
Container(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
decoration: const BoxDecoration(
|
|
|
|
border:
|
|
|
|
Border(top: BorderSide(width: 0.3, color: Color(0xffdedede))),
|
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
TextButton.icon(
|
|
|
|
icon: const Icon(Icons.comment),
|
2024-03-24 05:34:29 +00:00
|
|
|
label: Text(widget.item.commentCount.toString()),
|
2024-03-24 04:12:13 +00:00
|
|
|
onPressed: () => viewComments(context),
|
2024-03-24 05:34:29 +00:00
|
|
|
),
|
|
|
|
TextButton.icon(
|
|
|
|
icon: const Icon(Icons.emoji_emotions),
|
|
|
|
label: Text(reactionCount.toString()),
|
|
|
|
style: TextButton.styleFrom(foregroundColor: Colors.teal),
|
|
|
|
onPressed: () => viewReactions(context),
|
|
|
|
),
|
2024-03-24 04:12:13 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-03-23 17:22:01 +00:00
|
|
|
],
|
2024-03-23 15:05:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|