2020-08-31 19:56:48 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
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';
|
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-09-12 09:30:22 +02:00
|
|
|
final String instanceUrl;
|
2020-08-31 19:56:48 +02:00
|
|
|
final String text;
|
2020-09-16 20:09:09 +02:00
|
|
|
final bool selectable;
|
|
|
|
|
|
|
|
MarkdownText(this.text, {@required this.instanceUrl, this.selectable = false})
|
2020-09-12 09:30:22 +02:00
|
|
|
: assert(instanceUrl != null);
|
2020-08-31 19:56:48 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => MarkdownBody(
|
2020-09-16 20:09:09 +02:00
|
|
|
selectable: selectable,
|
2020-08-31 19:56:48 +02:00
|
|
|
data: text,
|
|
|
|
extensionSet: md.ExtensionSet.gitHubWeb,
|
|
|
|
onTapLink: (href) {
|
2020-09-12 17:05:52 +02:00
|
|
|
linkLauncher(context: context, url: href, instanceUrl: instanceUrl)
|
2020-08-31 19:56:48 +02:00
|
|
|
.catchError((e) => Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Row(
|
|
|
|
children: [
|
|
|
|
Icon(Icons.warning),
|
2020-09-12 09:30:22 +02:00
|
|
|
Text("couldn't open link, ${e.toString()}"),
|
2020-08-31 19:56:48 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
)));
|
|
|
|
},
|
2020-09-13 15:42:39 +02:00
|
|
|
imageBuilder: (uri, title, alt) => FullscreenableImage(
|
|
|
|
url: uri.toString(),
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: uri.toString(),
|
|
|
|
errorWidget: (context, url, error) => Row(
|
|
|
|
children: [
|
|
|
|
Icon(Icons.warning),
|
|
|
|
Text("couldn't load image, ${error.toString()}")
|
|
|
|
],
|
2020-09-11 19:29:17 +02:00
|
|
|
),
|
2020-08-31 19:56:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2020-08-31 12:22:29 +02:00
|
|
|
}
|