122 lines
3.3 KiB
Dart
122 lines
3.3 KiB
Dart
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;
|
||
} |