lemmur-app-android/lib/widgets/markdown_text.dart

49 lines
1.6 KiB
Dart
Raw Normal View History

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' as md;
import '../url_launcher.dart';
import 'fullscreenable_image.dart';
2020-09-30 19:05:00 +02:00
/// A Markdown renderer with link/image handling
class MarkdownText extends StatelessWidget {
final String instanceHost;
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,
{@required this.instanceHost, this.selectable = false})
: assert(instanceHost != null);
@override
Widget build(BuildContext context) => MarkdownBody(
2020-09-16 20:09:09 +02:00
selectable: selectable,
data: text,
extensionSet: md.ExtensionSet.gitHubWeb,
onTapLink: (href) {
linkLauncher(context: context, url: href, instanceHost: instanceHost)
.catchError((e) => Scaffold.of(context).showSnackBar(SnackBar(
content: Row(
children: [
2021-01-03 19:43:39 +01:00
const Icon(Icons.warning),
Text("couldn't open link, ${e.toString()}"),
],
),
)));
},
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: [
2021-01-03 19:43:39 +01:00
const Icon(Icons.warning),
2020-09-13 15:42:39 +02:00
Text("couldn't load image, ${error.toString()}")
],
),
),
),
);
}