Skip to content

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

fix: avoid info page jank (#2058)

Prediditd8b67fcfd018
diff --git a/lib/bean/card/network_img_layer.dart b/lib/bean/card/network_img_layer.dart
index 487e527f..5c0af200 100644
--- a/lib/bean/card/network_img_layer.dart
+++ b/lib/bean/card/network_img_layer.dart
@@ -15,6 +15,9 @@ class NetworkImgLayer extends StatelessWidget {
this.fadeInDuration,
this.quality,
this.origAspectRatio,
+ this.filterQuality = FilterQuality.high,
+ this.color,
+ this.colorBlendMode,
});
final String? src;
@@ -25,6 +28,9 @@ class NetworkImgLayer extends StatelessWidget {
final Duration? fadeInDuration;
final int? quality;
final double? origAspectRatio;
+ final FilterQuality filterQuality;
+ final Color? color;
+ final BlendMode? colorBlendMode;
@override
Widget build(BuildContext context) {
@@ -78,9 +84,12 @@ class NetworkImgLayer extends StatelessWidget {
fadeOutDuration ?? const Duration(milliseconds: 120),
fadeInDuration:
fadeInDuration ?? const Duration(milliseconds: 120),
- filterQuality: FilterQuality.high,
+ filterQuality: filterQuality,
+ color: color,
+ colorBlendMode: colorBlendMode,
errorListener: (e) {
- KazumiLogger().w("NetworkImage: network image load error", error: e);
+ KazumiLogger()
+ .w("NetworkImage: network image load error", error: e);
},
errorWidget: (BuildContext context, String url, Object error) =>
placeholder(context),
@@ -96,7 +105,10 @@ class NetworkImgLayer extends StatelessWidget {
height: height,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
- color: Theme.of(context).colorScheme.onInverseSurface.withValues(alpha: 0.4),
+ color: Theme.of(context)
+ .colorScheme
+ .onInverseSurface
+ .withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(type == 'avatar'
? 50
: type == 'emote'
diff --git a/lib/pages/info/info_controller.dart b/lib/pages/info/info_controller.dart
index 25e664cd..170c68d3 100644
--- a/lib/pages/info/info_controller.dart
+++ b/lib/pages/info/info_controller.dart
@@ -38,7 +38,8 @@ abstract class _InfoController with Store {
Future<void> queryBangumiInfoByID(int id, {String type = "init"}) async {
isLoading = true;
- await BangumiApi.getBangumiInfoByID(id).then((value) {
+ try {
+ final value = await BangumiApi.getBangumiInfoByID(id);
if (value != null) {
if (type == "init") {
bangumiItem = value;
@@ -54,9 +55,10 @@ abstract class _InfoController with Store {
bangumiItem.votesCount = value.votesCount;
}
collectController.updateLocalCollect(bangumiItem);
- isLoading = false;
}
- });
+ } finally {
+ isLoading = false;
+ }
}
Future<void> queryBangumiCommentsByID(int id, {int offset = 0}) async {
diff --git a/lib/pages/info/info_page.dart b/lib/pages/info/info_page.dart
index 152ae49f..2835474f 100644
--- a/lib/pages/info/info_page.dart
+++ b/lib/pages/info/info_page.dart
@@ -29,6 +29,9 @@ class InfoPage extends StatefulWidget {
}
class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
+ static const Duration _minimumBangumiInfoLoadingDuration =
+ Duration(milliseconds: 400);
+
/// Don't use modular singleton here. We may have multiple info pages.
/// Use a new instance of InfoController for each info page.
final InfoController infoController = InfoController();
@@ -48,14 +51,17 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
bool staffIsLoading = false;
bool staffQueryTimeout = false;
bool staffIsEmpty = false;
+ bool _showBangumiInfoSkeleton = false;
final inputBangumiIten = Modular.args.data as BangumiItem;
+ bool get _isShowingBangumiInfoSkeleton =>
+ infoController.isLoading || _showBangumiInfoSkeleton;
+
bool _needsBangumiInfoRefresh(BangumiItem bangumiItem) {
final votesCount = bangumiItem.votesCount;
- final missingVoteDistribution = votesCount.isEmpty ||
- bangumiItem.votes <= 0 ||
- votesCount.length < 10;
+ final missingVoteDistribution =
+ votesCount.isEmpty || bangumiItem.votes <= 0 || votesCount.length < 10;
return bangumiItem.summary == '' || missingVoteDistribution;
}
@@ -96,7 +102,8 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
staffIsEmpty = false;
});
try {
- await infoController.queryBangumiStaffsByID(infoController.bangumiItem.id);
+ await infoController
+ .queryBangumiStaffsByID(infoController.bangumiItem.id);
if (mounted) {
setState(() {
staffIsLoading = false;
@@ -159,12 +166,18 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
// We need the type parameter to determine whether to attach the new data to the old data
// We can't generally replace the old data with the new data, because the old data contains images url, update them will cause the image to reload and flicker
if (_needsBangumiInfoRefresh(infoController.bangumiItem)) {
- queryBangumiInfoByID(infoController.bangumiItem.id, type: 'attach');
+ _showBangumiInfoSkeleton = true;
+ queryBangumiInfoByID(
+ infoController.bangumiItem.id,
+ type: 'attach',
+ enforceMinimumLoadingDuration: true,
+ );
}
sourceTabController =
TabController(length: pluginsController.pluginList.length, vsync: this);
infoTabController = TabController(length: 5, vsync: this);
- showRating = GStorage.setting.get(SettingBoxKey.showRating, defaultValue: true);
+ showRating =
+ GStorage.setting.get(SettingBoxKey.showRating, defaultValue: true);
infoTabController.addListener(() {
int index = infoTabController.index;
if (index == 1 &&
@@ -203,12 +216,35 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
super.dispose();
}
- Future<void> queryBangumiInfoByID(int id, {String type = "init"}) async {
+ Future<void> queryBangumiInfoByID(
+ int id, {
+ String type = "init",
+ bool enforceMinimumLoadingDuration = false,
+ }) async {
+ final loadingStartedAt = DateTime.now();
try {
await infoController.queryBangumiInfoByID(id, type: type);
- setState(() {});
} catch (e) {
- KazumiLogger().e('InfoController: failed to query bangumi info by ID', error: e);
+ KazumiLogger()
+ .e('InfoController: failed to query bangumi info by ID', error: e);
+ } finally {
+ if (enforceMinimumLoadingDuration && mounted) {
+ await _waitForMinimumBangumiInfoLoadingDuration(loadingStartedAt);
+ }
+ if (mounted) {
+ setState(() {
+ _showBangumiInfoSkeleton = false;
+ });
+ }
+ }
+ }
+
+ Future<void> _waitForMinimumBangumiInfoLoadingDuration(
+ DateTime loadingStartedAt) async {
+ final elapsed = DateTime.now().difference(loadingStartedAt);
+ final remaining = _minimumBangumiInfoLoadingDuration - elapsed;
+ if (remaining > Duration.zero) {
+ await Future.delayed(remaining);
}
}
@@ -297,47 +333,19 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.pin,
background: Observer(builder: (context) {
+ final showBangumiInfoSkeleton =
+ _isShowingBangumiInfoSkeleton;
return Stack(
children: [
// No background image when loading to make loading looks better
- if (!infoController.isLoading)
+ if (!showBangumiInfoSkeleton)
Positioned.fill(
bottom: kTextTabBarHeight,
child: IgnorePointer(
- child: Opacity(
- opacity: 0.4,
- child: LayoutBuilder(
- builder: (context, boxConstraints) {
- return ImageFiltered(
- imageFilter: ImageFilter.blur(
- sigmaX: 15.0, sigmaY: 15.0),
- child: ShaderMask(
- shaderCallback: (Rect bounds) {
- return const LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [
- Colors.white,
- Colors.transparent,
- ],
- stops: [0.8, 1],
- ).createShader(bounds);
- },
- child: NetworkImgLayer(
- src: infoController.bangumiItem
- .images['large'] ??
- '',
- width: boxConstraints.maxWidth,
- height: boxConstraints.maxHeight,
- fadeInDuration: const Duration(
- milliseconds: 0),
- fadeOutDuration: const Duration(
- milliseconds: 0),
- ),
- ),
- );
- },
- ),
+ child: _InfoHeaderBackground(
+ imageUrl: infoController
+ .bangumiItem.images['large'] ??
+ '',
),
),
),
@@ -351,7 +359,7 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
16, kToolbarHeight, 16, 0),
child: BangumiInfoCardV(
bangumiItem: infoController.bangumiItem,
- isLoading: infoController.isLoading,
+ isLoading: showBangumiInfoSkeleton,
showRating: showRating,
),
),
@@ -375,6 +383,7 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
];
},
body: Observer(builder: (context) {
+ final showBangumiInfoSkeleton = _isShowingBangumiInfoSkeleton;
return InfoTabView(
tabController: infoTabController,
bangumiItem: infoController.bangumiItem,
@@ -390,7 +399,7 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
commentsList: infoController.commentsList,
characterList: infoController.characterList,
staffList: infoController.staffList,
- isLoading: infoController.isLoading,
+ isLoading: showBangumiInfoSkeleton,
);
}),
),
@@ -415,7 +424,9 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
showDragHandle: true,
context: context,
builder: (context) {
- return SourceSheet(tabController: sourceTabController, infoController: infoController);
+ return SourceSheet(
+ tabController: sourceTabController,
+ infoController: infoController);
},
);
},
@@ -425,3 +436,110 @@ class _InfoPageState extends State<InfoPage> with TickerProviderStateMixin {
);
}
}
+
+class _InfoHeaderBackground extends StatelessWidget {
+ const _InfoHeaderBackground({
+ required this.imageUrl,
+ });
+
+ static const double _downsample = 0.5;
+ static const double _blurSigma = 15.0;
+ static const double _opacity = 0.4;
+ static const double _edgeBleed = 32.0;
+ static const double _bottomFeatherHeight = 48.0;
+
+ final String imageUrl;
+
+ @override
+ Widget build(BuildContext context) {
+ if (imageUrl.isEmpty) {
+ return const SizedBox.shrink();
+ }
+
+ return LayoutBuilder(
+ builder: (context, constraints) {
+ final width = constraints.maxWidth;
+ final height = constraints.maxHeight;
+ if (width <= 0 || height <= 0) {
+ return const SizedBox.shrink();
+ }
+
+ final rasterWidth = width * _downsample;
+ final rasterHeight = (height + _edgeBleed) * _downsample;
+
+ final backgroundColor = Theme.of(context).scaffoldBackgroundColor;
+
+ return ClipRect(
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ ShaderMask(
+ shaderCallback: (bounds) {
+ return const LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ Colors.white,
+ Colors.transparent,
+ ],
+ stops: [0.8, 1],
+ ).createShader(bounds);
+ },
+ child: Align(
+ alignment: Alignment.topCenter,
+ child: RepaintBoundary(
+ child: Transform.scale(
+ scale: 1 / _downsample,
+ alignment: Alignment.topCenter,
+ filterQuality: FilterQuality.low,
+ child: SizedBox(
+ width: rasterWidth,
+ height: rasterHeight,
+ child: ImageFiltered(
+ imageFilter: ImageFilter.blur(
+ sigmaX: _blurSigma * _downsample,
+ sigmaY: _blurSigma * _downsample,
+ ),
+ child: NetworkImgLayer(
+ src: imageUrl,
+ width: rasterWidth,
+ height: rasterHeight,
+ fadeInDuration: Duration.zero,
+ fadeOutDuration: Duration.zero,
+ filterQuality: FilterQuality.low,
+ color: Colors.white.withValues(alpha: _opacity),
+ colorBlendMode: BlendMode.modulate,
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ Positioned(
+ left: 0,
+ right: 0,
+ bottom: 0,
+ height: _bottomFeatherHeight,
+ child: DecoratedBox(
+ decoration: BoxDecoration(
+ gradient: LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ backgroundColor.withValues(alpha: 0),
+ backgroundColor.withValues(alpha: 0.55),
+ backgroundColor,
+ ],
+ stops: const [0, 0.72, 1],
+ ),
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ },
+ );
+ }
+}