129 lines
3.8 KiB
Dart
129 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../models/record.dart';
|
|
|
|
class MonthlyReport extends StatelessWidget {
|
|
final List<Record> records;
|
|
|
|
const MonthlyReport({
|
|
Key? key,
|
|
required this.records,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final monthlyData = _getMonthlyData();
|
|
|
|
return Container(
|
|
padding: const EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.05),
|
|
blurRadius: 8,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'月报表',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Table(
|
|
columnWidths: const {
|
|
0: FlexColumnWidth(1.5),
|
|
1: FlexColumnWidth(2),
|
|
2: FlexColumnWidth(2),
|
|
3: FlexColumnWidth(2),
|
|
},
|
|
children: [
|
|
const TableRow(
|
|
children: [
|
|
Text('月份', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
Text('收入', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
Text('支出', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
Text('结余', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
],
|
|
),
|
|
...monthlyData.map((data) => TableRow(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: Text('${data.month}月'),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: Text(
|
|
'¥${data.income.toStringAsFixed(2)}',
|
|
style: const TextStyle(color: Colors.green),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: Text(
|
|
'¥${data.expense.toStringAsFixed(2)}',
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: Text(
|
|
'¥${(data.income - data.expense).toStringAsFixed(2)}',
|
|
style: TextStyle(
|
|
color: data.income >= data.expense
|
|
? Colors.green
|
|
: Colors.red,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
)).toList(),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
List<MonthData> _getMonthlyData() {
|
|
final Map<int, MonthData> monthlyMap = {};
|
|
|
|
// 初始化12个月的数据
|
|
for (int month = 1; month <= 12; month++) {
|
|
monthlyMap[month] = MonthData(month: month);
|
|
}
|
|
|
|
// 填充实际数据
|
|
for (final record in records) {
|
|
final month = record.createTime.month;
|
|
if (record.type == RecordType.expense) {
|
|
monthlyMap[month]!.expense += record.amount;
|
|
} else {
|
|
monthlyMap[month]!.income += record.amount;
|
|
}
|
|
}
|
|
|
|
return monthlyMap.values.toList()
|
|
..sort((a, b) => a.month.compareTo(b.month));
|
|
}
|
|
}
|
|
|
|
class MonthData {
|
|
final int month;
|
|
double expense;
|
|
double income;
|
|
|
|
MonthData({
|
|
required this.month,
|
|
this.expense = 0,
|
|
this.income = 0,
|
|
});
|
|
} |