111 lines
3.2 KiB
Dart
111 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../Services/date_service.dart';
|
|
import '../../Dialog/dialog_action.dart';
|
|
import '../../Dialog/dialog_input_field.dart';
|
|
import '../../Dialog/dialog_input_field_type_enum.dart';
|
|
import '../../Dialog/dynamic_dialog.dart';
|
|
|
|
/// Stellt einen DateRange-Picker als Input-Feld dar
|
|
class DateRangePicker extends StatefulWidget {
|
|
/// Initialisiert eine neue Instanz dieser Klasse
|
|
const DateRangePicker({
|
|
this.controller,
|
|
this.autofocus = false,
|
|
super.key,
|
|
this.decoration,
|
|
this.onChanged,
|
|
});
|
|
|
|
/// Ob das Feld automatisch ausgewählt werden soll
|
|
final bool autofocus;
|
|
|
|
/// Die Funktion, die bei Veränderung des Wertes aufgerufen wird
|
|
final Function(DateTimeRange?)? onChanged;
|
|
|
|
/// Die Dekoration, wie das Feld aussehen soll
|
|
final InputDecoration? decoration;
|
|
|
|
/// Der Controller über den der Wert des Inputfeldes gesetzt
|
|
/// und ausgelesen werden kann
|
|
final TextEditingController? controller;
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _DateRangePicker();
|
|
}
|
|
|
|
class _DateRangePicker extends State<DateRangePicker> {
|
|
final TextEditingController _valueController = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_valueController.dispose();
|
|
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(final BuildContext context) => TextField(
|
|
readOnly: true,
|
|
controller: widget.controller ?? _valueController,
|
|
decoration: widget.decoration,
|
|
onTap: _pickDateRange,
|
|
);
|
|
|
|
Future<void> _pickDateRange() async {
|
|
final TextEditingController controller =
|
|
widget.controller ?? _valueController;
|
|
|
|
final DateTimeRange<DateTime>? dateTimeRange =
|
|
DateService.stringToDateTimeRange(controller.text);
|
|
|
|
await DynamicDialog(
|
|
title: 'Zeitraum auswählen',
|
|
inputFields: [
|
|
DialogInputField(
|
|
id: 'dateFrom',
|
|
label: 'Startdatum',
|
|
keyboardType: TextInputType.datetime,
|
|
inputType: DialogInputFieldTypeEnum.date,
|
|
initialValue: dateTimeRange?.start,
|
|
),
|
|
DialogInputField(
|
|
id: 'dateTo',
|
|
label: 'Enddatum',
|
|
keyboardType: TextInputType.datetime,
|
|
inputType: DialogInputFieldTypeEnum.date,
|
|
initialValue: dateTimeRange?.end,
|
|
),
|
|
],
|
|
actions: [
|
|
DialogAction(label: 'Abbrechen'),
|
|
DialogAction(
|
|
label: 'Leeren',
|
|
onPressed: (_) {
|
|
controller.text = '';
|
|
widget.onChanged?.call(null);
|
|
},
|
|
),
|
|
DialogAction(
|
|
label: 'Auswählen',
|
|
isPrimary: true,
|
|
onPressed: (final Map<String, dynamic> values) {
|
|
if (values['dateFrom'] != null &&
|
|
values['dateFrom'] is DateTime &&
|
|
values['dateTo'] != null &&
|
|
values['dateTo'] is DateTime) {
|
|
final DateTimeRange<DateTime> timeRange = DateTimeRange(
|
|
start: values['dateFrom'],
|
|
end: values['dateTo'],
|
|
);
|
|
|
|
controller.text = DateService.dateTimeRangeToString(timeRange)!;
|
|
widget.onChanged?.call(timeRange);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
).show();
|
|
}
|
|
}
|