添加月份选择

This commit is contained in:
ddshi 2024-12-24 19:20:01 +08:00
parent 114699f015
commit edef638500
2 changed files with 110 additions and 6 deletions

View File

@ -22,6 +22,7 @@ class _HomePageState extends State<HomePage> {
bool _isNavigating = false; bool _isNavigating = false;
final _dbService = DatabaseService(); final _dbService = DatabaseService();
List<Record> records = []; List<Record> records = [];
DateTime _selectedMonth = DateTime.now(); //
// //
static const double _shakeThreshold = 8.0; // static const double _shakeThreshold = 8.0; //
@ -60,7 +61,7 @@ class _HomePageState extends State<HomePage> {
_loadRecords(); _loadRecords();
} }
/// /// <EFBFBD><EFBFBD>
void _resetShakeDetection() { void _resetShakeDetection() {
_lastShakeTime = null; _lastShakeTime = null;
_recentXValues.clear(); _recentXValues.clear();
@ -73,7 +74,15 @@ class _HomePageState extends State<HomePage> {
/// ///
Future<void> _loadRecords() async { Future<void> _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(() { setState(() {
records = loadedRecords; records = loadedRecords;
records.sort((a, b) => b.createTime.compareTo(a.createTime)); records.sort((a, b) => b.createTime.compareTo(a.createTime));
@ -170,16 +179,83 @@ class _HomePageState extends State<HomePage> {
await _loadRecords(); 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<void> _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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('记账本'), title: const Text('记账本'),
), ),
body: RecordList( body: Column(
records: records, children: [
onRecordTap: _navigateToRecordPage, Padding(
onRecordDelete: _deleteRecord, 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( floatingActionButton: FloatingActionButton(
onPressed: () => _navigateToRecordPage(), onPressed: () => _navigateToRecordPage(),

View File

@ -91,4 +91,32 @@ class DatabaseService {
); );
}); });
} }
/// <EFBFBD><EFBFBD><EFBFBD>
Future<List<Record>> getRecordsByDateRange(DateTime startDate, DateTime endDate) async {
final db = await database;
final List<Map<String, dynamic>> 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(','),
);
});
}
} }