Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

fix(images): unify character portrait loading

Predidit495bf11ae1d9
diff --git a/lib/bean/card/network_img_layer.dart b/lib/bean/card/network_img_layer.dart
index dfbf0b0f..506111bc 100644
--- a/lib/bean/card/network_img_layer.dart
+++ b/lib/bean/card/network_img_layer.dart
@@ -1,8 +1,8 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
+import 'package:kazumi/services/logging/logger.dart';
import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/utils/image_extension.dart';
-import 'package:kazumi/services/logging/logger.dart';
class NetworkImgLayer extends StatelessWidget {
const NetworkImgLayer({
@@ -10,10 +10,10 @@ class NetworkImgLayer extends StatelessWidget {
this.src,
required this.width,
required this.height,
+ this.fit = BoxFit.cover,
this.type,
this.fadeOutDuration,
this.fadeInDuration,
- this.quality,
this.origAspectRatio,
this.filterQuality = FilterQuality.high,
this.color,
@@ -23,10 +23,10 @@ class NetworkImgLayer extends StatelessWidget {
final String? src;
final double width;
final double height;
+ final BoxFit fit;
final String? type;
final Duration? fadeOutDuration;
final Duration? fadeInDuration;
- final int? quality;
final double? origAspectRatio;
final FilterQuality filterQuality;
final Color? color;
@@ -58,81 +58,78 @@ class NetworkImgLayer extends StatelessWidget {
@override
Widget build(BuildContext context) {
- final String imageUrl = src ?? '';
+ final imageUrl = src ?? '';
+ if (imageUrl.isEmpty) {
+ return _placeholder(context);
+ }
+
+ final (memCacheWidth, memCacheHeight) = _cacheSize(context);
+ return ClipRRect(
+ clipBehavior: Clip.antiAlias,
+ borderRadius: _borderRadius,
+ child: CachedNetworkImage(
+ imageUrl: imageUrl,
+ width: width,
+ height: height,
+ memCacheWidth: memCacheWidth,
+ memCacheHeight: memCacheHeight,
+ fit: fit,
+ fadeOutDuration: fadeOutDuration ?? const Duration(milliseconds: 120),
+ fadeInDuration: fadeInDuration ?? const Duration(milliseconds: 120),
+ filterQuality: filterQuality,
+ color: color,
+ colorBlendMode: colorBlendMode,
+ errorListener: (e) {
+ KazumiLogger().w("NetworkImage: network image load error", error: e);
+ },
+ errorWidget: (context, url, error) => _placeholder(context),
+ placeholder: (context, url) => _placeholder(context),
+ ),
+ );
+ }
- //// We need this to shink memory usage
- int? memCacheWidth, memCacheHeight;
- double aspectRatio = (width / height).toDouble();
+ (int?, int?) _cacheSize(BuildContext context) {
+ final cacheWidth = width.cacheSize(context);
+ final cacheHeight = height.cacheSize(context);
+ final aspectRatio = width / height;
+ final sourceAspectRatio = origAspectRatio;
- void setMemCacheSizes() {
- final sourceAspectRatio = origAspectRatio;
- if (sourceAspectRatio != null) {
- // BoxFit.cover needs the decoded image to fill the target's limiting
- // axis. A portrait source shown in a landscape box must therefore be
- // decoded by width, otherwise it is downscaled and enlarged again.
- if (sourceAspectRatio < aspectRatio) {
- memCacheWidth = width.cacheSize(context);
- } else if (sourceAspectRatio > aspectRatio) {
- memCacheHeight = height.cacheSize(context);
+ switch (fit) {
+ case BoxFit.none:
+ case BoxFit.scaleDown:
+ // These modes depend on the original image dimensions.
+ return (null, null);
+ case BoxFit.fill:
+ return (cacheWidth, cacheHeight);
+ case BoxFit.fitWidth:
+ return (cacheWidth, null);
+ case BoxFit.fitHeight:
+ return (null, cacheHeight);
+ case BoxFit.contain:
+ // A single decode axis preserves unknown source proportions.
+ return sourceAspectRatio != null && sourceAspectRatio > aspectRatio
+ ? (cacheWidth, null)
+ : (null, cacheHeight);
+ case BoxFit.cover:
+ // Decode enough pixels along the axis that fills the box.
+ if (sourceAspectRatio != null) {
+ if (sourceAspectRatio < aspectRatio) return (cacheWidth, null);
+ if (sourceAspectRatio > aspectRatio) return (null, cacheHeight);
} else {
- memCacheWidth = width.cacheSize(context);
- memCacheHeight = height.cacheSize(context);
+ if (aspectRatio > 1) return (null, cacheHeight);
+ if (aspectRatio < 1) return (cacheWidth, null);
}
- return;
- }
- if (aspectRatio > 1) {
- memCacheHeight = height.cacheSize(context);
- } else if (aspectRatio < 1) {
- memCacheWidth = width.cacheSize(context);
- } else {
- memCacheWidth = width.cacheSize(context);
- memCacheHeight = height.cacheSize(context);
- }
- }
-
- setMemCacheSizes();
-
- if (memCacheWidth == null && memCacheHeight == null) {
- memCacheWidth = width.toInt();
+ return (cacheWidth, cacheHeight);
}
-
- return src != '' && src != null
- ? ClipRRect(
- clipBehavior: Clip.antiAlias,
- borderRadius: BorderRadius.circular(
- type == 'avatar'
- ? 50
- : type == 'emote'
- ? 0
- : StyleString.imgRadius.x,
- ),
- child: CachedNetworkImage(
- imageUrl: imageUrl,
- width: width,
- height: height,
- memCacheWidth: memCacheWidth,
- memCacheHeight: memCacheHeight,
- fit: BoxFit.cover,
- fadeOutDuration:
- fadeOutDuration ?? const Duration(milliseconds: 120),
- fadeInDuration:
- fadeInDuration ?? const Duration(milliseconds: 120),
- filterQuality: filterQuality,
- color: color,
- colorBlendMode: colorBlendMode,
- errorListener: (e) {
- KazumiLogger()
- .w("NetworkImage: network image load error", error: e);
- },
- errorWidget: (BuildContext context, String url, Object error) =>
- placeholder(context),
- placeholder: (BuildContext context, String url) =>
- placeholder(context),
- ))
- : placeholder(context);
}
- Widget placeholder(BuildContext context) {
+ BorderRadius get _borderRadius => BorderRadius.circular(switch (type) {
+ 'avatar' => 50,
+ 'emote' => 0,
+ _ => StyleString.imgRadius.x,
+ });
+
+ Widget _placeholder(BuildContext context) {
return Container(
width: width,
height: height,
@@ -142,11 +139,7 @@ class NetworkImgLayer extends StatelessWidget {
.colorScheme
.onInverseSurface
.withValues(alpha: 0.4),
- borderRadius: BorderRadius.circular(type == 'avatar'
- ? 50
- : type == 'emote'
- ? 0
- : StyleString.imgRadius.x),
+ borderRadius: _borderRadius,
),
child: type == 'bg'
? const SizedBox()
diff --git a/lib/pages/info/character_info_view.dart b/lib/pages/info/character_info_view.dart
index f3fc2b7b..6ab0311e 100644
--- a/lib/pages/info/character_info_view.dart
+++ b/lib/pages/info/character_info_view.dart
@@ -1,13 +1,12 @@
-import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
+import 'package:kazumi/bean/card/network_img_layer.dart';
import 'package:kazumi/bean/dialog/material_bottom_sheet.dart';
import 'package:kazumi/bean/widget/content_section.dart';
import 'package:kazumi/bean/widget/empty_state_widget.dart';
import 'package:kazumi/bean/widget/image_preview.dart';
import 'package:kazumi/bean/widget/tonal_card.dart';
import 'package:kazumi/modules/character/character_full_item.dart';
-import 'package:kazumi/utils/image_extension.dart';
class CharacterInfoView extends StatelessWidget {
const CharacterInfoView({
@@ -262,23 +261,11 @@ class _CharacterPortrait extends StatelessWidget {
padding: const EdgeInsets.fromLTRB(12, 12, 12, 0),
child: Hero(
tag: heroTag,
- child: CachedNetworkImage(
- imageUrl: imageUrl,
+ child: NetworkImgLayer(
+ src: imageUrl,
width: width - 24,
height: height,
- // Single-axis decoding and contain preserve extreme aspect ratios.
- memCacheHeight: height.cacheSize(context),
fit: BoxFit.contain,
- fadeInDuration: const Duration(milliseconds: 120),
- fadeOutDuration: const Duration(milliseconds: 120),
- placeholder: (context, _) => Center(
- child: CircularProgressIndicator(
- strokeWidth: 2,
- semanticsLabel: '正在加载人物图片',
- ),
- ),
- errorWidget: (context, url, error) => _imageMessage(
- context, Icons.broken_image_outlined, '图片加载失败'),
),
),
),
@@ -308,22 +295,4 @@ class _CharacterPortrait extends StatelessWidget {
),
);
}
-
- Widget _imageMessage(BuildContext context, IconData icon, String message) {
- final colors = Theme.of(context).colorScheme;
- return Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(icon, size: 32, color: colors.onSurfaceVariant),
- const SizedBox(height: 8),
- Text(message,
- style: Theme.of(context)
- .textTheme
- .bodyMedium
- ?.copyWith(color: colors.onSurfaceVariant)),
- ],
- ),
- );
- }
}