From 3ae5c7c7f2c65feda5077e3546170d6c4df22140 Mon Sep 17 00:00:00 2001 From: nyne Date: Thu, 23 Jan 2025 19:08:38 +0800 Subject: [PATCH] Improve thumbnail --- lib/components/image.dart | 47 ++++++++++++++++++++++----------------- lib/pages/comic_page.dart | 27 +++++++++++----------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/lib/components/image.dart b/lib/components/image.dart index 4a95eb8..f46de4e 100644 --- a/lib/components/image.dart +++ b/lib/components/image.dart @@ -277,35 +277,38 @@ class _AnimatedImageState extends State if (_imageInfo != null) { if (widget.part != null) { - return CustomPaint( + result = CustomPaint( + isComplex: true, painter: ImagePainter( image: _imageInfo!.image, part: widget.part!, + fit: widget.fit ?? BoxFit.cover, ), child: SizedBox( width: widget.width, height: widget.height, ), ); + } else { + result = RawImage( + image: _imageInfo?.image, + width: widget.width, + height: widget.height, + debugImageLabel: _imageInfo?.debugLabel, + scale: _imageInfo?.scale ?? 1.0, + color: widget.color, + opacity: widget.opacity, + colorBlendMode: widget.colorBlendMode, + fit: BoxFit.cover, + alignment: widget.alignment, + repeat: widget.repeat, + centerSlice: widget.centerSlice, + matchTextDirection: widget.matchTextDirection, + invertColors: _invertColors, + isAntiAlias: widget.isAntiAlias, + filterQuality: widget.filterQuality, + ); } - result = RawImage( - image: _imageInfo?.image, - width: widget.width, - height: widget.height, - debugImageLabel: _imageInfo?.debugLabel, - scale: _imageInfo?.scale ?? 1.0, - color: widget.color, - opacity: widget.opacity, - colorBlendMode: widget.colorBlendMode, - fit: BoxFit.cover, - alignment: widget.alignment, - repeat: widget.repeat, - centerSlice: widget.centerSlice, - matchTextDirection: widget.matchTextDirection, - invertColors: _invertColors, - isAntiAlias: widget.isAntiAlias, - filterQuality: widget.filterQuality, - ); } else if (_lastException != null) { result = const Center( child: Icon(Icons.error), @@ -362,10 +365,13 @@ class ImagePainter extends CustomPainter { final ImagePart part; + final BoxFit fit; + /// Render a part of the image. const ImagePainter({ required this.image, this.part = const ImagePart(), + this.fit = BoxFit.cover, }); @override @@ -377,7 +383,8 @@ class ImagePainter extends CustomPainter { part.y2 ?? image.height.toDouble(), ), ); - final Rect dst = Offset.zero & size; + var fitted = applyBoxFit(fit, Size(src.width, src.height), size).destination; + var dst = Alignment.center.inscribe(fitted, Offset.zero & size); canvas.drawImageRect(image, src, dst, Paint()); } diff --git a/lib/pages/comic_page.dart b/lib/pages/comic_page.dart index b7ad999..d583b49 100644 --- a/lib/pages/comic_page.dart +++ b/lib/pages/comic_page.dart @@ -1283,7 +1283,9 @@ class _ComicThumbnailsState extends State<_ComicThumbnails> { y2 = double.parse(r.split('-')[1]); } } - } finally {} + } catch (_) { + // ignore + } part = ImagePart(x1: x1, y1: y1, x2: x2, y2: y2); } return Padding( @@ -1308,19 +1310,16 @@ class _ComicThumbnailsState extends State<_ComicThumbnails> { ), width: double.infinity, height: double.infinity, - child: ClipRRect( - borderRadius: - const BorderRadius.all(Radius.circular(16)), - child: AnimatedImage( - image: CachedImageProvider( - url, - sourceKey: state.widget.sourceKey, - ), - fit: BoxFit.contain, - width: double.infinity, - height: double.infinity, - part: part, + clipBehavior: Clip.antiAlias, + child: AnimatedImage( + image: CachedImageProvider( + url, + sourceKey: state.widget.sourceKey, ), + fit: BoxFit.contain, + width: double.infinity, + height: double.infinity, + part: part, ), ), ), @@ -1336,7 +1335,7 @@ class _ComicThumbnailsState extends State<_ComicThumbnails> { ), gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: 200, - childAspectRatio: 0.65, + childAspectRatio: 0.7, ), ), if (error != null)