Velar/Frontend/monarch/lib/static.dart

713 lines
22 KiB
Dart
Raw Normal View History

2025-06-18 17:56:39 -07:00
// ignore_for_file: sort_child_properties_last, use_build_context_synchronously, deprecated_member_use, unnecessary_import, unused_local_variable, unused_import, sized_box_for_whitespace
2025-06-17 02:17:57 -07:00
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
2025-06-18 17:56:39 -07:00
import 'package:monarch/add.dart';
import 'package:monarch/homepage.dart';
import 'package:monarch/navbar.dart';
2025-06-17 02:17:57 -07:00
class Transaction {
final String description;
final double amount;
final String category;
final DateTime date;
Transaction({
required this.description,
required this.amount,
required this.category,
required this.date,
});
factory Transaction.fromJson(Map<String, dynamic> json) {
return Transaction(
description: json['description'],
amount: (json['amount'] as num).toDouble(),
category: json['category'],
date: DateTime.parse(json['date']),
);
}
}
class Statistics extends StatefulWidget {
const Statistics({super.key});
@override
State<Statistics> createState() => StatisticsState();
}
2025-06-18 17:56:39 -07:00
class StatisticsState extends State<Statistics> with TickerProviderStateMixin {
2025-06-17 02:17:57 -07:00
List<Transaction> transactions = [];
bool isLoading = false;
2025-06-18 17:56:39 -07:00
late AnimationController _animationController;
late Animation<double> _fadeAnimation;
late Animation<Offset> _slideAnimation;
2025-06-17 02:17:57 -07:00
final List<String> categories = [
'All',
'Food',
'Shopping',
'Bills',
'Travel',
'Entertainment',
'Other',
];
String selectedCategory = 'All';
String _selectedPlan = 'Monthly';
Map<String, double> totalAmountPerCategory = {};
2025-06-18 17:56:39 -07:00
// Modern color palette inspired by the image
final Color backgroundColor = Color(0xFFF8F9FA); // Light cream/white
final Color primaryColor = Color(0xFF2D3436); // Deep charcoal
final Color accentColor = Color(0xFF00B894); // Emerald green
final Color cardColor = Color(0xFFFFFFFF); // Pure white
final Color textSecondary = Color(0xFF636E72); // Muted gray
final Color surfaceColor = Color(0xFFF1F2F6); // Light surface
2025-06-17 02:17:57 -07:00
@override
void initState() {
super.initState();
2025-06-18 17:56:39 -07:00
_animationController = AnimationController(
duration: const Duration(milliseconds: 1200),
vsync: this,
);
_fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeOut),
);
_slideAnimation = Tween<Offset>(
begin: const Offset(0, 0.3),
end: Offset.zero,
).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeOut),
);
2025-06-17 02:17:57 -07:00
fetchTransactions();
2025-06-18 17:56:39 -07:00
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
2025-06-17 02:17:57 -07:00
}
//API Call for Fetch
Future<void> fetchTransactions({String category = 'All'}) async {
setState(() {
isLoading = true;
});
try {
2025-06-18 17:56:39 -07:00
final uri = Uri.parse('http://192.168.1.9:3000/api/transactions').replace(
2025-06-17 02:17:57 -07:00
queryParameters: category == 'All' ? null : {'category': category},
);
final response = await http.get(uri);
if (response.statusCode == 200) {
final data = json.decode(response.body);
final List<dynamic> list = data['data'];
final fetched = list.map((json) => Transaction.fromJson(json)).toList();
final totals = <String, double>{};
for (var tx in fetched) {
totals[tx.category] = (totals[tx.category] ?? 0) + tx.amount;
}
setState(() {
transactions = fetched;
totalAmountPerCategory = totals;
});
} else {
throw Exception('Failed to load transactions');
}
} catch (e) {
2025-06-18 17:56:39 -07:00
_showCustomSnackBar(
message: 'Error fetching transactions',
icon: Icons.data_exploration_rounded,
backgroundColor: const Color(0xFFFF9F43),
iconColor: Colors.white,
duration: const Duration(seconds: 4),
);
2025-06-17 02:17:57 -07:00
} finally {
setState(() {
isLoading = false;
});
}
}
2025-06-18 17:56:39 -07:00
void _showCustomSnackBar({
required String message,
required IconData icon,
required Color backgroundColor,
required Color iconColor,
Duration duration = const Duration(seconds: 3),
}) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Container(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, color: iconColor, size: 20),
),
const SizedBox(width: 12),
Expanded(
child: Text(
message,
style: GoogleFonts.inter(
fontWeight: FontWeight.w500,
color: Colors.white,
fontSize: 14,
height: 1.4,
2025-06-17 02:17:57 -07:00
),
),
2025-06-18 17:56:39 -07:00
),
],
),
),
backgroundColor: backgroundColor,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
margin: const EdgeInsets.all(16),
duration: duration,
elevation: 8,
2025-06-17 02:17:57 -07:00
),
);
}
2025-06-18 17:56:39 -07:00
Widget _buildHeader() {
2025-06-17 02:17:57 -07:00
return Container(
2025-06-18 17:56:39 -07:00
padding: const EdgeInsets.fromLTRB(24, 60, 24, 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: cardColor,
borderRadius: BorderRadius.circular(22),
boxShadow: [
BoxShadow(
color: primaryColor.withOpacity(0.08),
blurRadius: 20,
offset: const Offset(0, 4),
),
],
),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(22),
child: InkWell(
borderRadius: BorderRadius.circular(22),
onTap: () => Navigator.pop(context),
child: Icon(
Icons.arrow_back_ios_rounded,
color: primaryColor,
size: 20,
),
),
),
),
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: cardColor,
borderRadius: BorderRadius.circular(22),
boxShadow: [
BoxShadow(
color: primaryColor.withOpacity(0.08),
blurRadius: 20,
offset: const Offset(0, 4),
),
],
),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(22),
child: InkWell(
borderRadius: BorderRadius.circular(22),
onTap: () {},
child: Icon(
Icons.more_horiz_rounded,
color: primaryColor,
size: 24,
),
),
),
2025-06-17 02:17:57 -07:00
),
],
),
2025-06-18 17:56:39 -07:00
);
}
Widget _buildTotalSpentCard() {
final totalSpent = transactions.fold<double>(
0,
(sum, tx) => sum + tx.amount,
);
final budget = 10000.0; // You can make this dynamic
final progress = totalSpent / budget;
return SlideTransition(
position: _slideAnimation,
child: FadeTransition(
opacity: _fadeAnimation,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 24),
padding: const EdgeInsets.all(32),
decoration: BoxDecoration(
color: cardColor,
borderRadius: BorderRadius.circular(28),
boxShadow: [
BoxShadow(
color: primaryColor.withOpacity(0.06),
blurRadius: 30,
offset: const Offset(0, 8),
spreadRadius: 0,
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'TOTAL SPENT',
style: GoogleFonts.inter(
fontSize: 13,
fontWeight: FontWeight.w500,
color: textSecondary,
letterSpacing: 1.2,
),
),
const SizedBox(height: 12),
Text(
'₹${totalSpent.toStringAsFixed(1)}',
style: GoogleFonts.inter(
fontSize: 42,
fontWeight: FontWeight.w700,
color: primaryColor,
height: 1.1,
),
),
const SizedBox(height: 32),
// Modern Circular Progress Ring
Center(
child: Container(
width: 160,
height: 160,
child: Stack(
alignment: Alignment.center,
children: [
// Background circle
Container(
width: 160,
height: 160,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: surfaceColor, width: 8),
),
),
// Progress circle
SizedBox(
width: 160,
height: 160,
child: CircularProgressIndicator(
value: progress > 1 ? 1 : progress,
strokeWidth: 8,
backgroundColor: Colors.transparent,
valueColor: AlwaysStoppedAnimation<Color>(
progress > 0.8
? Color(0xFFE74C3C)
: progress > 0.6
? Color(0xFFF39C12)
: accentColor,
),
strokeCap: StrokeCap.round,
),
),
// Center content
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'${(progress * 100).toInt()}%',
style: GoogleFonts.inter(
fontSize: 24,
fontWeight: FontWeight.w700,
color: primaryColor,
),
),
const SizedBox(height: 4),
Text(
'of ₹${budget.toInt()}',
style: GoogleFonts.inter(
fontSize: 12,
fontWeight: FontWeight.w500,
color: textSecondary,
),
),
],
),
],
),
),
),
const SizedBox(height: 24),
// Modern category breakdown
if (totalAmountPerCategory.isNotEmpty) _buildCategoryBreakdown(),
],
2025-06-17 02:17:57 -07:00
),
),
2025-06-18 17:56:39 -07:00
),
);
}
Widget _buildCategoryBreakdown() {
final sortedCategories =
totalAmountPerCategory.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));
final topCategories = sortedCategories.take(3).toList();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'TOP CATEGORIES',
style: GoogleFonts.inter(
fontSize: 11,
fontWeight: FontWeight.w600,
color: textSecondary,
letterSpacing: 1,
2025-06-17 02:17:57 -07:00
),
),
2025-06-18 17:56:39 -07:00
const SizedBox(height: 16),
...topCategories.map(
(entry) => _buildCategoryRow(entry.key, entry.value),
),
],
);
}
Widget _buildCategoryRow(String category, double amount) {
final total = transactions.fold<double>(0, (sum, tx) => sum + tx.amount);
final percentage = total > 0 ? (amount / total) : 0.0;
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Row(
children: [
Container(
width: 8,
height: 8,
decoration: BoxDecoration(
color: _getCategoryColor(category),
shape: BoxShape.circle,
),
),
const SizedBox(width: 12),
Expanded(
child: Text(
category,
style: GoogleFonts.inter(
fontSize: 14,
fontWeight: FontWeight.w500,
color: primaryColor,
),
),
),
Text(
'${(percentage * 100).toInt()}%',
style: GoogleFonts.inter(
fontSize: 14,
fontWeight: FontWeight.w600,
color: textSecondary,
),
),
],
),
);
}
Widget _buildToggleButtons() {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 24, vertical: 24),
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: surfaceColor,
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Expanded(child: _buildToggleButton('Monthly')),
Expanded(child: _buildToggleButton('Yearly')),
],
2025-06-17 02:17:57 -07:00
),
);
}
Widget _buildToggleButton(String label) {
final bool isSelected = _selectedPlan == label;
return GestureDetector(
onTap: () => setState(() => _selectedPlan = label),
child: AnimatedContainer(
duration: const Duration(milliseconds: 300),
2025-06-18 17:56:39 -07:00
curve: Curves.easeInOut,
padding: const EdgeInsets.symmetric(vertical: 16),
2025-06-17 02:17:57 -07:00
decoration: BoxDecoration(
2025-06-18 17:56:39 -07:00
color: isSelected ? cardColor : Colors.transparent,
borderRadius: BorderRadius.circular(12),
2025-06-17 02:17:57 -07:00
boxShadow:
isSelected
? [
BoxShadow(
2025-06-18 17:56:39 -07:00
color: primaryColor.withOpacity(0.08),
blurRadius: 10,
spreadRadius: 0,
offset: const Offset(0, 2),
2025-06-17 02:17:57 -07:00
),
]
: [],
),
alignment: Alignment.center,
child: Text(
label,
2025-06-18 17:56:39 -07:00
style: GoogleFonts.inter(
color: isSelected ? primaryColor : textSecondary,
2025-06-17 02:17:57 -07:00
fontWeight: FontWeight.w600,
2025-06-18 17:56:39 -07:00
fontSize: 15,
2025-06-17 02:17:57 -07:00
),
),
),
);
}
2025-06-18 17:56:39 -07:00
Widget buildCategoryChips() {
return Container(
height: 50,
margin: const EdgeInsets.only(bottom: 16),
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 20),
itemCount: categories.length,
itemBuilder: (context, index) {
final cat = categories[index];
final bool isSelected = cat == selectedCategory;
return Container(
margin: const EdgeInsets.only(right: 12),
child: FilterChip(
label: Text(cat),
selected: isSelected,
onSelected: (selected) {
if (selected) {
setState(() {
selectedCategory = cat;
});
fetchTransactions(category: cat);
}
},
backgroundColor: surfaceColor,
selectedColor: primaryColor,
checkmarkColor: cardColor,
labelStyle: GoogleFonts.inter(
color: isSelected ? cardColor : textSecondary,
fontWeight: FontWeight.w500,
fontSize: 14,
),
side: BorderSide.none,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
),
);
},
),
);
}
2025-06-17 02:17:57 -07:00
2025-06-18 17:56:39 -07:00
Widget buildTransactionCard(Transaction tx) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 24, vertical: 6),
decoration: BoxDecoration(
color: cardColor,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: primaryColor.withOpacity(0.04),
blurRadius: 20,
offset: const Offset(0, 4),
),
],
),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
leading: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: _getCategoryColor(tx.category),
borderRadius: BorderRadius.circular(14),
),
child: Icon(
_getCategoryIcon(tx.category),
color: cardColor,
size: 24,
),
2025-06-17 02:17:57 -07:00
),
2025-06-18 17:56:39 -07:00
title: Text(
tx.description,
style: GoogleFonts.inter(
color: primaryColor,
fontWeight: FontWeight.w600,
fontSize: 16,
),
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
'${tx.category.toUpperCase()} • ${_formatTime(tx.date)}',
style: GoogleFonts.inter(
color: textSecondary,
fontSize: 13,
fontWeight: FontWeight.w500,
2025-06-17 02:17:57 -07:00
),
),
2025-06-18 17:56:39 -07:00
),
trailing: Text(
'₹${tx.amount.toStringAsFixed(1)}',
style: GoogleFonts.inter(
color: primaryColor,
fontWeight: FontWeight.w700,
fontSize: 17,
),
2025-06-17 02:17:57 -07:00
),
),
);
}
2025-06-18 17:56:39 -07:00
Color _getCategoryColor(String category) {
final colors = [
Color(0xFF6C5CE7),
Color(0xFFE17055),
Color(0xFF00B894),
Color(0xFF0984E3),
Color(0xFFE84393),
Color(0xFFFD79A8),
Color(0xFF6C5CE7),
];
return colors[categories.indexOf(category) % colors.length];
}
IconData _getCategoryIcon(String category) {
switch (category) {
case 'Food':
return Icons.restaurant_rounded;
case 'Shopping':
return Icons.shopping_bag_rounded;
case 'Bills':
return Icons.receipt_rounded;
case 'Travel':
return Icons.flight_rounded;
case 'Entertainment':
return Icons.movie_rounded;
default:
return Icons.category_rounded;
}
}
String _formatTime(DateTime date) {
final now = DateTime.now();
final difference = now.difference(date);
if (difference.inDays == 0) {
return '${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
} else {
return '${date.day.toString().padLeft(2, '0')}-${date.month.toString().padLeft(2, '0')}';
}
}
int _selectedIndex = 0;
2025-06-17 02:17:57 -07:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backgroundColor,
body: Column(
children: [
2025-06-18 17:56:39 -07:00
_buildHeader(),
_buildTotalSpentCard(),
_buildToggleButtons(),
2025-06-17 02:17:57 -07:00
buildCategoryChips(),
Expanded(
child:
isLoading
2025-06-18 17:56:39 -07:00
? Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(primaryColor),
strokeWidth: 2,
),
)
2025-06-17 02:17:57 -07:00
: transactions.isEmpty
? Center(
2025-06-18 17:56:39 -07:00
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.analytics_outlined,
size: 64,
color: textSecondary.withOpacity(0.5),
),
const SizedBox(height: 16),
Text(
'No transactions found',
style: GoogleFonts.inter(
color: textSecondary,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
],
2025-06-17 02:17:57 -07:00
),
)
: ListView.builder(
2025-06-18 17:56:39 -07:00
padding: const EdgeInsets.only(top: 8, bottom: 120),
2025-06-17 02:17:57 -07:00
itemCount: transactions.length,
itemBuilder:
(context, index) =>
buildTransactionCard(transactions[index]),
),
),
],
),
2025-06-18 17:56:39 -07:00
floatingActionButton: CustomNavBar(
currentIndex: _selectedIndex,
onTap: (index) {
setState(() => _selectedIndex = index);
},
backgroundColor: backgroundColor,
accentColor: accentColor,
primaryColor: primaryColor,
cardColor: cardColor,
2025-06-17 02:17:57 -07:00
),
2025-06-18 17:56:39 -07:00
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
2025-06-17 02:17:57 -07:00
);
}
}