immich/mobile/lib/presentation/widgets/search/quick_date_picker.dart

71 lines
2.3 KiB
Dart
Raw Normal View History

import 'package:easy_localization/easy_localization.dart';
2025-10-04 11:16:56 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2025-10-12 09:58:12 +00:00
import 'package:immich_mobile/extensions/translate_extensions.dart';
2025-10-04 11:16:56 +00:00
class QuickDatePicker extends HookWidget {
const QuickDatePicker({super.key, required this.onSelect, required this.onRequestPicker});
final Function() onRequestPicker;
final Function(DateTimeRange<DateTime> range) onSelect;
void _selectRange(DateTimeRange range) {
// Ensure we don't go beyond today, eg when picking "in $current_year"
final now = DateTime.now();
if (range.end.isAfter(now)) {
range = DateTimeRange(start: range.start, end: now);
}
onSelect(range);
}
2025-10-12 09:58:12 +00:00
ListTile _monthListTile(BuildContext context, int monthsFromNow) {
String label = 'last_months'.t(context: context, args: {"count": monthsFromNow.toString()});
2025-10-04 11:16:56 +00:00
return ListTile(
title: Text(label),
2025-10-04 11:16:56 +00:00
onTap: () {
final now = DateTime.now();
// We use the first of the target month here to avoid issues with different month lengths
// the negative overflow of months is handled by DateTime correctly
final from = DateTime(now.year, now.month - monthsFromNow, 1);
_selectRange(DateTimeRange(start: from, end: now));
2025-10-04 11:16:56 +00:00
},
);
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
if (index == 0) {
return ListTile(
title: Text('pick_exact_date'.tr()),
2025-10-04 11:16:56 +00:00
onTap: () {
onRequestPicker();
},
);
} else if (index == 1) {
2025-10-12 09:58:12 +00:00
return _monthListTile(context, 1);
2025-10-04 11:16:56 +00:00
} else if (index == 2) {
2025-10-12 09:58:12 +00:00
return _monthListTile(context, 3);
2025-10-04 11:16:56 +00:00
} else if (index == 3) {
2025-10-12 09:58:12 +00:00
return _monthListTile(context, 9);
2025-10-04 11:16:56 +00:00
} else {
final now = DateTime.now();
final years = index - 4;
2025-10-04 11:16:56 +00:00
final year = now.year - years;
return ListTile(
title: Text("in_year".tr(namedArgs: {"year": year.toString()})),
2025-10-04 11:16:56 +00:00
onTap: () {
final from = DateTime(year, 1, 1);
final to = DateTime(year, 12, 31, 23, 59, 59);
_selectRange(DateTimeRange(start: from, end: to));
2025-10-04 11:16:56 +00:00
},
);
}
},
itemCount: 50,
);
}
}