loris/lib/partials/post_text_renderer.dart

56 lines
1.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:html2md/html2md.dart' as html2md;
import 'package:url_launcher/url_launcher.dart';
class PostTextRenderer extends StatelessWidget {
const PostTextRenderer({
required this.identityName,
required this.input,
Key? key,
}) : super(key: key);
final String input;
final String identityName;
@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);
return MarkdownBody(
onTapLink: ((text, href, title) {
if (href != null) {
launchUrl(Uri.parse(href));
}
}),
styleSheet: mdStyle,
data: s,
selectable: true,
);
}
}