Solian/lib/widgets/chat/chat_event_message.dart

84 lines
2.3 KiB
Dart
Raw Normal View History

2024-06-27 16:59:11 +00:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/models/event.dart';
import 'package:solian/widgets/attachments/attachment_list.dart';
2024-07-11 16:44:57 +00:00
import 'package:solian/widgets/markdown_text_content.dart';
2024-06-27 16:59:11 +00:00
class ChatEventMessage extends StatelessWidget {
final Event item;
final bool isContentPreviewing;
final bool isQuote;
final bool isMerged;
final bool isHasMerged;
const ChatEventMessage({
super.key,
required this.item,
this.isContentPreviewing = false,
this.isMerged = false,
this.isHasMerged = false,
this.isQuote = false,
});
2024-06-27 18:49:28 +00:00
Widget buildAttachment(BuildContext context) {
final body = EventMessageBody.fromJson(item.body);
return SizedBox(
2024-06-29 10:19:52 +00:00
key: Key('m${item.uuid}attachments-box'),
2024-06-27 18:49:28 +00:00
width: min(MediaQuery.of(context).size.width, 640),
child: AttachmentList(
key: Key('m${item.uuid}attachments'),
parentId: item.uuid,
attachmentsId: body.attachments ?? List.empty(),
divided: true,
viewport: 1,
),
);
}
2024-06-27 16:59:11 +00:00
Widget buildContent() {
final body = EventMessageBody.fromJson(item.body);
final hasAttachment = body.attachments?.isNotEmpty ?? false;
2024-07-11 16:44:57 +00:00
return MarkdownTextContent(content: body.text).paddingOnly(
2024-06-27 18:49:28 +00:00
left: isQuote ? 0 : 12,
right: isQuote ? 0 : 12,
top: body.quoteEvent == null ? 2 : 0,
2024-06-27 16:59:11 +00:00
bottom: hasAttachment ? 4 : (isHasMerged ? 2 : 0),
);
}
Widget buildBody(BuildContext context) {
final body = EventMessageBody.fromJson(item.body);
2024-06-27 18:49:28 +00:00
if (isContentPreviewing) {
2024-06-27 16:59:11 +00:00
return buildContent();
} else if (isMerged) {
return Column(
2024-06-27 18:49:28 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2024-06-27 16:59:11 +00:00
children: [
buildContent().paddingOnly(left: 52),
if (body.attachments?.isNotEmpty ?? false)
2024-06-27 18:49:28 +00:00
buildAttachment(context).paddingOnly(left: 52, bottom: 4),
2024-06-27 16:59:11 +00:00
],
);
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
buildContent(),
if (body.attachments?.isNotEmpty ?? false)
2024-06-27 18:49:28 +00:00
buildAttachment(context).paddingOnly(bottom: 4),
2024-06-27 16:59:11 +00:00
],
);
}
}
@override
Widget build(BuildContext context) {
return buildBody(context);
}
}