start parsing html
This commit is contained in:
parent
517ef59af7
commit
efc70fde5a
|
@ -1,4 +1,4 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:localization/localization.dart';
|
||||
import 'package:loris/business_logic/timeline/media.dart';
|
||||
|
||||
|
@ -29,8 +29,24 @@ class ImageAttachmentDisplay extends StatelessWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(children: [
|
||||
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
Image.network(model.url, width: double.infinity),
|
||||
AltText(text: model.description.toString())
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class AltText extends StatelessWidget {
|
||||
const AltText({
|
||||
required this.text,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
final String text;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SelectableText(
|
||||
text,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import 'package:localization/localization.dart';
|
|||
import 'package:loris/business_logic/account/account.dart';
|
||||
import 'package:loris/business_logic/timeline/media.dart';
|
||||
import 'package:loris/partials/media_attachment.dart';
|
||||
import 'package:loris/partials/post_text_renderer.dart';
|
||||
import '../business_logic/timeline/timeline.dart' as tl;
|
||||
|
||||
class Post extends StatefulWidget {
|
||||
|
@ -17,6 +18,7 @@ class _PostState extends State<Post> {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
DisplayName(account: widget.model.account),
|
||||
PostBody(
|
||||
|
@ -148,13 +150,9 @@ class _PostBodyState extends State<PostBody> {
|
|||
Visibility(
|
||||
visible: visible,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
RichText(
|
||||
textAlign: TextAlign.start,
|
||||
text: TextSpan(
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
text: widget.content),
|
||||
),
|
||||
PostTextRenderer(htmlRef: widget.content),
|
||||
MediaAttachments(models: widget.media),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
class PostTextRenderer extends StatelessWidget {
|
||||
const PostTextRenderer({
|
||||
required this.html,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
final String html;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
String text = html;
|
||||
text = handleParagraph(html);
|
||||
text = handleBreak(text);
|
||||
return SelectableText.rich(TextSpan(text: text, children: []));
|
||||
}
|
||||
}
|
||||
|
||||
String handleParagraph(String input) {
|
||||
RegExp opening = RegExp(r"<p>");
|
||||
RegExp closing = RegExp(r"</p>");
|
||||
|
||||
String out = input.replaceAll(closing, "\n\n").replaceAll(opening, "");
|
||||
if (out.endsWith("\n\n")) {
|
||||
out = out.substring(0, out.length - 2);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
String handleBreak(String input) {
|
||||
return input.replaceAll("<br>", "\n");
|
||||
}
|
Loading…
Reference in New Issue