Feat: Macht die Trend-Seite funktional
This commit is contained in:
111
lib/Pages/Misc/InputFields/date_range_picker.dart
Normal file
111
lib/Pages/Misc/InputFields/date_range_picker.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:flutter/material.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.autofocus = false,
|
||||
super.key,
|
||||
this.decoration,
|
||||
this.initialValue,
|
||||
this.onChanged,
|
||||
});
|
||||
|
||||
/// Der Initiale Wert des Input-Feldes
|
||||
final DateTimeRange? initialValue;
|
||||
|
||||
/// 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;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _DateRangePicker();
|
||||
}
|
||||
|
||||
class _DateRangePicker extends State<DateRangePicker> {
|
||||
final ValueNotifier<DateTimeRange?> _value = ValueNotifier(null);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_value.value = widget.initialValue;
|
||||
_value.addListener(() {
|
||||
widget.onChanged?.call(_value.value);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(final BuildContext context) => TextField(
|
||||
readOnly: true,
|
||||
controller: TextEditingController(
|
||||
text: widget.initialValue != null
|
||||
? '${widget.initialValue!.start.day}'
|
||||
'.${widget.initialValue!.start.month}'
|
||||
'.${widget.initialValue!.start.year}'
|
||||
' - '
|
||||
'${widget.initialValue!.end.day}'
|
||||
'.${widget.initialValue!.end.month}'
|
||||
'.${widget.initialValue!.end.year}'
|
||||
: null,
|
||||
),
|
||||
decoration: widget.decoration,
|
||||
onTap: _pickDateRange,
|
||||
);
|
||||
|
||||
Future<void> _pickDateRange() async {
|
||||
await DynamicDialog(
|
||||
title: 'Zeitraum auswählen',
|
||||
inputFields: [
|
||||
DialogInputField(
|
||||
id: 'dateFrom',
|
||||
label: 'Startdatum',
|
||||
keyboardType: TextInputType.datetime,
|
||||
inputType: DialogInputFieldTypeEnum.date,
|
||||
initialValue: _value.value?.start,
|
||||
),
|
||||
DialogInputField(
|
||||
id: 'dateTo',
|
||||
label: 'Enddatum',
|
||||
keyboardType: TextInputType.datetime,
|
||||
inputType: DialogInputFieldTypeEnum.date,
|
||||
initialValue: _value.value?.end,
|
||||
),
|
||||
],
|
||||
actions: [
|
||||
DialogAction(label: 'Abbrechen'),
|
||||
DialogAction(
|
||||
label: 'Leeren',
|
||||
onPressed: (_) {
|
||||
_value.value = 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) {
|
||||
_value.value = DateTimeRange(
|
||||
start: values['dateFrom'],
|
||||
end: values['dateTo'],
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
).show();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user