132 lines
3.4 KiB
Dart
132 lines
3.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:todoapp/modals/edittodomodal.dart';
|
|
import 'package:todoapp/providers/todoprovider.dart';
|
|
|
|
class TodoWidget extends StatefulWidget {
|
|
const TodoWidget(
|
|
{super.key,
|
|
required this.checked,
|
|
required this.title,
|
|
required this.dueTime});
|
|
|
|
final bool checked;
|
|
final String title;
|
|
final DateTime dueTime;
|
|
|
|
@override
|
|
State<TodoWidget> createState() => _TodoWidgetState();
|
|
}
|
|
|
|
class _TodoWidgetState extends State<TodoWidget> {
|
|
String formatNumber(int number) {
|
|
if (number < 10) {
|
|
return "0$number";
|
|
}
|
|
return number.toString();
|
|
}
|
|
|
|
List<dynamic> buildDisplayString() {
|
|
DateTime today = DateTime.now();
|
|
DateTime due = widget.dueTime;
|
|
|
|
print(today);
|
|
print(due);
|
|
|
|
DateTime nowDay = DateTime(today.year, today.month, today.day);
|
|
DateTime dueDay = DateTime(due.year, due.month, due.day);
|
|
int diff = dueDay.difference(nowDay).inDays;
|
|
|
|
Map<int, String> monthList = {
|
|
1: "Jan",
|
|
2: "Feb",
|
|
3: "Mar",
|
|
4: "Apr",
|
|
5: "Mai",
|
|
6: "Jun",
|
|
7: "Jul",
|
|
8: "Aug",
|
|
9: "Sep",
|
|
10: "Okt",
|
|
11: "Nov",
|
|
12: "Dez"
|
|
};
|
|
|
|
print(diff);
|
|
|
|
if (diff < 0) {
|
|
return [
|
|
"Over",
|
|
Colors.red,
|
|
Colors.white
|
|
];
|
|
}
|
|
else if (diff < 1) {
|
|
return [
|
|
"Today ${formatNumber(due.hour)}:${formatNumber(due.minute)}",
|
|
Theme.of(context).colorScheme.primary,
|
|
Colors.black
|
|
];
|
|
} else if (diff < 2) {
|
|
return [
|
|
"Tomorrow ${formatNumber(due.hour)}:${formatNumber(due.minute)}",
|
|
Theme.of(context).colorScheme.inversePrimary,
|
|
Colors.white
|
|
];
|
|
} else if (diff < 7) {
|
|
return [
|
|
"In $diff days",
|
|
Theme.of(context).colorScheme.secondary.withOpacity(0.35),
|
|
Colors.white
|
|
];
|
|
}
|
|
else {
|
|
return [
|
|
"${monthList[due.month]} ${formatNumber(due.day)}, ${due.year}",
|
|
Theme.of(context).colorScheme.secondary.withOpacity(0.35),
|
|
Colors.white
|
|
];
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onLongPress: () {
|
|
EditTodoModal.add(context, widget.title, widget.dueTime);
|
|
},
|
|
child: CheckboxListTile(
|
|
value: widget.checked,
|
|
subtitle: Row(
|
|
children: [
|
|
Container(
|
|
padding:
|
|
widget.checked ? EdgeInsets.zero : const EdgeInsets.all(4),
|
|
decoration: BoxDecoration(
|
|
color: widget.checked
|
|
? const Color(0x00000000)
|
|
: buildDisplayString()[1],
|
|
borderRadius: BorderRadius.circular(10)),
|
|
child: Text(
|
|
buildDisplayString()[0],
|
|
style: TextStyle(
|
|
color:
|
|
widget.checked ? Colors.white : buildDisplayString()[2],
|
|
decoration: widget.checked
|
|
? TextDecoration.lineThrough
|
|
: TextDecoration.none),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
title: Text(widget.title,
|
|
style: widget.checked
|
|
? const TextStyle(decoration: TextDecoration.lineThrough)
|
|
: const TextStyle()),
|
|
onChanged: (value) =>
|
|
{context.read<TodoProvider>().setTodoState(widget.title, value!)},
|
|
),
|
|
);
|
|
}
|
|
}
|