swing_account/lib/models/record.dart

73 lines
1.7 KiB
Dart
Raw 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';
/// 记账类型枚举
enum RecordType {
expense, // 支出
income, // 收入
}
/// 记账分类模型
class Category {
final String id;
final String name;
final IconData icon;
final RecordType type;
final String? parentId; // 为未来二级分类预留
const Category({
required this.id,
required this.name,
required this.icon,
required this.type,
this.parentId,
});
}
/// 记账记录模型
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,
);
}