diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index 2518c92..aca2396 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -22,6 +22,7 @@ class _HomePageState extends State { bool _isNavigating = false; final _dbService = DatabaseService(); List records = []; + DateTime _selectedMonth = DateTime.now(); // 添加选中月份状态 // 定义摇晃检测的常量 static const double _shakeThreshold = 8.0; // 单次摇晃的加速度阈值 @@ -60,7 +61,7 @@ class _HomePageState extends State { _loadRecords(); } - /// 重置摇晃检测状态 + /// 重置摇��检测状态 void _resetShakeDetection() { _lastShakeTime = null; _recentXValues.clear(); @@ -73,7 +74,15 @@ class _HomePageState extends State { /// 加载记录 Future _loadRecords() async { - final loadedRecords = await _dbService.getAllRecords(); + // 计算选中月份的起始和结束日期 + final startDate = DateTime(_selectedMonth.year, _selectedMonth.month, 1); + final endDate = DateTime(_selectedMonth.year, _selectedMonth.month + 1, 0); + + final loadedRecords = await _dbService.getRecordsByDateRange( + startDate, + endDate, + ); + setState(() { records = loadedRecords; records.sort((a, b) => b.createTime.compareTo(a.createTime)); @@ -170,16 +179,83 @@ class _HomePageState extends State { await _loadRecords(); } + // 添加月份选择器组件 + Widget _buildMonthSelector() { + return GestureDetector( + onTap: () => _showMonthPicker(), + child: Container( + padding: const EdgeInsets.all(16.0), + decoration: BoxDecoration( + color: Theme.of(context).primaryColor.withOpacity(0.1), + borderRadius: BorderRadius.circular(8.0), + ), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text( + '${_selectedMonth.year}年${_selectedMonth.month}月', + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.bold, + ), + ), + const Icon(Icons.arrow_drop_down), + ], + ), + ), + ); + } + + // 显示月份选择器 + Future _showMonthPicker() async { + final DateTime? picked = await showDatePicker( + context: context, + initialDate: _selectedMonth, + firstDate: DateTime(2000), + lastDate: DateTime(2100), + initialDatePickerMode: DatePickerMode.year, + ); + + if (picked != null) { + setState(() { + _selectedMonth = DateTime(picked.year, picked.month); + }); + _loadRecords(); // 重新加载数据 + } + } + @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('记账本'), ), - body: RecordList( - records: records, - onRecordTap: _navigateToRecordPage, - onRecordDelete: _deleteRecord, + body: Column( + children: [ + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + _buildMonthSelector(), + Text( + '总支出: ¥${records.fold(0.0, (sum, record) => sum + record.amount).toStringAsFixed(2)}', + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.bold, + ), + ), + ], + ), + ), + Expanded( + child: RecordList( + records: records, + onRecordTap: _navigateToRecordPage, + onRecordDelete: _deleteRecord, + ), + ), + ], ), floatingActionButton: FloatingActionButton( onPressed: () => _navigateToRecordPage(), diff --git a/lib/services/database_service.dart b/lib/services/database_service.dart index 3673b73..0e2acd0 100644 --- a/lib/services/database_service.dart +++ b/lib/services/database_service.dart @@ -91,4 +91,32 @@ class DatabaseService { ); }); } + + /// 添加按日���范围获取记录的方法 + Future> getRecordsByDateRange(DateTime startDate, DateTime endDate) async { + final db = await database; + final List> maps = await db.query( + 'records', + where: 'createTime BETWEEN ? AND ?', + whereArgs: [ + startDate.toIso8601String(), + endDate.toIso8601String(), + ], + orderBy: 'createTime DESC', + ); + + return List.generate(maps.length, (i) { + final map = maps[i]; + return Record( + id: map['id'], + type: RecordType.values[map['type']], + categoryId: map['categoryId'], + note: map['note'], + amount: map['amount'], + createTime: DateTime.parse(map['createTime']), + accountId: map['accountId'], + imageUrls: map['imageUrls']?.split(','), + ); + }); + } } \ No newline at end of file