80 lines
2.1 KiB
Dart
80 lines
2.1 KiB
Dart
// * Shared widget for date selection across the application
|
|
// * Features:
|
|
// * - Date picker dialog interface
|
|
// * - Formatted date display
|
|
// * - Customizable button label
|
|
// * - Date range validation
|
|
// * - Callback for date changes
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Widget for managing date selection
|
|
/// Provides a button to open date picker and displays selected date
|
|
class Datum extends StatefulWidget {
|
|
/// Initial date value
|
|
final DateTime? initDatum;
|
|
/// Callback function when date changes
|
|
final Function(DateTime) onDateChanged;
|
|
/// Label for the date picker button
|
|
final String name;
|
|
|
|
const Datum(
|
|
{super.key, required this.initDatum, required this.onDateChanged, required this.name});
|
|
|
|
@override
|
|
State<Datum> createState() => _DatumState();
|
|
}
|
|
|
|
/// State class for the date selection widget
|
|
class _DatumState extends State<Datum> {
|
|
/// Currently selected date
|
|
DateTime? datum;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
datum = widget.initDatum;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Row(
|
|
children: [
|
|
Row(children: [
|
|
// Date picker button
|
|
SizedBox(
|
|
width: 140,
|
|
child: ElevatedButton(
|
|
onPressed: () async {
|
|
final date = await pickDate();
|
|
if (date == null) return;
|
|
setState(() => datum = date);
|
|
widget.onDateChanged(date);
|
|
},
|
|
child: Text(widget.name)),
|
|
),
|
|
const SizedBox(
|
|
width: 10,
|
|
),
|
|
// Formatted date display
|
|
Text(
|
|
'${datum?.day}. ${datum?.month}. ${datum?.year}',
|
|
),
|
|
]),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// Shows date picker dialog and returns selected date
|
|
/// @return Future[DateTime?] Selected date or null if cancelled
|
|
Future<DateTime?> pickDate() async {
|
|
final date = await showDatePicker(
|
|
context: context,
|
|
initialDate: datum!,
|
|
firstDate: DateTime(2000),
|
|
lastDate: DateTime(5000));
|
|
|
|
return date;
|
|
}
|
|
}
|