import 'package:flutter/material.dart'; abstract class ListItem { String get id; String get title; int get width; int get height; int get difficulty; int get rows => difficulty; int get cols => difficulty; bool get hard; String get thumb; String get image; String get cachePath => 'cache/$id.jpeg'; String get jsonPath => 'work/$id.json'; ValueNotifier notifier = ValueNotifier(0); // 新增:用于序列化 Map toJson(); // 添加抽象方法,强制子类实现 } /// assets图片, 内置图 class AssetItem extends ListItem { @override final String id; @override final String title; @override final int width; @override final int height; @override final int difficulty; @override final bool hard; @override final String thumb; @override final String image; AssetItem(this.id, this.title, this.width, this.height, this.difficulty, this.hard, this.thumb, this.image); // 从JSON文件读取 AssetItem.fromJSON(Map json) : id = json['_id'], title = json['title'] ?? '', width = json['width'], height = json['height'], difficulty = json['difficulty'], hard = json['hard'] ?? false, thumb = json['thumb'], image = json['raw']; @override String toString() { return 'AssetItem(id=$id, width=$width, height=$height, rows=$rows, cols=$cols, thumb=$thumb, image=$image'; } @override Map toJson() => {'_id': id, 'width': width, 'height': height, 'difficulty': difficulty, 'hard': hard, 'thumb': thumb, 'raw': image}; } class RemoteItem extends ListItem { @override final String id; @override final String title; @override final int width; @override final int height; @override final int difficulty; @override final bool hard; @override final String thumb; @override final String image; RemoteItem(this.id, this.title, this.width, this.height, this.difficulty, this.hard, this.thumb, this.image); factory RemoteItem.fromJSON(Map json) { // return RemoteItem(json['_id'], json['width'], json['height'], json['difficulty'], json['raw']); return RemoteItem( json['_id'], json['title'] ?? '', json['width'], json['height'], json['difficulty'] ?? 3, json['hard'] ?? false, json['thumb'], json['raw'], ); // 临时本地调试 } @override Map toJson() => { '_id': id, 'title': title, 'width': width, 'height': height, 'difficulty': difficulty, 'hard': hard, 'thumb': thumb, 'raw': image, }; } // 记录已完成作品的信息 class Work extends ListItem { @override final String id; // 作品ID @override final String title; // 作品标题 @override final int difficulty; // 难度 (rows/cols) @override final bool hard; // 是否困难模式 @override final int width; // 原始图片宽度 @override final int height; // 原始图片高度 @override final String thumb; @override final String image; final DateTime completedTime; // 完成时间 final Duration? timeSpent; // 完成耗时 (可选) Work({ required this.id, required this.title, required this.difficulty, required this.hard, required this.width, required this.height, required this.thumb, required this.image, required this.completedTime, this.timeSpent, }); // 序列化为 JSON @override Map toJson() { return { 'id': id, 'title': title, 'difficulty': difficulty, 'hard': hard, 'width': width, 'height': height, 'thumb': thumb, 'image': image, 'completedTime': completedTime.toIso8601String(), // 日期时间转为ISO字符串 'timeSpentSeconds': timeSpent?.inSeconds, // 耗时转为秒 }; } // 从 JSON 反序列化 factory Work.fromJson(Map json) { return Work( id: json['id'], title: json['title'] ?? '', difficulty: json['difficulty'], hard: json['hard'] ?? false, width: json['width'], height: json['height'], thumb: json['thumb'], image: json['image'], completedTime: DateTime.parse(json['completedTime']), timeSpent: json['timeSpentSeconds'] != null ? Duration(seconds: json['timeSpentSeconds']) : null, ); } // 从 ListItem 创建 Work factory Work.fromListItem(ListItem item, {Duration? timeSpent}) { return Work( id: item.id, title: item.title, difficulty: item.difficulty, hard: item.hard, width: item.width, height: item.height, thumb: item.thumb, image: item.image, completedTime: DateTime.now(), timeSpent: timeSpent, ); } }