
import 'package:flutter/material.dart';

class TimeOfDayFormField extends StatefulWidget {
  final TimeOfDay time;
  final TextEditingController controller;
  final String label;
  final void Function(TimeOfDay?) onChanged;

  const TimeOfDayFormField(
      {Key? key,
      required this.time,
      required this.controller,
      required this.label,
      required this.onChanged})
      : super(key: key);

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

class _TimeOfDayFormFieldState extends State<TimeOfDayFormField> {
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      readOnly: true,
      controller: widget.controller,
      decoration: InputDecoration(
        labelText: widget.label,
      ),
      onTap: () async {
        final newTime = await showTimePicker(
          context: context,
          initialTime: widget.time,
        );
        widget.onChanged(newTime);
      },
    );
  }
}