import 'package:flutter/material.dart'; import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:html2md/html2md.dart' as html2md; import 'package:loris/business_logic/emoji/emoji.dart'; import 'package:loris/dialogues/profile_view.dart'; import 'package:url_launcher/url_launcher.dart'; import '../business_logic/account/account.dart' as account; class PostTextRenderer extends StatelessWidget { const PostTextRenderer({ required this.identityName, required this.input, this.emoji = const [], Key? key, }) : super(key: key); final String input; final String identityName; final List emoji; @override Widget build(BuildContext context) { final MarkdownStyleSheet mdStyle = MarkdownStyleSheet( codeblockDecoration: BoxDecoration( color: Theme.of(context).colorScheme.background, borderRadius: const BorderRadius.all(Radius.circular(8)), border: Border.all( color: Theme.of(context).colorScheme.secondary, style: BorderStyle.solid, ), ), code: TextStyle( decorationColor: Theme.of(context).backgroundColor, color: Theme.of(context).colorScheme.onBackground, backgroundColor: Colors.transparent, ), a: TextStyle(color: Theme.of(context).colorScheme.secondary), blockquote: TextStyle(color: Theme.of(context).colorScheme.onBackground), blockquoteDecoration: BoxDecoration( color: Theme.of(context).colorScheme.background, borderRadius: const BorderRadius.all(Radius.circular(8)), border: Border.all( color: Theme.of(context).colorScheme.secondary, style: BorderStyle.solid, ), ), ); String s = html2md.convert(input); for (var e in emoji) { s = insertEmojiInMd(s, e); } return MarkdownBody( imageBuilder: (uri, title, alt) { return Image.network( "${uri.scheme}://${uri.host}${uri.path}", height: Theme.of(context).textTheme.bodyMedium?.fontSize, ); }, onTapLink: ((text, href, title) async { if (href != null) { // see if this is an account and in that case search for it if (text.startsWith("@")) { final result = await account.searchModel(identityName, href); if (result.keys.first == 200) { showDialog( context: context, builder: (context) => ProfileView(model: result.values.first!), ); return; } } // if this is not an account or the account couldn't be found // then just open it in the default browser launchUrl(Uri.parse(href)); } }), styleSheet: mdStyle, data: s, selectable: true, ); } }