feat(app): 客户端展示多语言并支持筛选分页

This commit is contained in:
Zhou Weidong committed 2026-10-24 17:20:24 +08:00
1 parent 8481611372
commit 8b50b5bd06
68 files changed
+2742 -1120

No files matched your search

+89 -116
View File
@@ -14,20 +14,16 @@ import '../../../core/data/services/home_feed_controller.dart';
import '../../../core/data/services/wallet_controller.dart';
import '../../../core/theme/app_colors.dart';
import '../../../core/theme/app_dimens.dart';
import '../../../core/theme/app_theme.dart';
import '../../../core/widgets/app_pull_to_refresh.dart';
import '../../../core/widgets/drama_grid_skeleton.dart';
import '../../../core/widgets/empty_state.dart';
import '../../../core/widgets/paged_list_footer.dart';
import '../../../l10n/app_localizations.dart';
import '../../ranking/ranking_page.dart';
import 'home_drama_classification.dart';
import 'home_category_dramas.dart';
import 'home_feed_card.dart';
import 'home_grid_header.dart';
const double _loadMoreThreshold = 240;
const List<String> _costumeAliases = ['古装', 'costume', 'period'];
const List<String> _communityAliases = ['社群', 'community', 'komunitas'];
class HomeFeedGrid extends StatefulWidget {
const HomeFeedGrid({
@@ -46,21 +42,15 @@ class HomeFeedGrid extends StatefulWidget {
}
class _HomeFeedGridState extends State<HomeFeedGrid> {
HomePrimaryTab _selectedPrimary = HomePrimaryTab.featured;
int _sectionIndex = 0;
final PageController _pageController = PageController();
final Map<HomePrimaryTab, ScrollController> _scrollControllers = {
for (final tab in HomePrimaryTab.values) tab: ScrollController(),
};
final Map<HomePrimaryTab, String?> _selectedTags = {
for (final tab in HomePrimaryTab.values) tab: null,
};
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted && _pageController.hasClients) {
_pageController.jumpToPage(_selectedPrimary.index);
_pageController.jumpToPage(_sectionIndex);
}
});
}
@@ -68,9 +58,6 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
@override
void dispose() {
_pageController.dispose();
for (final controller in _scrollControllers.values) {
controller.dispose();
}
super.dispose();
}
@@ -81,17 +68,16 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
final catalog = context.watch<CatalogController>();
final auth = context.watch<AuthController>();
final wallet = context.watch<WalletController>();
final shortcuts = _categoryShortcuts(categories, l10n);
final shortcuts = _categoryShortcuts(
categories,
Localizations.localeOf(context).languageCode,
);
final walletBadge = auth.isSignedIn && wallet.snapshot != null
? wallet.balance
: null;
final ranking = _selectedPrimary == HomePrimaryTab.ranking;
return DecoratedBox(
key: const Key('home-grid'),
decoration: BoxDecoration(
color: ranking ? null : AppColors.appBackground,
gradient: ranking ? AppTheme.pageGradient : null,
),
decoration: const BoxDecoration(color: AppColors.appBackground),
child: SafeArea(
bottom: false,
child: Column(
@@ -100,8 +86,8 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
HomeGridHeader(
callbacks: widget.callbacks,
categories: shortcuts,
selectedPrimary: _selectedPrimary,
onPrimarySelected: _selectPrimary,
selectedIndex: _sectionIndex,
onSectionSelected: _selectSection,
walletBadge: walletBadge,
),
Expanded(
@@ -110,22 +96,29 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
builder: (context, _) => PageView.builder(
key: const Key('home-primary-page-view'),
controller: _pageController,
itemCount: HomePrimaryTab.values.length,
onPageChanged: (index) => setState(
() => _selectedPrimary = HomePrimaryTab.values[index],
),
itemBuilder: (context, index) => _KeepAlivePage(
key: PageStorageKey(
'home-primary-page-${HomePrimaryTab.values[index].name}',
),
child: _tabGrid(
context,
l10n,
tab: HomePrimaryTab.values[index],
catalog: catalog,
shortcuts: shortcuts,
),
),
itemCount: shortcuts.isEmpty ? 1 : shortcuts.length,
onPageChanged: (index) =>
setState(() => _sectionIndex = index),
itemBuilder: (context, index) {
if (shortcuts.isEmpty) {
return _categoryPlaceholder(
l10n,
context.watch<CategoryController>(),
);
}
final category = shortcuts[index];
return _KeepAlivePage(
key: PageStorageKey(
'home-primary-page-${category.categoryId}',
),
child: _tabGrid(
context,
l10n,
category: category,
catalog: catalog,
),
);
},
),
),
),
@@ -135,28 +128,54 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
);
}
void _selectPrimary(HomePrimaryTab tab) {
if (tab == _selectedPrimary) return;
void _selectSection(int index) {
if (index == _sectionIndex) return;
if (!_pageController.hasClients) {
setState(() => _selectedPrimary = tab);
setState(() => _sectionIndex = index);
return;
}
unawaited(
_pageController.animateToPage(
tab.index,
index,
duration: AppDimens.homePrimaryTabAnimationDuration,
curve: Curves.easeOut,
),
);
}
Widget _categoryPlaceholder(
AppLocalizations l10n,
CategoryController controller,
) {
if (controller.loading) {
return const DramaGridSkeleton(
key: Key('home-grid-skeleton'),
columns: 3,
horizontalPadding: AppDimens.feedPageHorizontal,
crossAxisSpacing: AppDimens.homeGridGap,
mainAxisSpacing: AppDimens.homeGridRowGap,
);
}
return EmptyState(
key: const Key('home-grid-empty'),
icon: Icons.movie_filter_outlined,
title: l10n.emptyContent,
);
}
Widget _tabGrid(
BuildContext context,
AppLocalizations l10n, {
required HomePrimaryTab tab,
required HomeGridCategoryShortcut category,
required CatalogController catalog,
required List<HomeGridCategoryShortcut> shortcuts,
}) {
final controller = context.read<CategoryController>();
if (controller.dramaPageLoader != null && category.categoryId.isNotEmpty) {
return HomeCategoryDramas(
lockedCategoryId: category.categoryId,
onOpenDrama: widget.onOpenDrama,
);
}
if (catalog.error != null && widget.feedController.dramas.isEmpty) {
return _catalogErrorState(l10n, catalog);
}
@@ -170,11 +189,7 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
mainAxisSpacing: AppDimens.homeGridRowGap,
);
}
if (tab == HomePrimaryTab.ranking) {
return const RankingPage(embedded: true);
}
final tag = _selectedTags[tab];
final dramas = _dramasFor(tab, tag, shortcuts);
final dramas = _dramasFor(category);
if (widget.feedController.loadingFirstPage && dramas.isEmpty) {
return const DramaGridSkeleton(
key: Key('home-grid-skeleton'),
@@ -201,7 +216,7 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
onRefresh: _refresh,
child: CustomScrollView(
key: const Key('home-grid-scroll'),
controller: _scrollControllers[tab],
controller: null,
physics: AppPullToRefresh.physics,
slivers: [
if (dramas.isEmpty)
@@ -237,6 +252,8 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
padding: const EdgeInsets.only(bottom: AppDimens.spaceXl),
child: PagedListFooter(
loading: widget.feedController.loadingMore,
hasMore: widget.feedController.hasMore,
hasItems: true,
errorMessage: widget.feedController.lastError == null
? null
: l10n.catalogError,
@@ -289,75 +306,31 @@ class _HomeFeedGridState extends State<HomeFeedGrid> {
List<HomeGridCategoryShortcut> _categoryShortcuts(
List<ContentCategory> categories,
AppLocalizations l10n,
String languageCode,
) {
final costume = _findCategory(categories, _costumeAliases);
final community = _findCategory(categories, _communityAliases);
return [
HomeGridCategoryShortcut(
label: l10n.homeSectionCostume,
categoryName: costume?.name ?? '',
tab: HomePrimaryTab.costume,
),
HomeGridCategoryShortcut(
label: l10n.homeSectionCommunity,
categoryName: community?.name ?? '',
tab: HomePrimaryTab.community,
),
for (final category in categories)
HomeGridCategoryShortcut(
label: category.localizedName(languageCode),
categoryName: category.name,
categoryId: category.id,
tab: HomePrimaryTab.categories,
),
];
}
ContentCategory? _findCategory(
List<ContentCategory> categories,
List<String> aliases,
) {
for (final category in categories) {
final name = category.name.toLowerCase();
if (aliases.any(name.contains)) return category;
}
return null;
}
List<Drama> _dramasFor(
HomePrimaryTab tab,
String? tag,
List<HomeGridCategoryShortcut> shortcuts,
) {
Iterable<Drama> source = widget.feedController.dramas;
if (tab == HomePrimaryTab.costume || tab == HomePrimaryTab.community) {
final categoryName = shortcuts
.firstWhere((shortcut) => shortcut.tab == tab)
.categoryName
.toLowerCase();
if (categoryName.isEmpty) return const [];
source = source.where(
(drama) =>
drama.category.toLowerCase().contains(categoryName) ||
drama.genre.toLowerCase().contains(categoryName),
);
}
if (tag != null) {
source = source.where((drama) => matchesHomeTag(drama, tag));
}
final dramas = source.toList(growable: false);
switch (tab) {
case HomePrimaryTab.newReleases:
return [...dramas]..sort((a, b) {
final left = publishedAtForDrama(a);
final right = publishedAtForDrama(b);
if (left == null && right == null) return b.id.compareTo(a.id);
if (left == null) return 1;
if (right == null) return -1;
return right.compareTo(left);
});
case HomePrimaryTab.ranking:
return [...dramas]..sort((a, b) => b.viewCount.compareTo(a.viewCount));
case HomePrimaryTab.featured:
case HomePrimaryTab.categories:
case HomePrimaryTab.costume:
case HomePrimaryTab.community:
return dramas;
}
List<Drama> _dramasFor(HomeGridCategoryShortcut category) {
final dramas = widget.feedController.dramas;
final categoryName = category.categoryName.toLowerCase();
if (categoryName.isEmpty) return dramas;
final matched = dramas
.where(
(drama) =>
drama.category.toLowerCase().contains(categoryName) ||
drama.genre.toLowerCase().contains(categoryName),
)
.toList(growable: false);
return matched.isEmpty ? dramas : matched;
}
Widget _emptyState(AppLocalizations l10n) {