Post link expand

 Cache link expansion image
This commit is contained in:
LittleSheep 2024-08-19 19:56:44 +08:00
parent cf1cfecb08
commit e45d9b39d5
3 changed files with 29 additions and 11 deletions

View File

@ -1,6 +1,8 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/platform.dart';
import 'package:solian/providers/link_expander.dart'; import 'package:solian/providers/link_expander.dart';
import 'package:url_launcher/url_launcher_string.dart'; import 'package:url_launcher/url_launcher_string.dart';
@ -9,6 +11,12 @@ class LinkExpansion extends StatelessWidget {
const LinkExpansion({super.key, required this.content}); const LinkExpansion({super.key, required this.content});
Widget _buildImage(String url, {double? width, double? height}) {
return PlatformInfo.canCacheImage
? CachedNetworkImage(imageUrl: url, width: width, height: height)
: Image.network(url, width: width, height: height);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final linkRegex = RegExp( final linkRegex = RegExp(
@ -43,20 +51,17 @@ class LinkExpansion extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
if ([ if ([
snapshot.data!.icon != null && (snapshot.data!.icon?.isNotEmpty ?? false),
(snapshot.data!.icon?.startsWith('http') ?? false),
snapshot.data!.siteName != null snapshot.data!.siteName != null
].any((x) => x)) ].any((x) => x))
Row( Row(
children: [ children: [
if (snapshot.data!.icon != null && if (snapshot.data!.icon?.isNotEmpty ?? false)
(snapshot.data!.icon?.startsWith('http') ??
false))
ClipRRect( ClipRRect(
borderRadius: const BorderRadius.all( borderRadius: const BorderRadius.all(
Radius.circular(8), Radius.circular(8),
), ),
child: Image.network( child: _buildImage(
snapshot.data!.icon!, snapshot.data!.icon!,
width: 32, width: 32,
height: 32, height: 32,
@ -68,14 +73,17 @@ class LinkExpansion extends StatelessWidget {
style: Theme.of(context).textTheme.labelLarge, style: Theme.of(context).textTheme.labelLarge,
), ),
], ],
).paddingOnly(bottom: 8), ).paddingOnly(
bottom:
(snapshot.data!.icon?.isNotEmpty ?? false) ? 8 : 4,
),
if (snapshot.data!.image != null && if (snapshot.data!.image != null &&
(snapshot.data!.image?.startsWith('http') ?? false)) (snapshot.data!.image?.startsWith('http') ?? false))
ClipRRect( ClipRRect(
borderRadius: const BorderRadius.all( borderRadius: const BorderRadius.all(
Radius.circular(8), Radius.circular(8),
), ),
child: Image.network( child: _buildImage(
snapshot.data!.image!, snapshot.data!.image!,
), ),
).paddingOnly(bottom: 8), ).paddingOnly(bottom: 8),

View File

@ -33,10 +33,12 @@ class _AppNavigationDrawerState extends State<AppNavigationDrawer> {
final resp = await provider.getCurrentStatus(); final resp = await provider.getCurrentStatus();
final status = AccountStatus.fromJson(resp.body); final status = AccountStatus.fromJson(resp.body);
if (mounted) {
setState(() { setState(() {
_accountStatus = status; _accountStatus = status;
}); });
} }
}
void _closeDrawer() { void _closeDrawer() {
rootScaffoldKey.currentState!.closeDrawer(); rootScaffoldKey.currentState!.closeDrawer();

View File

@ -10,6 +10,7 @@ import 'package:solian/shells/title_shell.dart';
import 'package:solian/widgets/account/account_avatar.dart'; import 'package:solian/widgets/account/account_avatar.dart';
import 'package:solian/widgets/account/account_profile_popup.dart'; import 'package:solian/widgets/account/account_profile_popup.dart';
import 'package:solian/widgets/attachments/attachment_list.dart'; import 'package:solian/widgets/attachments/attachment_list.dart';
import 'package:solian/widgets/link_expansion.dart';
import 'package:solian/widgets/markdown_text_content.dart'; import 'package:solian/widgets/markdown_text_content.dart';
import 'package:solian/widgets/posts/post_tags.dart'; import 'package:solian/widgets/posts/post_tags.dart';
import 'package:solian/widgets/posts/post_quick_action.dart'; import 'package:solian/widgets/posts/post_quick_action.dart';
@ -349,6 +350,11 @@ class _PostItemState extends State<PostItem> {
), ),
], ],
), ),
LinkExpansion(content: item.body['content']).paddingOnly(
left: 8,
right: 8,
top: 4,
),
_buildFooter().paddingOnly(left: 16), _buildFooter().paddingOnly(left: 16),
if (attachments.isNotEmpty) if (attachments.isNotEmpty)
Row( Row(
@ -445,6 +451,8 @@ class _PostItemState extends State<PostItem> {
if (widget.item.repostTo != null && widget.isShowEmbed) if (widget.item.repostTo != null && widget.isShowEmbed)
_buildRepost(context).paddingOnly(top: 4), _buildRepost(context).paddingOnly(top: 4),
_buildFooter().paddingOnly(left: 12), _buildFooter().paddingOnly(left: 12),
LinkExpansion(content: item.body['content'])
.paddingOnly(top: 4),
], ],
), ),
), ),