mirror of
https://github.com/koloideal/Casha.git
synced 2026-06-10 10:25:28 +03:00
update
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../shared/models/transaction.dart';
|
||||
|
||||
class AddTransactionState {
|
||||
final double? amount;
|
||||
final String category;
|
||||
final TransactionType type;
|
||||
final DateTime date;
|
||||
final String note;
|
||||
final bool isSubmitting;
|
||||
|
||||
const AddTransactionState({
|
||||
this.amount,
|
||||
this.category = 'Food',
|
||||
this.type = TransactionType.expense,
|
||||
required this.date,
|
||||
this.note = '',
|
||||
this.isSubmitting = false,
|
||||
});
|
||||
|
||||
AddTransactionState copyWith({
|
||||
double? amount,
|
||||
String? category,
|
||||
TransactionType? type,
|
||||
DateTime? date,
|
||||
String? note,
|
||||
bool? isSubmitting,
|
||||
}) =>
|
||||
AddTransactionState(
|
||||
amount: amount ?? this.amount,
|
||||
category: category ?? this.category,
|
||||
type: type ?? this.type,
|
||||
date: date ?? this.date,
|
||||
note: note ?? this.note,
|
||||
isSubmitting: isSubmitting ?? this.isSubmitting,
|
||||
);
|
||||
}
|
||||
|
||||
class AddTransactionNotifier extends StateNotifier<AddTransactionState> {
|
||||
AddTransactionNotifier()
|
||||
: super(AddTransactionState(date: DateTime.now()));
|
||||
|
||||
void setAmount(double? v) => state = state.copyWith(amount: v);
|
||||
void setCategory(String v) => state = state.copyWith(category: v);
|
||||
void setType(TransactionType v) => state = state.copyWith(type: v);
|
||||
void setDate(DateTime v) => state = state.copyWith(date: v);
|
||||
void setNote(String v) => state = state.copyWith(note: v);
|
||||
void setSubmitting(bool v) => state = state.copyWith(isSubmitting: v);
|
||||
void reset() => state = AddTransactionState(date: DateTime.now());
|
||||
}
|
||||
|
||||
final addTransactionProvider =
|
||||
StateNotifierProvider.autoDispose<AddTransactionNotifier, AddTransactionState>(
|
||||
(ref) => AddTransactionNotifier(),
|
||||
);
|
||||
@@ -0,0 +1,357 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../core/constants.dart';
|
||||
import '../../shared/models/transaction.dart';
|
||||
import '../dashboard/provider.dart';
|
||||
import 'provider.dart';
|
||||
|
||||
const _uuid = Uuid();
|
||||
|
||||
class AddTransactionScreen extends ConsumerStatefulWidget {
|
||||
const AddTransactionScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<AddTransactionScreen> createState() =>
|
||||
_AddTransactionScreenState();
|
||||
}
|
||||
|
||||
class _AddTransactionScreenState extends ConsumerState<AddTransactionScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _amountController = TextEditingController();
|
||||
final _noteController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_amountController.dispose();
|
||||
_noteController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
final state = ref.read(addTransactionProvider);
|
||||
ref.read(addTransactionProvider.notifier).setSubmitting(true);
|
||||
|
||||
final tx = Transaction(
|
||||
id: _uuid.v4(),
|
||||
amount: state.amount!,
|
||||
category: state.category,
|
||||
type: state.type,
|
||||
date: state.date,
|
||||
note: state.note.isEmpty ? null : state.note,
|
||||
);
|
||||
|
||||
await ref.read(transactionsProvider.notifier).add(tx);
|
||||
ref.read(addTransactionProvider.notifier).setSubmitting(false);
|
||||
|
||||
if (mounted) context.pop();
|
||||
}
|
||||
|
||||
Future<void> _pickDate() async {
|
||||
final state = ref.read(addTransactionProvider);
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: state.date,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime.now(),
|
||||
builder: (context, child) => Theme(
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: const ColorScheme.dark(
|
||||
primary: AppColors.accent,
|
||||
surface: AppColors.surface,
|
||||
),
|
||||
),
|
||||
child: child!,
|
||||
),
|
||||
);
|
||||
if (picked != null) {
|
||||
ref.read(addTransactionProvider.notifier).setDate(picked);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final state = ref.watch(addTransactionProvider);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
appBar: AppBar(
|
||||
title: const Text('Add Transaction'),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close_rounded),
|
||||
onPressed: () => context.pop(),
|
||||
),
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
children: [
|
||||
// Type toggle
|
||||
_TypeToggle(
|
||||
selected: state.type,
|
||||
onChanged: (t) =>
|
||||
ref.read(addTransactionProvider.notifier).setType(t),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Amount
|
||||
_SectionLabel('Amount'),
|
||||
const SizedBox(height: 8),
|
||||
TextFormField(
|
||||
controller: _amountController,
|
||||
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
|
||||
],
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
color: AppColors.textPrimary,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
decoration: const InputDecoration(
|
||||
prefixText: '\$ ',
|
||||
prefixStyle: TextStyle(
|
||||
color: AppColors.textSecondary,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
hintText: '0.00',
|
||||
),
|
||||
onChanged: (v) {
|
||||
final parsed = double.tryParse(v);
|
||||
ref.read(addTransactionProvider.notifier).setAmount(parsed);
|
||||
},
|
||||
validator: (v) {
|
||||
if (v == null || v.isEmpty) return 'Enter an amount';
|
||||
if (double.tryParse(v) == null) return 'Invalid amount';
|
||||
if (double.parse(v) <= 0) return 'Amount must be > 0';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// Category
|
||||
_SectionLabel('Category'),
|
||||
const SizedBox(height: 8),
|
||||
_CategoryPicker(
|
||||
selected: state.category,
|
||||
onChanged: (c) =>
|
||||
ref.read(addTransactionProvider.notifier).setCategory(c),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// Date
|
||||
_SectionLabel('Date'),
|
||||
const SizedBox(height: 8),
|
||||
InkWell(
|
||||
onTap: _pickDate,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: AppColors.divider),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.calendar_today_rounded,
|
||||
color: AppColors.textSecondary, size: 18),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
DateFormat('MMMM d, yyyy').format(state.date),
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// Note
|
||||
_SectionLabel('Note (optional)'),
|
||||
const SizedBox(height: 8),
|
||||
TextFormField(
|
||||
controller: _noteController,
|
||||
maxLines: 2,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Add a note...',
|
||||
),
|
||||
onChanged: (v) =>
|
||||
ref.read(addTransactionProvider.notifier).setNote(v),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
|
||||
ElevatedButton(
|
||||
onPressed: state.isSubmitting ? null : _submit,
|
||||
child: state.isSubmitting
|
||||
? const SizedBox(
|
||||
height: 20,
|
||||
width: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text('Save Transaction'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SectionLabel extends StatelessWidget {
|
||||
final String text;
|
||||
const _SectionLabel(this.text);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Text(
|
||||
text,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: AppColors.textSecondary,
|
||||
fontWeight: FontWeight.w600,
|
||||
letterSpacing: 0.5,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TypeToggle extends StatelessWidget {
|
||||
final TransactionType selected;
|
||||
final ValueChanged<TransactionType> onChanged;
|
||||
const _TypeToggle({required this.selected, required this.onChanged});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.divider),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
_TypeOption(
|
||||
label: 'Expense',
|
||||
icon: Icons.arrow_upward_rounded,
|
||||
color: AppColors.expense,
|
||||
isSelected: selected == TransactionType.expense,
|
||||
onTap: () => onChanged(TransactionType.expense),
|
||||
),
|
||||
_TypeOption(
|
||||
label: 'Income',
|
||||
icon: Icons.arrow_downward_rounded,
|
||||
color: AppColors.income,
|
||||
isSelected: selected == TransactionType.income,
|
||||
onTap: () => onChanged(TransactionType.income),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TypeOption extends StatelessWidget {
|
||||
final String label;
|
||||
final IconData icon;
|
||||
final Color color;
|
||||
final bool isSelected;
|
||||
final VoidCallback onTap;
|
||||
const _TypeOption({
|
||||
required this.label,
|
||||
required this.icon,
|
||||
required this.color,
|
||||
required this.isSelected,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Expanded(
|
||||
child: GestureDetector(
|
||||
onTap: onTap,
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected ? color.withOpacity(0.15) : Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(13),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(icon, color: isSelected ? color : AppColors.textSecondary, size: 18),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
label,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: isSelected ? color : AppColors.textSecondary,
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CategoryPicker extends StatelessWidget {
|
||||
final String selected;
|
||||
final ValueChanged<String> onChanged;
|
||||
const _CategoryPicker({required this.selected, required this.onChanged});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: AppCategories.all.map((cat) {
|
||||
final isSelected = cat == selected;
|
||||
final color = AppCategories.colors[cat] ?? AppColors.accent;
|
||||
final icon = AppCategories.icons[cat] ?? Icons.category_rounded;
|
||||
return GestureDetector(
|
||||
onTap: () => onChanged(cat),
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected ? color.withOpacity(0.2) : AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: isSelected ? color : AppColors.divider,
|
||||
width: isSelected ? 1.5 : 1,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, color: isSelected ? color : AppColors.textSecondary, size: 16),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
cat,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: isSelected ? color : AppColors.textSecondary,
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user