From 0b8a5a33036c3978a4d3c0a08ac99da8a5809b64 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 14 Oct 2024 00:03:45 +0800 Subject: [PATCH] :lipstick: Optimize share as image --- assets/locales/en_us.json | 2 +- lib/widgets/posts/post_action.dart | 19 +++++++++++++++++-- lib/widgets/posts/post_item.dart | 26 +++++++++++++++++--------- lib/widgets/posts/post_share.dart | 23 +++++++++++++++++------ pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/assets/locales/en_us.json b/assets/locales/en_us.json index c51d4b8..54e865d 100644 --- a/assets/locales/en_us.json +++ b/assets/locales/en_us.json @@ -484,5 +484,5 @@ "authMaximumAuthStepsDesc": "The maximum number of authentication steps when logging in, higher value is more secure, lower value is more convenient; default is 2", "auditLog": "Audit log", "shareImage": "Share as image", - "shareImageFooter": "See more interesting posts on Solar Network" + "shareImageFooter": "Only on the Solar Network" } diff --git a/lib/widgets/posts/post_action.dart b/lib/widgets/posts/post_action.dart index e1a2feb..27201e3 100644 --- a/lib/widgets/posts/post_action.dart +++ b/lib/widgets/posts/post_action.dart @@ -89,10 +89,25 @@ class _PostActionState extends State { } Future _shareImage() async { + final List attachments = widget.item.body['attachments'] is List + ? List.from(widget.item.body['attachments']?.whereType()) + : List.empty(); + final hasAttachment = attachments.isNotEmpty; + final screenshot = ScreenshotController(); - final image = await screenshot.captureFromWidget( - PostShareImage(item: widget.item), + final image = await screenshot.captureFromLongWidget( + MediaQuery( + data: MediaQuery.of(context), + child: PostShareImage(item: widget.item), + ), context: context, + pixelRatio: 2, + constraints: BoxConstraints( + minWidth: 480, + maxWidth: hasAttachment ? 480 : 640, + minHeight: 640, + maxHeight: double.infinity, + ), ); final directory = await getApplicationDocumentsDirectory(); final imageFile = await File( diff --git a/lib/widgets/posts/post_item.dart b/lib/widgets/posts/post_item.dart index ab4d749..1545ef8 100644 --- a/lib/widgets/posts/post_item.dart +++ b/lib/widgets/posts/post_item.dart @@ -31,6 +31,7 @@ class PostItem extends StatefulWidget { final bool isOverrideEmbedClickable; final bool isFullDate; final bool isContentSelectable; + final bool isNonScrollAttachment; final bool showFeaturedReply; final String? attachmentParent; @@ -49,6 +50,7 @@ class PostItem extends StatefulWidget { this.isOverrideEmbedClickable = false, this.isFullDate = false, this.isContentSelectable = false, + this.isNonScrollAttachment = false, this.showFeaturedReply = false, this.attachmentParent, this.padding, @@ -214,6 +216,7 @@ class _PostItemState extends State { _PostAttachmentWidget( item: item, padding: widget.padding, + isNonScrollAttachment: widget.isNonScrollAttachment, ), if (widget.showFeaturedReply) _PostFeaturedReplyWidget(item: item).paddingSymmetric( @@ -380,8 +383,13 @@ class _PostFeaturedReplyWidget extends StatelessWidget { class _PostAttachmentWidget extends StatelessWidget { final Post item; final EdgeInsets? padding; + final bool isNonScrollAttachment; - const _PostAttachmentWidget({required this.item, required this.padding}); + const _PostAttachmentWidget({ + required this.item, + required this.padding, + required this.isNonScrollAttachment, + }); @override Widget build(BuildContext context) { @@ -401,14 +409,6 @@ class _PostAttachmentWidget extends StatelessWidget { autoload: false, isFullWidth: true, ); - } else if (attachments.length == 1) { - return AttachmentList( - parentId: item.id.toString(), - attachmentIds: item.preload == null ? attachments : null, - attachments: item.preload?.attachments, - autoload: false, - isColumn: true, - ).paddingSymmetric(horizontal: (padding?.horizontal ?? 0) + 14); } else if (attachments.length > 1 && attachments.length % 3 == 0 && !isLargeScreen) { @@ -419,6 +419,14 @@ class _PostAttachmentWidget extends StatelessWidget { autoload: false, isGrid: true, ).paddingSymmetric(horizontal: (padding?.horizontal ?? 0) + 14); + } else if (attachments.length == 1 || isNonScrollAttachment) { + return AttachmentList( + parentId: item.id.toString(), + attachmentIds: item.preload == null ? attachments : null, + attachments: item.preload?.attachments, + autoload: false, + isColumn: true, + ).paddingSymmetric(horizontal: (padding?.horizontal ?? 0) + 14); } else { return AttachmentList( parentId: item.id.toString(), diff --git a/lib/widgets/posts/post_share.dart b/lib/widgets/posts/post_share.dart index 1741c4d..896755c 100644 --- a/lib/widgets/posts/post_share.dart +++ b/lib/widgets/posts/post_share.dart @@ -15,14 +15,15 @@ class PostShareImage extends StatelessWidget { Widget build(BuildContext context) { final textColor = Theme.of(context).colorScheme.onSurface.withOpacity(0.3); return RootContainer( - child: Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.center, + child: Wrap( + alignment: WrapAlignment.spaceBetween, + runAlignment: WrapAlignment.center, children: [ - const Gap(24), + const SizedBox(height: 40), Material( color: Colors.transparent, child: Card( + margin: EdgeInsets.zero, child: PostItem( item: item, isShowEmbed: true, @@ -30,11 +31,15 @@ class PostShareImage extends StatelessWidget { showFeaturedReply: false, isReactable: false, isShowReply: false, - padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8), + isNonScrollAttachment: true, + padding: const EdgeInsets.symmetric( + horizontal: 4, + vertical: 8, + ), onComment: () {}, ), ), - ), + ).paddingOnly(bottom: 24), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ @@ -80,6 +85,12 @@ class PostShareImage extends StatelessWidget { version: QrVersions.auto, padding: const EdgeInsets.all(4), size: 48, + dataModuleStyle: QrDataModuleStyle( + color: Theme.of(context).colorScheme.onSurface, + ), + eyeStyle: QrEyeStyle( + color: Theme.of(context).colorScheme.onSurface, + ), ), ), ), diff --git a/pubspec.lock b/pubspec.lock index c99b3fb..ac45a9b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -803,6 +803,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.23" + flutter_resizable_container: + dependency: "direct main" + description: + name: flutter_resizable_container + sha256: "5b15c79c6cc338ed79640c706bb5176baa3333d92fd3627ad279aa3e25d2f0e7" + url: "https://pub.dev" + source: hosted + version: "3.0.0" flutter_secure_storage: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 00085c6..6327033 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -87,6 +87,7 @@ dependencies: timeline_tile: ^2.0.0 screenshot: ^3.0.0 qr_flutter: ^4.1.0 + flutter_resizable_container: ^3.0.0 dev_dependencies: flutter_test: