This commit is contained in:
2026-03-20 01:08:46 +03:00
commit 3dcbb6164e
142 changed files with 6599 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
import 'package:flutter/foundation.dart';
enum TransactionType { income, expense }
@immutable
class Transaction {
final String id;
final double amount;
final String category;
final TransactionType type;
final DateTime date;
final String? note;
const Transaction({
required this.id,
required this.amount,
required this.category,
required this.type,
required this.date,
this.note,
});
factory Transaction.fromJson(Map<String, dynamic> json) => Transaction(
id: json['id'] as String,
amount: (json['amount'] as num).toDouble(),
category: json['category'] as String,
type: TransactionType.values.firstWhere(
(e) => e.name == json['type'],
),
date: DateTime.parse(json['date'] as String),
note: json['note'] as String?,
);
Map<String, dynamic> toJson() => {
'id': id,
'amount': amount,
'category': category,
'type': type.name,
'date': date.toIso8601String(),
'note': note,
};
Transaction copyWith({
String? id,
double? amount,
String? category,
TransactionType? type,
DateTime? date,
String? note,
}) =>
Transaction(
id: id ?? this.id,
amount: amount ?? this.amount,
category: category ?? this.category,
type: type ?? this.type,
date: date ?? this.date,
note: note ?? this.note,
);
}
+36
View File
@@ -0,0 +1,36 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/transaction.dart';
class StorageService {
static const _key = 'transactions';
final SharedPreferences _prefs;
StorageService(this._prefs);
List<Transaction> loadTransactions() {
final raw = _prefs.getString(_key);
if (raw == null) return [];
final list = jsonDecode(raw) as List<dynamic>;
return list
.map((e) => Transaction.fromJson(e as Map<String, dynamic>))
.toList();
}
Future<void> saveTransactions(List<Transaction> transactions) async {
final encoded = jsonEncode(transactions.map((t) => t.toJson()).toList());
await _prefs.setString(_key, encoded);
}
Future<void> addTransaction(Transaction transaction) async {
final list = loadTransactions();
list.add(transaction);
await saveTransactions(list);
}
Future<void> deleteTransaction(String id) async {
final list = loadTransactions()..removeWhere((t) => t.id == id);
await saveTransactions(list);
}
}