feat:增加hive模块

This commit is contained in:
2025-11-07 17:21:21 +08:00
parent 0f106f6adc
commit 00e65bc5be
12 changed files with 579 additions and 43 deletions

View File

@@ -1,48 +1,81 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
class TodoItem {
num id;
String title;
String content;
bool isCompleted;
DateTime? dueDate;
TodoPriority priority;
part 'todo.g.dart';
TodoItem({
required this.id,
@HiveType(typeId: 0)
class Todo extends HiveObject {
@HiveField(0)
late int id;
@HiveField(1)
late String title;
@HiveField(2)
late String content;
@HiveField(3)
late bool isCompleted;
@HiveField(4)
late DateTime? dueDate;
@HiveField(5)
late int priorityIndex;
@HiveField(6)
late DateTime createTime;
@HiveField(7)
late DateTime updateTime;
TodoPriority get priority => TodoPriority.values[priorityIndex];
set priority(TodoPriority value) => priorityIndex = value.index;
Todo({
int? id,
required this.title,
required this.content,
this.content = '',
this.isCompleted = false,
this.dueDate,
this.priority = TodoPriority.medium,
});
TodoPriority priority = TodoPriority.medium,
DateTime? createTime,
DateTime? updateTime,
}) {
// 简单时间戳ID
this.id = id ?? DateTime.now().millisecondsSinceEpoch;
this.priorityIndex = priority.index;
this.createTime = createTime ?? DateTime.now();
this.updateTime = updateTime ?? DateTime.now();
}
TodoItem copyWith({
num? id,
Todo copyWith({
String? title,
String? content,
bool? isCompleted,
DateTime? dueDate,
TodoPriority? priority,
}) {
return TodoItem(
id: id ?? this.id,
return Todo(
id: id,
title: title ?? this.title,
content: content ?? this.content,
isCompleted: isCompleted ?? this.isCompleted,
dueDate: dueDate ?? this.dueDate,
priority: priority ?? this.priority,
createTime: createTime,
updateTime: DateTime.now(),
);
}
static TodoItem getEmpty() {
return TodoItem(
id: 0,
title: '',
content: '',
isCompleted: false,
dueDate: null,
priority: TodoPriority.medium,
static Todo getEmpty() {
return Todo(
id: 0,
title: '',
content: '',
isCompleted: false,
dueDate: null,
priority: TodoPriority.medium,
);
}
}

61
lib/models/todo.g.dart Normal file
View File

@@ -0,0 +1,61 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'todo.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class TodoAdapter extends TypeAdapter<Todo> {
@override
final int typeId = 0;
@override
Todo read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Todo(
id: fields[0] as int?,
title: fields[1] as String,
content: fields[2] as String,
isCompleted: fields[3] as bool,
dueDate: fields[4] as DateTime?,
createTime: fields[6] as DateTime?,
updateTime: fields[7] as DateTime?,
)..priorityIndex = fields[5] as int;
}
@override
void write(BinaryWriter writer, Todo obj) {
writer
..writeByte(8)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.title)
..writeByte(2)
..write(obj.content)
..writeByte(3)
..write(obj.isCompleted)
..writeByte(4)
..write(obj.dueDate)
..writeByte(5)
..write(obj.priorityIndex)
..writeByte(6)
..write(obj.createTime)
..writeByte(7)
..write(obj.updateTime);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is TodoAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}