import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:lucide_icons/lucide_icons.dart'; import 'package:path_provider/path_provider.dart'; import 'package:share_plus/share_plus.dart'; import 'package:cross_file/cross_file.dart'; import 'auth_screen.dart'; import '../../../core/network/dio_client.dart'; import '../providers/auth_provider.dart'; import '../../transactions/providers/providers.dart'; import '../../../core/theme/theme_provider.dart'; class ProfileScreen extends ConsumerStatefulWidget { const ProfileScreen({super.key}); @override ConsumerState createState() => _ProfileScreenState(); } class _ProfileScreenState extends ConsumerState { bool _isExporting = false; Future _logout(BuildContext context) async { const storage = FlutterSecureStorage(); await storage.delete(key: 'jwt_token'); if (context.mounted) { Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => const AuthScreen()), (route) => false, ); } } Future _exportData() async { setState(() => _isExporting = true); try { final bytes = await ref.read(apiRepositoryProvider).exportTransactions(); final tempDir = await getTemporaryDirectory(); final file = File('${tempDir.path}/transactions_export.csv'); await file.writeAsBytes(bytes); final xFile = XFile(file.path); await Share.shareXFiles([xFile], text: 'Here is my Kifi transactions export.'); } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Export failed: $e'))); } } finally { if (mounted) { setState(() => _isExporting = false); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Profile'), backgroundColor: Colors.transparent, elevation: 0, ), body: SafeArea( child: Padding( padding: const EdgeInsets.all(24.0), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ const SizedBox(height: 20), CircleAvatar( radius: 50, backgroundColor: Theme.of(context).colorScheme.primary.withOpacity(0.1), child: Icon(LucideIcons.user, size: 50, color: Theme.of(context).colorScheme.primary), ), const SizedBox(height: 24), const Text('Kifi User', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), const SizedBox(height: 8), const Text('maddy23285@gmail.com', style: TextStyle(color: Colors.grey, fontSize: 16)), const SizedBox(height: 48), Card( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), child: Column( children: [ ListTile( leading: const Icon(LucideIcons.downloadCloud), title: const Text('Export Data to CSV'), trailing: _isExporting ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2)) : const Icon(LucideIcons.chevronRight), onTap: _isExporting ? null : _exportData, ), const Divider(height: 1), ListTile( leading: const Icon(LucideIcons.moon), title: const Text('Theme'), trailing: SegmentedButton( segments: const [ ButtonSegment(value: ThemeMode.light, icon: Icon(LucideIcons.sun)), ButtonSegment(value: ThemeMode.system, icon: Icon(LucideIcons.monitor)), ButtonSegment(value: ThemeMode.dark, icon: Icon(LucideIcons.moon)), ], selected: {ref.watch(themeProvider)}, onSelectionChanged: (Set newSelection) { ref.read(themeProvider.notifier).setTheme(newSelection.first); }, showSelectedIcon: false, style: ButtonStyle(visualDensity: VisualDensity.compact), ), ), const Divider(height: 1), ListTile( leading: const Icon(LucideIcons.helpCircle), title: const Text('Help & Support'), trailing: const Icon(LucideIcons.chevronRight), onTap: () {}, ), ], ), ), const Spacer(), SizedBox( width: double.infinity, child: ElevatedButton.icon( onPressed: () => _logout(context), style: ElevatedButton.styleFrom( backgroundColor: Colors.red.withOpacity(0.1), foregroundColor: Colors.red, padding: const EdgeInsets.symmetric(vertical: 16), ), icon: const Icon(LucideIcons.logOut), label: const Text('Log Out', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), ), ), const SizedBox(height: 20), ], ), ), ), ); } }