添加月份选择
This commit is contained in:
parent
114699f015
commit
edef638500
@ -22,6 +22,7 @@ class _HomePageState extends State<HomePage> {
|
||||
bool _isNavigating = false;
|
||||
final _dbService = DatabaseService();
|
||||
List<Record> records = [];
|
||||
DateTime _selectedMonth = DateTime.now(); // 添加选中月份状态
|
||||
|
||||
// 定义摇晃检测的常量
|
||||
static const double _shakeThreshold = 8.0; // 单次摇晃的加速度阈值
|
||||
@ -60,7 +61,7 @@ class _HomePageState extends State<HomePage> {
|
||||
_loadRecords();
|
||||
}
|
||||
|
||||
/// 重置摇晃检测状态
|
||||
/// 重置摇<EFBFBD><EFBFBD>检测状态
|
||||
void _resetShakeDetection() {
|
||||
_lastShakeTime = null;
|
||||
_recentXValues.clear();
|
||||
@ -73,7 +74,15 @@ class _HomePageState extends State<HomePage> {
|
||||
|
||||
/// 加载记录
|
||||
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(() {
|
||||
records = loadedRecords;
|
||||
records.sort((a, b) => b.createTime.compareTo(a.createTime));
|
||||
@ -170,17 +179,84 @@ class _HomePageState extends State<HomePage> {
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('记账本'),
|
||||
),
|
||||
body: RecordList(
|
||||
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(),
|
||||
child: const Icon(Icons.add),
|
||||
|
||||
@ -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(','),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user