swing_account/lib/widgets/monthly_summary_card.dart
2024-12-25 14:06:42 +08:00

136 lines
3.8 KiB
Dart

import 'package:flutter/material.dart';
import '../models/record.dart';
class MonthlySummaryCard extends StatelessWidget {
final List<Record> records;
final bool isVisible;
final VoidCallback onAddRecord;
final Function(bool) onVisibilityChanged;
const MonthlySummaryCard({
Key? key,
required this.records,
required this.isVisible,
required this.onAddRecord,
required this.onVisibilityChanged,
}) : super(key: key);
String _formatAmount(double amount, {bool showSign = false}) {
if (!isVisible) return '¥****';
String prefix = showSign && amount > 0 ? '+' : '';
return '$prefix¥${amount.toStringAsFixed(2)}';
}
@override
Widget build(BuildContext context) {
final monthlyExpense = records
.where((r) => r.type == RecordType.expense)
.fold(0.0, (sum, r) => sum + r.amount);
final monthlyIncome = records
.where((r) => r.type == RecordType.income)
.fold(0.0, (sum, r) => sum + r.amount);
final monthlyBalance = monthlyIncome - monthlyExpense;
return Container(
margin: const EdgeInsets.all(16),
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'月支出',
style: TextStyle(
fontSize: 16,
color: Colors.black54,
),
),
IconButton(
icon: Icon(
isVisible ? Icons.visibility : Icons.visibility_off,
size: 20,
color: Colors.black54,
),
onPressed: () => onVisibilityChanged(!isVisible),
),
],
),
const SizedBox(height: 8),
Text(
_formatAmount(monthlyExpense),
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildSummaryItem('月收入', monthlyIncome),
_buildSummaryItem('月结余', monthlyBalance, showSign: true),
],
),
const SizedBox(height: 20),
SizedBox(
width: double.infinity,
height: 44,
child: ElevatedButton(
onPressed: onAddRecord,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF9FE2BF),
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(22),
),
),
child: const Text(
'记一笔',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
),
),
],
),
);
}
Widget _buildSummaryItem(String label, double amount, {bool showSign = false}) {
return Column(
children: [
Text(
label,
style: const TextStyle(
fontSize: 14,
color: Colors.black54,
),
),
const SizedBox(height: 4),
Text(
_formatAmount(amount, showSign: showSign),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
],
);
}
}