swing_account/lib/pages/statistics_page.dart
2025-01-02 13:28:37 +08:00

122 lines
3.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import '../models/record.dart';
import '../services/database_service.dart';
import '../widgets/statistics/view_selector.dart';
import '../widgets/statistics/monthly_overview.dart';
import '../widgets/statistics/daily_chart.dart';
import '../widgets/statistics/category_chart.dart';
import '../widgets/statistics/daily_report.dart';
class StatisticsPage extends StatefulWidget {
const StatisticsPage({Key? key}) : super(key: key);
@override
State<StatisticsPage> createState() => _StatisticsPageState();
}
class _StatisticsPageState extends State<StatisticsPage> {
final _dbService = DatabaseService();
bool _isMonthView = true; // true为月视图false为年视图
DateTime _selectedDate = DateTime.now();
List<Record> _records = [];
@override
void initState() {
super.initState();
_loadRecords();
}
/// 加载记录数据
Future<void> _loadRecords() async {
DateTime startDate;
DateTime endDate;
if (_isMonthView) {
startDate = DateTime(_selectedDate.year, _selectedDate.month, 1);
endDate = DateTime(_selectedDate.year, _selectedDate.month + 1, 0, 23, 59, 59);
} else {
startDate = DateTime(_selectedDate.year, 1, 1);
endDate = DateTime(_selectedDate.year, 12, 31, 23, 59, 59);
}
final records = await _dbService.getRecordsByDateRange(startDate, endDate);
setState(() => _records = records);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('统计'),
),
body: Column(
children: [
// 视图选择器
ViewSelector(
isMonthView: _isMonthView,
selectedDate: _selectedDate,
onViewChanged: (isMonth) {
setState(() {
_isMonthView = isMonth;
_loadRecords();
});
},
onDateChanged: (date) {
setState(() {
_selectedDate = date;
_loadRecords();
});
},
),
// 数据详情
Expanded(
child: _isMonthView ? _buildMonthView() : _buildYearView(),
),
],
),
);
}
/// 构建月视图
Widget _buildMonthView() {
if (_records.isEmpty) {
return const Center(
child: Text('暂无数据'),
);
}
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: [
// 月度总览
MonthlyOverview(records: _records),
const SizedBox(height: 16),
// 每日统计图表
if (_hasValidRecords) ...[
DailyChart(records: _records),
const SizedBox(height: 16),
],
// 分类统计图表
if (_hasValidRecords) ...[
CategoryChart(records: _records),
const SizedBox(height: 16),
],
// 日报表
if (_hasValidRecords)
DailyReport(records: _records),
],
),
);
}
/// 构建年视图
Widget _buildYearView() {
return const Center(
child: Text('年度统计功能开发中...'),
);
}
/// 检查是否有有效记录
bool get _hasValidRecords => _records.isNotEmpty;
}