Skip to content

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

perf(collect): avoid full library rebuilds and per-frame font churn on category tab switches

Predidit4d04b470bed5
diff --git a/lib/pages/collect/collect_library_controls.dart b/lib/pages/collect/collect_library_controls.dart
index c188145f..787f0711 100644
--- a/lib/pages/collect/collect_library_controls.dart
+++ b/lib/pages/collect/collect_library_controls.dart
@@ -4,16 +4,22 @@ class _CollectCategories extends StatelessWidget {
const _CollectCategories({
super.key,
required this.controller,
+ required this.selected,
required this.count,
});
final TabController controller;
+ final ValueListenable<CollectType?> selected;
final int Function(CollectType?) count;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colors = theme.colorScheme;
+ // Each lerped weight is a new font instance, so TabBar animates only the
+ // color and the selected label turns bold once per category change.
+ final labelStyle =
+ theme.textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w500);
return TabBar(
key: const PageStorageKey('collect-filter-strip'),
controller: controller,
@@ -29,10 +35,8 @@ class _CollectCategories extends StatelessWidget {
indicatorAnimation: TabIndicatorAnimation.elastic,
labelColor: colors.onPrimaryContainer,
unselectedLabelColor: colors.onSurfaceVariant,
- labelStyle:
- theme.textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w700),
- unselectedLabelStyle:
- theme.textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w500),
+ labelStyle: labelStyle,
+ unselectedLabelStyle: labelStyle,
labelPadding: const EdgeInsets.symmetric(horizontal: 2),
splashBorderRadius: BorderRadius.circular(24),
tabs: [
@@ -46,9 +50,15 @@ class _CollectCategories extends StatelessWidget {
constraints: const BoxConstraints(minWidth: 64),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
- child: Text(
- type?.label ?? '全部',
- textAlign: TextAlign.center,
+ child: ValueListenableBuilder(
+ valueListenable: selected,
+ builder: (context, value, _) => Text(
+ type?.label ?? '全部',
+ textAlign: TextAlign.center,
+ style: value == type
+ ? const TextStyle(fontWeight: FontWeight.w700)
+ : null,
+ ),
),
),
),
diff --git a/lib/pages/collect/collect_library_view.dart b/lib/pages/collect/collect_library_view.dart
index 6dc73c0a..b2af9dc8 100644
--- a/lib/pages/collect/collect_library_view.dart
+++ b/lib/pages/collect/collect_library_view.dart
@@ -1,3 +1,4 @@
+import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kazumi/bean/card/network_img_layer.dart';
@@ -58,11 +59,20 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
for (final type in _collectCategories) type: ScrollController(),
};
PageStorageBucket _resultsStorage = PageStorageBucket();
- CollectType? _selectedType = CollectType.watching;
CollectSort _sort = CollectSort.recentlyChanged;
String _query = '';
bool _searchExpanded = false;
+ // TabController also notifies when an animation ends; this only notifies on
+ // real category changes, and tab switches rebuild nothing but its listeners.
+ final _selectedType = ValueNotifier<CollectType?>(CollectType.watching);
+
+ @override
+ void initState() {
+ super.initState();
+ _selectedType.addListener(_libraryFocus.requestFocus);
+ }
+
@override
void didChangeDependencies() {
super.didChangeDependencies();
@@ -74,12 +84,15 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
_disableAnimations = disableAnimations;
_tabController = TabController(
length: _collectCategories.length,
- initialIndex: _collectCategories.indexOf(_selectedType),
+ initialIndex: _collectCategories.indexOf(_selectedType.value),
animationDuration: disableAnimations ? Duration.zero : null,
vsync: this,
- )..addListener(_onCategoryChanged);
+ )..addListener(_syncSelectedType);
}
+ void _syncSelectedType() =>
+ _selectedType.value = _collectCategories[_tabController.index];
+
@override
void didUpdateWidget(covariant CollectLibraryView oldWidget) {
super.didUpdateWidget(oldWidget);
@@ -89,6 +102,7 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
@override
void dispose() {
_tabController.dispose();
+ _selectedType.dispose();
_searchController.dispose();
_searchFocus.dispose();
_libraryFocus.dispose();
@@ -107,7 +121,7 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
}
void _focusSearch() {
- final controller = _scrollControllers[_selectedType]!;
+ final controller = _scrollControllers[_selectedType.value]!;
if (controller.hasClients) controller.jumpTo(0);
setState(() => _searchExpanded = true);
WidgetsBinding.instance.addPostFrameCallback((_) {
@@ -132,13 +146,6 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
});
}
- void _onCategoryChanged() {
- final type = _collectCategories[_tabController.index];
- if (type == _selectedType) return;
- _libraryFocus.requestFocus();
- setState(() => _selectedType = type);
- }
-
@override
Widget build(BuildContext context) {
final query = CollectLibraryQuery(widget.entries, _query);
@@ -180,8 +187,12 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
controller: _tabController,
children: [
for (final type in _collectCategories)
- HeroMode(
- enabled: type == _selectedType,
+ ValueListenableBuilder(
+ valueListenable: _selectedType,
+ builder: (context, selected, child) => HeroMode(
+ enabled: type == selected,
+ child: child!,
+ ),
child: _results(query, type, inset: inset),
),
],
@@ -189,7 +200,11 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
),
],
)
- : _results(query, _selectedType, inset: inset, header: header),
+ : ValueListenableBuilder(
+ valueListenable: _selectedType,
+ builder: (context, selected, _) =>
+ _results(query, selected, inset: inset, header: header),
+ ),
);
}),
),
@@ -206,18 +221,14 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
child: _CollectCategories(
key: _categoriesKey,
controller: _tabController,
+ selected: _selectedType,
count: query.count,
),
),
if (!compact)
Padding(
padding: const EdgeInsetsDirectional.only(start: 16),
- child: Text(
- '${query.count(_selectedType)} 部',
- style: Theme.of(context).textTheme.labelLarge?.copyWith(
- color: Theme.of(context).colorScheme.onSurfaceVariant,
- ),
- ),
+ child: _countLabel(query),
),
],
),
@@ -235,16 +246,7 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
),
)
else
- Expanded(
- child: Text(
- '${query.count(_selectedType)} 部',
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- style: Theme.of(context).textTheme.labelLarge?.copyWith(
- color: Theme.of(context).colorScheme.onSurfaceVariant,
- ),
- ),
- ),
+ Expanded(child: _countLabel(query)),
const SizedBox(width: 12),
_CollectSortMenu(
value: _sort,
@@ -278,6 +280,18 @@ class _CollectLibraryViewState extends State<CollectLibraryView>
);
}
+ Widget _countLabel(CollectLibraryQuery query) => ValueListenableBuilder(
+ valueListenable: _selectedType,
+ builder: (context, selected, _) => Text(
+ '${query.count(selected)} 部',
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: Theme.of(context).textTheme.labelLarge?.copyWith(
+ color: Theme.of(context).colorScheme.onSurfaceVariant,
+ ),
+ ),
+ );
+
Widget _searchBar() => Semantics(
label: '搜索收藏',
child: SearchBar(