import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:intl/intl.dart'; import '../../core/constants.dart'; import 'provider.dart'; final _currencyFmt = NumberFormat.currency(symbol: '\$', decimalDigits: 2); class SettingsScreen extends ConsumerStatefulWidget { const SettingsScreen({super.key}); @override ConsumerState createState() => _SettingsScreenState(); } class _SettingsScreenState extends ConsumerState { final _budgetController = TextEditingController(); bool _isEditing = false; @override void initState() { super.initState(); final budget = ref.read(budgetProvider); if (budget != null) { _budgetController.text = budget.toStringAsFixed(2); } } @override void dispose() { _budgetController.dispose(); super.dispose(); } Future _saveBudget() async { final text = _budgetController.text.trim(); if (text.isEmpty) { await ref.read(budgetProvider.notifier).setBudget(null); } else { final value = double.tryParse(text); if (value != null && value > 0) { await ref.read(budgetProvider.notifier).setBudget(value); } } setState(() => _isEditing = false); } @override Widget build(BuildContext context) { final budget = ref.watch(budgetProvider); return Scaffold( backgroundColor: AppColors.background, body: SafeArea( child: ListView( padding: const EdgeInsets.fromLTRB(20, 24, 20, 20), children: [ Text( 'Settings', style: Theme.of(context).textTheme.headlineSmall?.copyWith( fontWeight: FontWeight.w700, color: AppColors.textPrimary, ), ), Text( 'Manage your preferences', style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: AppColors.textSecondary, ), ), const SizedBox(height: 32), Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: AppColors.surface, borderRadius: BorderRadius.circular(16), border: Border.all(color: AppColors.divider), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( color: AppColors.accent.withOpacity(0.15), borderRadius: BorderRadius.circular(12), ), child: const Icon( Icons.account_balance_wallet_rounded, color: AppColors.accent, size: 20, ), ), const SizedBox(width: 12), Expanded( child: Text( 'Monthly Budget', style: Theme.of(context).textTheme.titleMedium?.copyWith( fontWeight: FontWeight.w600, color: AppColors.textPrimary, ), ), ), if (!_isEditing) IconButton( icon: const Icon(Icons.edit_rounded, size: 20), color: AppColors.textSecondary, onPressed: () => setState(() => _isEditing = true), ), ], ), const SizedBox(height: 16), if (_isEditing) Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ TextFormField( controller: _budgetController, keyboardType: const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')), ], decoration: const InputDecoration( prefixText: '\$ ', hintText: '0.00', helperText: 'Leave empty to remove budget limit', ), autofocus: true, ), const SizedBox(height: 12), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( onPressed: () { final budget = ref.read(budgetProvider); _budgetController.text = budget?.toStringAsFixed(2) ?? ''; setState(() => _isEditing = false); }, child: const Text('Cancel'), ), const SizedBox(width: 8), ElevatedButton( onPressed: _saveBudget, style: ElevatedButton.styleFrom( minimumSize: const Size(80, 40), ), child: const Text('Save'), ), ], ), ], ) else Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( budget != null ? _currencyFmt.format(budget) : 'Not set', style: Theme.of(context).textTheme.headlineSmall?.copyWith( color: budget != null ? AppColors.accent : AppColors.textSecondary, fontWeight: FontWeight.w700, ), ), const SizedBox(height: 8), Text( budget != null ? 'Your monthly spending limit' : 'Set a monthly spending limit to track your budget', style: Theme.of(context).textTheme.bodySmall?.copyWith( color: AppColors.textSecondary, ), ), ], ), ], ), ), const SizedBox(height: 24), Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: AppColors.accent.withOpacity(0.1), borderRadius: BorderRadius.circular(12), border: Border.all(color: AppColors.accent.withOpacity(0.3)), ), child: Row( children: [ const Icon(Icons.info_outline_rounded, color: AppColors.accent, size: 20), const SizedBox(width: 12), Expanded( child: Text( 'Budget tracking shows on the Dashboard with a progress bar and warning when exceeded.', style: Theme.of(context).textTheme.bodySmall?.copyWith( color: AppColors.textPrimary, ), ), ), ], ), ), ], ), ), ); } }