[WIP] truncate long text posts

This commit is contained in:
ryg-git 2021-02-03 23:43:43 +05:30
parent 7e457a8ae8
commit b999d53aa8
1 changed files with 69 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import 'dart:ui' as ui;
import 'package:cached_network_image/cached_network_image.dart';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/cupertino.dart';
@ -97,11 +99,35 @@ class PostWidget extends HookWidget {
);
}
List<ui.LineMetrics> postNeedsToTruncate(
String body,
Size size,
ui.TextDirection textDirection,
) {
final span = TextSpan(
text: body,
);
final tp = TextPainter(
text: span,
maxLines: 20,
textDirection: textDirection,
)..layout(maxWidth: size.width - 20);
return tp.computeLineMetrics();
}
// == UI ==
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final textPostData = post.post.body == null
? []
: postNeedsToTruncate(
post.post.body,
MediaQuery.of(context).size,
Directionality.of(context),
);
void _openLink() => linkLauncher(
context: context, url: post.post.url, instanceHost: instanceHost);
@ -419,12 +445,54 @@ class PostWidget extends HookWidget {
postImage()
else if (post.post.url != null && post.post.url.isNotEmpty)
linkPreview(),
if (post.post.body != null)
if (post.post.body != null && fullPost)
// TODO: trim content
Padding(
padding: const EdgeInsets.all(10),
child:
MarkdownText(post.post.body, instanceHost: instanceHost)),
if (post.post.body != null && !fullPost && textPostData.length < 20)
// TODO: trim content
Padding(
padding: const EdgeInsets.all(10),
child:
MarkdownText(post.post.body, instanceHost: instanceHost)),
if (post.post.body != null &&
!fullPost &&
textPostData.length >= 20)
Container(
constraints: BoxConstraints(
maxHeight:
(textPostData.first as ui.LineMetrics).height * 20),
child: Stack(
alignment: Alignment.bottomCenter,
children: [
ClipRect(
child: Align(
alignment: Alignment.topCenter,
heightFactor: 0.8,
child: Padding(
padding: const EdgeInsets.all(10),
child: MarkdownText(post.post.body,
instanceHost: instanceHost)),
),
),
Container(
height: (textPostData.first as ui.LineMetrics).height * 4,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
theme.cardColor,
],
),
),
),
],
),
),
actions(),
],
),