Files
ReelShort/lib/pages/ranking/ranking_page.dart
T

353 lines
12 KiB
Dart

// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../core/data/models/drama.dart';
import '../../core/data/services/catalog_controller.dart';
import '../../core/formatting/count_format.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/empty_state.dart';
import '../../core/widgets/plain_page_scaffold.dart';
import '../../core/widgets/remote_image.dart';
import '../../l10n/app_localizations.dart';
import '../drama_detail/drama_detail_view.dart';
const int _topCount = 50;
const double _podiumGap = 8;
const double _centerCoverWidth = 120;
const double _centerCoverHeight = 160;
const double _sideCoverWidth = 92;
const double _sideCoverHeight = 124;
const double _coverRadius = 12;
const double _titleSlotHeight = 24;
const double _rowGap = 12;
const double _rankColumnWidth = 56;
const double _listCoverWidth = 72;
const double _listCoverHeight = 96;
const Color _goldStart = Color(0xFFF3B34C);
const Color _goldEnd = Color(0xFFD9A23A);
const Color _bronzeStart = Color(0xFFCD7F32);
const Color _bronzeEnd = Color(0xFFA9713A);
/// 剧场榜单页:前三名横排领奖台,第 4 名起纵向列表。
class RankingPage extends StatelessWidget {
const RankingPage({super.key});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final dramas = [...context.read<CatalogController>().dramas]
..sort((a, b) => b.viewCount.compareTo(a.viewCount));
final top = dramas.take(_topCount).toList();
final podium = top.take(3).toList();
final rest = top.skip(3).toList();
return PlainPageScaffold(
pageKey: const Key('ranking-page'),
backKey: const Key('ranking-back'),
title: l10n.topCharts,
subtitle: l10n.rankingSubtitle,
child: top.isEmpty
? EmptyState(icon: Icons.leaderboard_outlined, title: l10n.noStories)
: ListView(
padding: const EdgeInsets.only(top: 4, bottom: 24),
children: [
if (podium.isNotEmpty)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 14),
child: _Podium(podium: podium, l10n: l10n),
),
if (rest.isNotEmpty) ...[
const SizedBox(height: 16),
for (var index = 0; index < rest.length; index++) ...[
if (index > 0) const SizedBox(height: _rowGap),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 14),
child: _RankingRow(
index: index + 3,
drama: rest[index],
l10n: l10n,
),
),
],
],
],
),
);
}
}
class _Podium extends StatelessWidget {
const _Podium({required this.podium, required this.l10n});
final List<Drama> podium;
final AppLocalizations l10n;
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
SizedBox(width: _sideCoverWidth, child: _slot(2)),
const SizedBox(width: _podiumGap),
SizedBox(width: _centerCoverWidth, child: _slot(1)),
const SizedBox(width: _podiumGap),
SizedBox(width: _sideCoverWidth, child: _slot(3)),
],
);
}
Widget _slot(int rank) {
if (podium.length < rank) return const SizedBox.shrink();
return _PodiumCard(rank: rank, drama: podium[rank - 1], l10n: l10n);
}
}
class _PodiumCard extends StatelessWidget {
const _PodiumCard({
required this.rank,
required this.drama,
required this.l10n,
});
final int rank;
final Drama drama;
final AppLocalizations l10n;
bool get _center => rank == 1;
double get _width => _center ? _centerCoverWidth : _sideCoverWidth;
double get _height => _center ? _centerCoverHeight : _sideCoverHeight;
LinearGradient get _badgeGradient => switch (rank) {
1 => const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [AppColors.blue, AppColors.bluePressed],
),
2 => const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [_goldStart, _goldEnd],
),
_ => const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [_bronzeStart, _bronzeEnd],
),
};
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => Navigator.of(context).push<void>(
MaterialPageRoute(builder: (_) => DramaDetailPage(drama: drama)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(_coverRadius),
child: SizedBox(
key: Key('ranking-cover-$rank'),
width: _width,
height: _height,
child: Stack(
fit: StackFit.expand,
children: [
RemoteImage(
drama.posterUrl,
fit: BoxFit.cover,
showFallbackIcon: true,
fallbackAspectRatio: 3 / 4,
),
Positioned(
top: 8,
left: 8,
child: Container(
key: Key('ranking-top-$rank'),
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 3,
),
decoration: BoxDecoration(
gradient: _badgeGradient,
borderRadius: BorderRadius.circular(999),
),
child: Text(
l10n.topLabel(rank),
maxLines: 1,
style: const TextStyle(
color: Colors.white,
fontSize: 11,
fontWeight: FontWeight.w800,
),
),
),
),
],
),
),
),
const SizedBox(height: 6),
SizedBox(
height: _titleSlotHeight,
child: Center(
child: Text(
drama.title,
key: Key('ranking-title-$rank'),
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: TextStyle(
color: AppColors.onSurface,
fontSize: _center ? 15 : 13,
fontWeight: _center ? FontWeight.w800 : FontWeight.w700,
),
),
),
),
],
),
);
}
}
class _RankingRow extends StatelessWidget {
const _RankingRow({
required this.index,
required this.drama,
required this.l10n,
});
final int index;
final Drama drama;
final AppLocalizations l10n;
int get _rank => index + 1;
@override
Widget build(BuildContext context) {
final genre = drama.genre.isEmpty ? drama.category : drama.genre;
return Container(
decoration: BoxDecoration(
color: AppColors.card,
borderRadius: BorderRadius.circular(14),
boxShadow: const [AppTheme.cardShadow],
),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(14),
clipBehavior: Clip.antiAlias,
child: InkWell(
key: Key('ranking-row-$index'),
onTap: () => Navigator.of(context).push<void>(
MaterialPageRoute(builder: (_) => DramaDetailPage(drama: drama)),
),
child: Padding(
padding: const EdgeInsets.all(10),
child: Row(
children: [
SizedBox(
width: _rankColumnWidth,
child: Text(
l10n.topLabel(_rank),
key: Key('ranking-top-$_rank'),
textAlign: TextAlign.center,
maxLines: 1,
style: const TextStyle(
color: AppColors.subtle,
fontSize: 18,
fontWeight: FontWeight.w800,
),
),
),
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: SizedBox(
width: _listCoverWidth,
height: _listCoverHeight,
child: RemoteImage(
drama.posterUrl,
fit: BoxFit.cover,
showFallbackIcon: true,
fallbackAspectRatio: 3 / 4,
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
drama.title,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: AppColors.onSurface,
fontSize: 15,
fontWeight: FontWeight.w700,
),
),
if (genre.isNotEmpty) ...[
const SizedBox(height: 6),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: AppColors.raised,
borderRadius: BorderRadius.circular(6),
),
child: Text(
genre,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: AppColors.muted,
fontSize: 11,
),
),
),
],
const SizedBox(height: 8),
Row(
children: [
const Icon(
Icons.local_fire_department_rounded,
color: AppColors.blue,
size: 12,
),
const SizedBox(width: 4),
Flexible(
child: Text(
l10n.heatLabel(
formatInteractionCount(drama.viewCount),
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: AppColors.muted,
fontSize: 11,
),
),
),
],
),
],
),
),
],
),
),
),
),
);
}
}