import 'package:flutter/material.dart';

class EditableLabel extends StatefulWidget {
  final String initialValue;
  final String? hintText; 
  final ValueChanged<String>? onChanged;
  final TextInputType? keyboardType;

  const EditableLabel({super.key, required this.initialValue, this.onChanged, this.hintText, this.keyboardType});

  @override
  State<EditableLabel> createState() => _EditableLabelState();
}

class _EditableLabelState extends State<EditableLabel> {
  bool editMode = false;
  String? value;

  Widget _editMode() {
    return Row(
      children: [
        SizedBox(
          width: 200,
          height: 50,
          child: TextFormField(
            keyboardType: widget.keyboardType,
            decoration: InputDecoration(
              hintText: widget.hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(5),
              ),
            ),
            initialValue: widget.initialValue,
            onChanged: (value) => this.value = value,
          )
        ),
        IconButton(
          onPressed: () => setState(() {
            editMode = !editMode;
          }),
          icon: const Icon(Icons.close),
        ),
        IconButton(
          onPressed: () => setState(() {
            editMode = !editMode;
            if (value != null && widget.onChanged != null) {
              widget.onChanged!(value!);
            }
          }),
          icon: const Icon(Icons.save),
        ),
      ],
    );
  }

  Widget _readMode() {
    return Row(
      children: [
        Text(widget.initialValue),
        IconButton(
          onPressed: () => setState(() {
            editMode = !editMode;
          }),
          icon: const Icon(Icons.edit),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return editMode ? _editMode() : _readMode();
  }
}