2021-03-09 08:32:14 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
2020-08-31 12:22:29 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
|
|
|
import 'package:markdown/markdown.dart' as md;
|
|
|
|
|
|
|
|
import '../url_launcher.dart';
|
2021-10-21 14:40:28 +02:00
|
|
|
import 'cached_network_image.dart';
|
2020-09-11 20:38:50 +02:00
|
|
|
import 'fullscreenable_image.dart';
|
2020-08-31 12:22:29 +02:00
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// A Markdown renderer with link/image handling
|
2020-08-31 19:56:48 +02:00
|
|
|
class MarkdownText extends StatelessWidget {
|
2020-12-31 14:58:23 +01:00
|
|
|
final String instanceHost;
|
2020-08-31 19:56:48 +02:00
|
|
|
final String text;
|
2020-09-16 20:09:09 +02:00
|
|
|
final bool selectable;
|
|
|
|
|
2021-01-03 18:21:56 +01:00
|
|
|
const MarkdownText(this.text,
|
2021-04-09 00:11:44 +02:00
|
|
|
{required this.instanceHost, this.selectable = false});
|
2020-08-31 19:56:48 +02:00
|
|
|
|
|
|
|
@override
|
2021-03-09 08:32:14 +01:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final theme = Theme.of(context);
|
2021-11-02 19:02:55 +01:00
|
|
|
|
2021-03-09 08:32:14 +01:00
|
|
|
return MarkdownBody(
|
2021-11-02 19:02:55 +01:00
|
|
|
key: ValueKey(Object.hashAll([selectable, text, instanceHost])),
|
2021-03-09 08:32:14 +01:00
|
|
|
selectable: selectable,
|
|
|
|
data: text,
|
|
|
|
extensionSet: md.ExtensionSet.gitHubWeb,
|
|
|
|
styleSheet: MarkdownStyleSheet.fromTheme(theme).copyWith(
|
|
|
|
blockquoteDecoration: BoxDecoration(
|
|
|
|
color: Colors.grey.withOpacity(0.3),
|
2021-09-12 22:37:07 +02:00
|
|
|
border: Border(
|
|
|
|
left: BorderSide(width: 2, color: theme.colorScheme.secondary),
|
|
|
|
),
|
2021-03-09 08:32:14 +01:00
|
|
|
),
|
|
|
|
code: theme.textTheme.bodyText1
|
|
|
|
// TODO: use a font from google fonts maybe? the defaults aren't very pretty
|
2021-04-09 00:11:44 +02:00
|
|
|
?.copyWith(fontFamily: Platform.isIOS ? 'Courier' : 'monospace'),
|
2021-03-09 08:32:14 +01:00
|
|
|
),
|
2021-04-05 20:14:39 +02:00
|
|
|
onTapLink: (text, href, title) {
|
2021-04-09 00:11:44 +02:00
|
|
|
if (href == null) return;
|
2021-03-09 08:32:14 +01:00
|
|
|
linkLauncher(context: context, url: href, instanceHost: instanceHost)
|
2021-03-10 08:34:30 +01:00
|
|
|
.catchError(
|
|
|
|
(e) => ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Row(
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.warning),
|
|
|
|
Text("couldn't open link, ${e.toString()}"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)));
|
2021-03-09 08:32:14 +01:00
|
|
|
},
|
|
|
|
imageBuilder: (uri, title, alt) => FullscreenableImage(
|
|
|
|
url: uri.toString(),
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: uri.toString(),
|
2021-10-21 14:40:28 +02:00
|
|
|
errorBuilder: (context, error) => Row(
|
2021-03-09 08:32:14 +01:00
|
|
|
children: [
|
|
|
|
const Icon(Icons.warning),
|
|
|
|
Text("couldn't load image, ${error.toString()}")
|
|
|
|
],
|
2020-08-31 19:56:48 +02:00
|
|
|
),
|
|
|
|
),
|
2021-03-09 08:32:14 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-08-31 12:22:29 +02:00
|
|
|
}
|