| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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,
- );
- }
- }
|