Solian/lib/widgets/chat/chat_event_message.dart

97 lines
2.6 KiB
Dart
Raw Normal View History

2024-06-27 16:59:11 +00:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:get/get.dart';
import 'package:solian/models/event.dart';
import 'package:solian/widgets/attachments/attachment_list.dart';
import 'package:url_launcher/url_launcher_string.dart';
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(
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;
return Markdown(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
data: body.text,
selectable: true,
padding: const EdgeInsets.all(0),
onTapLink: (text, href, title) async {
if (href == null) return;
await launchUrlString(
href,
mode: LaunchMode.externalApplication,
);
},
).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);
}
}