cpd/lib/database/habit.dart

49 lines
1.2 KiB
Dart

import 'package:flutter/material.dart';
class Habit {
final int id;
bool isComplete;
String title;
String subtitle;
int iconCodePoint;
String iconFontFamily;
Habit({
required this.id,
required this.isComplete,
required this.title,
required this.subtitle,
required this.iconCodePoint,
required this.iconFontFamily,
});
IconData get icon => IconData(iconCodePoint, fontFamily: iconFontFamily);
void updateIcon(int newCodePoint, String newFontFamily) {
iconCodePoint = newCodePoint;
iconFontFamily = newFontFamily;
}
factory Habit.fromSqfliteDatabase(Map<String, dynamic> map) {
return Habit(
id: map['id']?.toInt() ?? 0,
isComplete: map['isComplete'] == 1,
title: map['title'] ?? '',
subtitle: map['subtitle'] ?? '',
iconCodePoint: map['iconCodePoint']?.toInt() ?? Icons.favorite.codePoint,
iconFontFamily: map['iconFontFamily'] ?? 'MaterialIcons',
);
}
//konvertiert das Habit-Objekt in eine Map zum Speichern in der DB
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'subtitle': subtitle,
'isComplete': isComplete ? 1 : 0,
'iconCodePoint': iconCodePoint,
'iconFontFamily': iconFontFamily,
};
}
}