Files
Aq-Accounting-Flutter/lib/pages/bill/bill_statistics_page.dart
T
2026-09-23 15:33:59 +08:00

200 lines
6.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/network/api_exception.dart';
import '../../models/statistics_response.dart';
import '../../providers/statistics_provider.dart';
import '../../utils/amount_util.dart';
import '../../widgets/state_views.dart';
import 'widgets/category_pie_card.dart';
import 'widgets/category_rank_list.dart';
/// 统计页:按日/周/月切换 + 汇总 + 分类占比饼图 + 分类明细
///
/// 属于记账模块,从账单首页右上角进(不是顶层 Tab)。
class BillStatisticsPage extends ConsumerWidget {
const BillStatisticsPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final tab = ref.watch(statisticsTabProvider);
final statsAsync = ref.watch(statisticsProvider(tab));
return Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFFFFE5CC), Color(0xFFFFD4A8), Color(0xFFFFC08A)],
),
),
child: SafeArea(
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
children: [
_titleRow(context),
const SizedBox(height: 14),
_tabBar(tab, ref),
const SizedBox(height: 14),
_content(ref, statsAsync),
// 被 push 进来时底部没有导航栏,就不用留出避让高度
SizedBox(height: Navigator.of(context).canPop() ? 24 : 90),
],
),
),
),
);
}
// ---- 顶部标题(带返回,因为这个页面是 push 进来的) ----
Widget _titleRow(BuildContext context) {
return Row(
children: [
if (Navigator.of(context).canPop())
InkWell(
onTap: () => Navigator.of(context).pop(),
borderRadius: BorderRadius.circular(20),
child: const Padding(
padding: EdgeInsets.all(6),
child: Icon(Icons.arrow_back, size: 22, color: Color(0xFF5D4037)),
),
),
const Text(
'统计分析',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF5D4037),
),
),
],
);
}
// ---- 时间粒度切换 ----
Widget _tabBar(StatisticsTab current, WidgetRef ref) {
return Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.9),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: const Color(0xFFC8956E)),
),
child: Row(
children: [
_tabItem('按日', StatisticsTab.daily, current, ref),
_tabItem('按周', StatisticsTab.weekly, current, ref),
_tabItem('按月', StatisticsTab.monthly, current, ref),
],
),
);
}
Widget _tabItem(String text, StatisticsTab tab, StatisticsTab current, WidgetRef ref) {
final active = current == tab;
return Expanded(
child: GestureDetector(
onTap: () => ref.read(statisticsTabProvider.notifier).set(tab),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: active
? const LinearGradient(colors: [Color(0xFFC8956E), Color(0xFFF5D59E)])
: null,
),
child: Center(
child: Text(
text,
style: TextStyle(
fontSize: 14.5,
fontWeight: FontWeight.w600,
color: active ? const Color(0xFF5D4037) : const Color(0xFF8D6E63),
),
),
),
),
),
);
}
// ---- 内容区 ----
Widget _content(WidgetRef ref, AsyncValue<StatisticsResponse> statsAsync) {
return statsAsync.when(
loading: () => const LoadingView(),
error: (e, _) => ErrorView(
message: asApiException(e).message,
onRetry: () => ref.invalidate(statisticsProvider),
),
data: (stats) => Column(
children: [
_summaryCard(stats),
const SizedBox(height: 12),
if (stats.categoryStatistics.isEmpty)
const EmptyView(title: '暂无统计数据', tip: '所选时间范围内还没有账单')
else ...[
CategoryPieCard(stats: stats),
const SizedBox(height: 12),
CategoryRankList(stats: stats),
],
],
),
);
}
// ---- 汇总卡 ----
Widget _summaryCard(StatisticsResponse stats) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 18),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(18),
gradient: const LinearGradient(
colors: [Color(0xFFC8956E), Color(0xFFF5D59E), Color(0xFFFFD700)],
),
boxShadow: [
BoxShadow(
color: const Color(0xFF5D4037).withValues(alpha: 0.18),
blurRadius: 10,
offset: const Offset(0, 4),
),
],
),
child: Row(
children: [
_summaryItem('总收入', stats.totalIncome, const Color(0xFF1B5E20)),
_vDivider(),
_summaryItem('总支出', stats.totalExpense, const Color(0xFFB71C1C)),
_vDivider(),
_summaryItem('余额', stats.balance, const Color(0xFF3E2723)),
],
),
);
}
Widget _summaryItem(String label, double amount, Color color) {
return Expanded(
child: Column(
children: [
Text(
label,
style: const TextStyle(fontSize: 12, color: Color(0xFF4E342E)),
),
const SizedBox(height: 6),
Text(
'¥${formatAmount(amount)}',
style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold, color: color),
),
],
),
);
}
Widget _vDivider() =>
Container(width: 1, height: 26, color: const Color(0xFF5D4037).withValues(alpha: 0.25));
}