101 lines
3.1 KiB
Dart
101 lines
3.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:lucide_icons/lucide_icons.dart';
|
|
import '../../../transactions/providers/providers.dart';
|
|
|
|
class BudgetStatusCard extends ConsumerWidget {
|
|
const BudgetStatusCard({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final transState = ref.watch(transactionProvider);
|
|
final budgetState = ref.watch(budgetProvider);
|
|
|
|
if (transState.isLoading || budgetState.isLoading) {
|
|
return const CircularProgressIndicator();
|
|
}
|
|
|
|
final transactions = transState.value ?? [];
|
|
final budgets = budgetState.value ?? [];
|
|
|
|
if (budgets.isEmpty) {
|
|
return const SizedBox.shrink(); // Hide if no budgets
|
|
}
|
|
|
|
final now = DateTime.now();
|
|
final currentMonthTxs = transactions.where(
|
|
(t) => t.date.month == now.month && t.date.year == now.year,
|
|
);
|
|
|
|
double totalSpent = 0;
|
|
for (var b in budgets) {
|
|
if (b.walletId != null) {
|
|
final spentForWallet = currentMonthTxs
|
|
.where((t) => t.toWalletId == b.walletId)
|
|
.fold(0.0, (s, t) => s + t.amount);
|
|
totalSpent += spentForWallet;
|
|
}
|
|
}
|
|
|
|
final totalLimit = budgets.fold(0.0, (s, b) => s + b.monthlyLimit);
|
|
final progress = totalLimit > 0
|
|
? (totalSpent / totalLimit).clamp(0.0, 1.0)
|
|
: 0.0;
|
|
|
|
Color progressColor = Colors.green;
|
|
if (progress > 0.9)
|
|
progressColor = Colors.red;
|
|
else if (progress > 0.7)
|
|
progressColor = Colors.orange;
|
|
|
|
return Card(
|
|
child: InkWell(
|
|
onTap: () {
|
|
// Could navigate to budget tab or push budget screen
|
|
},
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Row(
|
|
children: [
|
|
Icon(LucideIcons.target, size: 18),
|
|
SizedBox(width: 8),
|
|
Text(
|
|
'Overall Budget Status',
|
|
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
LinearProgressIndicator(
|
|
value: progress,
|
|
backgroundColor: Colors.grey.shade200,
|
|
color: progressColor,
|
|
minHeight: 12,
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
'Spent: Rs. ${totalSpent.toStringAsFixed(0)}',
|
|
style: TextStyle(color: Colors.grey.shade700),
|
|
),
|
|
Text(
|
|
'Limit: Rs. ${totalLimit.toStringAsFixed(0)}',
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|