Skip to content
Snippets Groups Projects
Commit 1c23e3be authored by doodlezucc's avatar doodlezucc
Browse files

display relative week instead of exact date

parent 297974aa
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,26 @@ class DateSelector extends StatelessWidget { ...@@ -11,6 +11,26 @@ class DateSelector extends StatelessWidget {
final DateSelectedCallback onSelect; final DateSelectedCallback onSelect;
final DateTime date; final DateTime date;
String get weekRelativeString {
final currentWeekStart = DateTime.now().atStartOfWeek();
final selectedWeekStart = date.atStartOfWeek();
final difference = selectedWeekStart.difference(currentWeekStart);
final differenceInWeeks = (difference.inHours / 24 / 7).round();
if (differenceInWeeks >= 2) {
return 'In $differenceInWeeks Wochen';
} else if (differenceInWeeks == 1) {
return 'Nächste Woche';
} else if (differenceInWeeks <= -2) {
return 'Vor ${differenceInWeeks.abs()} Wochen';
} else if (differenceInWeeks == -1) {
return 'Letzte Woche';
}
return 'Aktuell';
}
const DateSelector({ const DateSelector({
super.key, super.key,
required this.date, required this.date,
...@@ -21,23 +41,14 @@ class DateSelector extends StatelessWidget { ...@@ -21,23 +41,14 @@ class DateSelector extends StatelessWidget {
const duration = oneWeek; const duration = oneWeek;
if (forward) { if (forward) {
onSelect(date.add(duration)); onSelect(date.add(duration).atStartOfWeek());
} else { } else {
onSelect(date.subtract(duration)); onSelect(date.subtract(duration).atStartOfWeek());
} }
} }
void onTap(BuildContext context) async { void onTap(BuildContext context) async {
final result = await showDatePicker( onSelect(DateTime.now().atStartOfWeek());
context: context,
initialDate: date,
firstDate: DateTime(2000, 1, 1),
lastDate: DateTime.now().add(const Duration(days: 60)),
);
if (result != null) {
onSelect(result);
}
} }
@override @override
...@@ -53,7 +64,7 @@ class DateSelector extends StatelessWidget { ...@@ -53,7 +64,7 @@ class DateSelector extends StatelessWidget {
child: SizedBox( child: SizedBox(
width: 100, width: 100,
child: Text( child: Text(
date.format(), weekRelativeString,
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
), ),
......
...@@ -18,8 +18,8 @@ class _ToolbarState extends State<Toolbar> { ...@@ -18,8 +18,8 @@ class _ToolbarState extends State<Toolbar> {
return Container( return Container(
color: Theme.of(context).colorScheme.background, color: Theme.of(context).colorScheme.background,
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Expanded(child: Container()),
DateSelector( DateSelector(
date: widget.appContext.date.value, date: widget.appContext.date.value,
onSelect: (v) => setState(() { onSelect: (v) => setState(() {
......
...@@ -22,4 +22,8 @@ extension DateExtension on DateTime { ...@@ -22,4 +22,8 @@ extension DateExtension on DateTime {
return '$dd.$mm.$year'; return '$dd.$mm.$year';
} }
DateTime atStartOfWeek() {
return subtract(Duration(days: weekday - 1));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment