
import 'package:flutter/material.dart';

class DateTimeFormField extends StatefulWidget {
  final DateTime date;
  final DateTime? minDate;
  final DateTime? maxDate;
  final TextEditingController controller;
  final String label;
  final void Function(DateTime?) onChanged;

  const DateTimeFormField(
      {Key? key,
      required this.date,
      this.minDate,
      this.maxDate,
      required this.controller,
      required this.label,
      required this.onChanged})
      : super(key: key);

  @override
  _DateTimeFormFieldState createState() => _DateTimeFormFieldState();
}

class _DateTimeFormFieldState extends State<DateTimeFormField> {
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      readOnly: true,
      controller: widget.controller,
      decoration: InputDecoration(
        labelText: widget.label,
      ),
      onTap: () async {
        final newTime = await showDatePicker(
          context: context,
          initialDate: widget.date,
          firstDate: widget.minDate ?? DateTime(2000, 1, 1),
          lastDate:
              widget.maxDate ?? DateTime.now().add(const Duration(days: 365)),
        );
        widget.onChanged(newTime);
      },
    );
  }
}
