MoodCanvas/lib/models/recipe.dart
fengshengxiong 91b7eebbf2 接入TopON
2026-01-22 16:34:55 +08:00

133 lines
3.8 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.

// "配方"模型,定义了所有可编辑的参数
class Recipe {
String id;
String baseImagePath;
double brightness;
double contrast;
double saturation;
double blur;
double pixelate;
String overlayText;
String fontFamily;
int textColor; // 存储为 ARGB 整数
Recipe({
required this.id,
required this.baseImagePath,
this.brightness = 0.0, // 亮度 (-1 to 1, default 0)
this.contrast = 1.0, // 对比度 (0 to 4, default 1)
this.saturation = 1.0, // 饱和度 (0 to 4, default 1)
this.blur = 0.0, // 模糊 (0 to 20, default 0)
this.pixelate = 1.0, // 像素化 (1 to 30, default 1 = off)
this.overlayText = '',
this.fontFamily = 'Lato',
this.textColor = 0xFFFFFFFF, // 默认白色
});
// 转换为 JSON (用于存储到 shared_preferences)
Map<String, dynamic> toJson() {
return {
'id': id,
'baseImagePath': baseImagePath,
'brightness': brightness,
'contrast': contrast,
'saturation': saturation,
'blur': blur,
'pixelate': pixelate,
'overlayText': overlayText,
'fontFamily': fontFamily,
'textColor': textColor,
};
}
// 从 JSON 恢复 (用于从 shared_preferences 读取)
factory Recipe.fromJson(Map<String, dynamic> json) {
return Recipe(
id: json['id'],
baseImagePath: json['baseImagePath'],
brightness: json['brightness'] ?? 0.0,
contrast: json['contrast'] ?? 1.0,
saturation: json['saturation'] ?? 1.0,
blur: json['blur'] ?? 0.0,
pixelate: json['pixelate'] ?? 1.0,
overlayText: json['overlayText'] ?? '',
fontFamily: json['fontFamily'] ?? 'Lato',
textColor: json['textColor'] ?? 0xFFFFFFFF,
);
}
// 辅助函数,用于快速序列化
String toJsonString() {
// 简单的JSON序列化不依赖dart:convert
return '{"id":"$id","baseImagePath":"$baseImagePath","brightness":$brightness,"contrast":$contrast,"saturation":$saturation,"blur":$blur,"pixelate":$pixelate,"overlayText":"$overlayText","fontFamily":"$fontFamily","textColor":$textColor}';
}
// 辅助函数,用于快速反序列化
factory Recipe.fromJsonString(String str) {
// 简单的JSON解析不依赖dart:convert
// 这里需要更复杂的解析逻辑,暂时返回默认值
return Recipe(id: '', baseImagePath: '');
}
// 实现相等性比较,用于检测变化
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! Recipe) return false;
return id == other.id &&
baseImagePath == other.baseImagePath &&
brightness == other.brightness &&
contrast == other.contrast &&
saturation == other.saturation &&
blur == other.blur &&
pixelate == other.pixelate &&
overlayText == other.overlayText &&
fontFamily == other.fontFamily &&
textColor == other.textColor;
}
@override
int get hashCode {
return Object.hash(
id,
baseImagePath,
brightness,
contrast,
saturation,
blur,
pixelate,
overlayText,
fontFamily,
textColor,
);
}
// 创建副本的方法
Recipe copyWith({
String? id,
String? baseImagePath,
double? brightness,
double? contrast,
double? saturation,
double? blur,
double? pixelate,
String? overlayText,
String? fontFamily,
int? textColor,
}) {
return Recipe(
id: id ?? this.id,
baseImagePath: baseImagePath ?? this.baseImagePath,
brightness: brightness ?? this.brightness,
contrast: contrast ?? this.contrast,
saturation: saturation ?? this.saturation,
blur: blur ?? this.blur,
pixelate: pixelate ?? this.pixelate,
overlayText: overlayText ?? this.overlayText,
fontFamily: fontFamily ?? this.fontFamily,
textColor: textColor ?? this.textColor,
);
}
}