Skip to content

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

fix(collect): smooth category tab transitions

Prediditba21fe35becd
diff --git a/lib/pages/collect/collect_library_controls.dart b/lib/pages/collect/collect_library_controls.dart
index 55ad8f5f..9a3eeaf7 100644
--- a/lib/pages/collect/collect_library_controls.dart
+++ b/lib/pages/collect/collect_library_controls.dart
@@ -1,165 +1,57 @@
part of 'collect_library_view.dart';
-const _collectCategories = <CollectType?>[
- null,
- CollectType.watching,
- CollectType.planToWatch,
- CollectType.watched,
- CollectType.onHold,
- CollectType.abandoned,
-];
-
-class _CollectCategoryPager extends StatefulWidget {
- const _CollectCategoryPager({
- required this.selectedIndex,
- required this.onChanged,
- required this.itemCount,
- required this.itemBuilder,
- });
-
- final int selectedIndex;
- final ValueChanged<int> onChanged;
- final int itemCount;
- final IndexedWidgetBuilder itemBuilder;
-
- @override
- State<_CollectCategoryPager> createState() => _CollectCategoryPagerState();
-}
-
-class _CollectCategoryPagerState extends State<_CollectCategoryPager> {
- late final _controller = PageController(
- initialPage: widget.selectedIndex,
- keepPage: false,
- );
-
- @override
- void didUpdateWidget(covariant _CollectCategoryPager oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (widget.selectedIndex != oldWidget.selectedIndex &&
- _controller.hasClients &&
- _controller.page?.round() != widget.selectedIndex) {
- // Tab taps jump; swipe callbacks keep the current animation.
- _controller.jumpToPage(widget.selectedIndex);
- }
- }
-
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) => PageView.builder(
- key: const ValueKey('collect-category-pages'),
- controller: _controller,
- onPageChanged: widget.onChanged,
- itemCount: widget.itemCount,
- itemBuilder: widget.itemBuilder,
- scrollBehavior:
- ScrollConfiguration.of(context).copyWith(scrollbars: false),
- );
-}
-
-class _CollectCategories extends StatefulWidget {
+class _CollectCategories extends StatelessWidget {
const _CollectCategories({
- required this.selected,
+ super.key,
+ required this.controller,
required this.count,
- required this.onSelected,
});
- final CollectType? selected;
+ final TabController controller;
final int Function(CollectType?) count;
- final ValueChanged<CollectType?> onSelected;
-
- @override
- State<_CollectCategories> createState() => _CollectCategoriesState();
-}
-
-class _CollectCategoriesState extends State<_CollectCategories> {
- final _controller = ScrollController(keepScrollOffset: false);
- final _keys = {for (final type in _collectCategories) type: GlobalKey()};
-
- @override
- void initState() {
- super.initState();
- _revealSelected();
- }
-
- @override
- void didUpdateWidget(covariant _CollectCategories oldWidget) {
- super.didUpdateWidget(oldWidget);
- _revealSelected();
- }
-
- void _revealSelected() {
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (!mounted || !_controller.hasClients) return;
- final target = _keys[widget.selected]?.currentContext?.findRenderObject();
- if (target == null) return;
- // Scroll only the tabs, never the enclosing collection viewport.
- _controller.position.ensureVisible(
- target,
- alignment: 0.5,
- duration: MediaQuery.disableAnimationsOf(context)
- ? Duration.zero
- : const Duration(milliseconds: 220),
- curve: Curves.easeInOutCubicEmphasized,
- );
- });
- }
-
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colors = theme.colorScheme;
- return SingleChildScrollView(
- key: const ValueKey('collect-filter-strip'),
- controller: _controller,
- scrollDirection: Axis.horizontal,
- child: Row(
- spacing: 4,
- children: [
- for (final type in _collectCategories)
- Semantics(
- key: _keys[type],
- selected: type == widget.selected,
- inMutuallyExclusiveGroup: true,
- button: true,
- label: '${type?.label ?? '全部'},${widget.count(type)} 部',
+ return TabBar(
+ key: const PageStorageKey('collect-filter-strip'),
+ controller: controller,
+ isScrollable: true,
+ tabAlignment: TabAlignment.start,
+ dividerHeight: 0,
+ indicatorSize: TabBarIndicatorSize.tab,
+ indicatorPadding: const EdgeInsets.symmetric(horizontal: 2),
+ indicator: BoxDecoration(
+ color: colors.primaryContainer,
+ borderRadius: BorderRadius.circular(24),
+ ),
+ 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),
+ labelPadding: const EdgeInsets.symmetric(horizontal: 2),
+ splashBorderRadius: BorderRadius.circular(24),
+ tabs: [
+ for (final type in _collectCategories)
+ Tab(
+ height: 48,
+ child: Semantics(
+ label: '${type?.label ?? '全部'},${count(type)} 部',
excludeSemantics: true,
- onTap: () => widget.onSelected(type),
- child: TextButton(
- key: ValueKey('collect-filter-${type?.value ?? 'all'}'),
- onPressed: () => widget.onSelected(type),
- style: TextButton.styleFrom(
- minimumSize: const Size(64, 48),
+ child: ConstrainedBox(
+ constraints: const BoxConstraints(minWidth: 64),
+ child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
- backgroundColor: type == widget.selected
- ? colors.primaryContainer
- : Colors.transparent,
- foregroundColor: type == widget.selected
- ? colors.onPrimaryContainer
- : colors.onSurfaceVariant,
- textStyle: theme.textTheme.titleSmall?.copyWith(
- fontWeight: type == widget.selected
- ? FontWeight.w700
- : FontWeight.w500,
- ),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(24)),
+ child: Text(type?.label ?? '全部'),
),
- child: Text(type?.label ?? '全部'),
),
),
- ],
- ),
+ ),
+ ],
);
}
}
diff --git a/lib/pages/collect/collect_library_view.dart b/lib/pages/collect/collect_library_view.dart
index 5b0b8912..d59c08b8 100644
--- a/lib/pages/collect/collect_library_view.dart
+++ b/lib/pages/collect/collect_library_view.dart
@@ -11,6 +11,15 @@ import 'package:kazumi/pages/collect/collect_library_query.dart';
part 'collect_library_card.dart';
part 'collect_library_controls.dart';
+const _collectCategories = <CollectType?>[
+ null,
+ CollectType.watching,
+ CollectType.planToWatch,
+ CollectType.watched,
+ CollectType.onHold,
+ CollectType.abandoned,
+];
+
class CollectLibraryView extends StatefulWidget {
const CollectLibraryView({
super.key,
@@ -35,10 +44,15 @@ class CollectLibraryView extends StatefulWidget {
State<CollectLibraryView> createState() => _CollectLibraryViewState();
}
-class _CollectLibraryViewState extends State<CollectLibraryView> {
+class _CollectLibraryViewState extends State<CollectLibraryView>
+ with TickerProviderStateMixin {
final _searchController = TextEditingController();
final _searchFocus = FocusNode();
final _libraryFocus = FocusNode();
+ // Keep tab animations alive when the category scroll view is replaced.
+ final _categoriesKey = GlobalKey();
+ late TabController _tabController;
+ bool? _disableAnimations;
final _scrollControllers = {
for (final type in _collectCategories) type: ScrollController(),
};
@@ -48,6 +62,23 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
String _query = '';
bool _searchExpanded = false;
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ final disableAnimations = MediaQuery.disableAnimationsOf(context);
+ if (_disableAnimations == disableAnimations) return;
+ if (_disableAnimations != null) {
+ _tabController.dispose();
+ }
+ _disableAnimations = disableAnimations;
+ _tabController = TabController(
+ length: _collectCategories.length,
+ initialIndex: _collectCategories.indexOf(_selectedType),
+ animationDuration: disableAnimations ? Duration.zero : null,
+ vsync: this,
+ )..addListener(_onCategoryChanged);
+ }
+
@override
void didUpdateWidget(covariant CollectLibraryView oldWidget) {
super.didUpdateWidget(oldWidget);
@@ -56,6 +87,7 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
@override
void dispose() {
+ _tabController.dispose();
_searchController.dispose();
_searchFocus.dispose();
_libraryFocus.dispose();
@@ -66,7 +98,7 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
}
void _resetResults() {
- // Unmounted categories must forget their previous layout's offsets too.
+ // Reset saved offsets for unmounted categories too.
_resultsStorage = PageStorageBucket();
for (final controller in _scrollControllers.values) {
if (controller.hasClients) controller.jumpTo(0);
@@ -99,7 +131,8 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
});
}
- void _selectType(CollectType? type) {
+ void _onCategoryChanged() {
+ final type = _collectCategories[_tabController.index];
if (type == _selectedType) return;
_libraryFocus.requestFocus();
setState(() => _selectedType = type);
@@ -142,17 +175,15 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
child: header,
),
Expanded(
- child: _CollectCategoryPager(
- selectedIndex:
- _collectCategories.indexOf(_selectedType),
- onChanged: (index) =>
- _selectType(_collectCategories[index]),
- itemCount: _collectCategories.length,
- itemBuilder: (context, index) => HeroMode(
- enabled: _collectCategories[index] == _selectedType,
- child: _results(query, _collectCategories[index],
- inset: inset),
- ),
+ child: TabBarView(
+ controller: _tabController,
+ children: [
+ for (final type in _collectCategories)
+ HeroMode(
+ enabled: type == _selectedType,
+ child: _results(query, type, inset: inset),
+ ),
+ ],
),
),
],
@@ -172,9 +203,9 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
children: [
Expanded(
child: _CollectCategories(
- selected: _selectedType,
+ key: _categoriesKey,
+ controller: _tabController,
count: query.count,
- onSelected: _selectType,
),
),
if (!compact)
@@ -276,10 +307,9 @@ class _CollectLibraryViewState extends State<CollectLibraryView> {
Widget _results(CollectLibraryQuery query, CollectType? type,
{required double inset, Widget? header}) {
- final entries = query.results(type, _sort);
return LayoutBuilder(builder: (context, constraints) {
+ final entries = query.results(type, _sort);
final contentWidth = constraints.maxWidth - inset * 2;
- // Keep the viewport full-width; only the slivers receive content insets.
return ScrollbarTheme(
data: ScrollbarTheme.of(context).copyWith(crossAxisMargin: 0),
child: CustomScrollView(