From 5364aecf745cea2b524bc286ac9cf22bbff50052 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 10 Nov 2024 22:56:18 +0800 Subject: [PATCH] :zap: Optimize image cache size --- lib/main.dart | 5 +++ lib/widgets/attachment/attachment_item.dart | 12 ++++--- lib/widgets/navigation/app_background.dart | 35 ++++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 7186d3e..ab347d1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'package:croppy/croppy.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization_loader/easy_localization_loader.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:relative_time/relative_time.dart'; @@ -15,6 +16,10 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); await EasyLocalization.ensureInitialized(); + if (!kReleaseMode) { + debugInvertOversizedImages = true; + } + runApp(const SolianApp()); } diff --git a/lib/widgets/attachment/attachment_item.dart b/lib/widgets/attachment/attachment_item.dart index 8d05b81..ead8491 100644 --- a/lib/widgets/attachment/attachment_item.dart +++ b/lib/widgets/attachment/attachment_item.dart @@ -23,10 +23,14 @@ class AttachmentItem extends StatelessWidget { case 'image': return Hero( tag: 'attachment-${data.rid}-$heroTag', - child: UniversalImage( - sn.getAttachmentUrl(data.rid), - fit: BoxFit.cover, - ), + child: LayoutBuilder(builder: (context, constraints) { + return UniversalImage( + sn.getAttachmentUrl(data.rid), + fit: BoxFit.cover, + cacheHeight: constraints.maxHeight, + cacheWidth: constraints.maxWidth, + ); + }), ); default: return const Placeholder(); diff --git a/lib/widgets/navigation/app_background.dart b/lib/widgets/navigation/app_background.dart index 16d5e7a..2b2bbc0 100644 --- a/lib/widgets/navigation/app_background.dart +++ b/lib/widgets/navigation/app_background.dart @@ -10,6 +10,8 @@ class AppBackground extends StatelessWidget { @override Widget build(BuildContext context) { + final devicePixelRatio = MediaQuery.of(context).devicePixelRatio; + return ScaffoldMessenger( child: FutureBuilder( future: @@ -21,17 +23,28 @@ class AppBackground extends StatelessWidget { if (file.existsSync()) { return Container( color: Theme.of(context).colorScheme.surface, - child: Container( - decoration: BoxDecoration( - backgroundBlendMode: BlendMode.darken, - color: Theme.of(context).colorScheme.surface, - image: DecorationImage( - opacity: 0.2, - image: FileImage(file), - fit: BoxFit.cover, - ), - ), - child: child, + child: LayoutBuilder( + builder: (context, constraints) { + return Container( + decoration: BoxDecoration( + backgroundBlendMode: BlendMode.darken, + color: Theme.of(context).colorScheme.surface, + image: DecorationImage( + opacity: 0.2, + image: ResizeImage( + FileImage(file), + width: (constraints.maxWidth * devicePixelRatio) + .round(), + height: (constraints.maxHeight * devicePixelRatio) + .round(), + policy: ResizeImagePolicy.fit, + ), + fit: BoxFit.cover, + ), + ), + child: child, + ); + }, ), ); }