95 lines
2.2 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import '../data/icons.dart';
/// 记账类型枚举
enum RecordType {
expense, // 支出
income, // 收入
}
/// 记账分类模型
class Category {
final String id;
final String name;
final String iconKey; // 改为使用图标key
final RecordType type;
final int sortOrder; // 添加排序字段
const Category({
required this.id,
required this.name,
required this.iconKey,
required this.type,
required this.sortOrder,
});
// 获取图标
IconData get icon => IconMapping.getIcon(iconKey);
// 转换为JSON格式
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'iconKey': iconKey,
'type': type.index,
'sortOrder': sortOrder,
};
// 从JSON格式转换回对象
factory Category.fromJson(Map<String, dynamic> json) => Category(
id: json['id'],
name: json['name'],
iconKey: json['iconKey'],
type: RecordType.values[json['type']],
sortOrder: json['sortOrder'],
);
}
/// 记账记录模型
class Record {
final String id;
final RecordType type;
final String categoryId;
final String? note;
final double amount;
final DateTime createTime;
final String? accountId; // 为未来账户功能预留
final List<String>? imageUrls; // 为未来配图功能预留
Record({
required this.id,
required this.type,
required this.categoryId,
this.note,
required this.amount,
required this.createTime,
this.accountId,
this.imageUrls,
});
// 转换为JSON格式方便存储
Map<String, dynamic> toJson() => {
'id': id,
'type': type.index,
'categoryId': categoryId,
'note': note,
'amount': amount,
'createTime': createTime.toIso8601String(),
'accountId': accountId,
'imageUrls': imageUrls,
};
// 从JSON格式转换回对象
factory Record.fromJson(Map<String, dynamic> json) => Record(
id: json['id'],
type: RecordType.values[json['type']],
categoryId: json['categoryId'],
note: json['note'],
amount: json['amount'],
createTime: DateTime.parse(json['createTime']),
accountId: json['accountId'],
imageUrls: json['imageUrls'] != null
? List<String>.from(json['imageUrls'])
: null,
);
}