import 'package:flutter/material.dart';


class SearchBox extends StatelessWidget {
  final TextEditingController controller;
  final String hintText;
  final ValueChanged<String> onChanged;
  final VoidCallback? onClear;
  final String? Function(String?)? validator; 
  final String? labelText;
  final String? helperText;

  const SearchBox({
    super.key,
    required this.controller,
    required this.hintText,
    required this.onChanged,
    this.onClear,
    this.validator,
    this.labelText,
    this.helperText,
  });

  @override
  Widget build(BuildContext context) => TextFormField(
    controller: controller,
    onChanged: onChanged,
    validator: validator, 
    decoration: InputDecoration(
      prefixIcon: const Icon(Icons.search),
      suffixIcon: ValueListenableBuilder<TextEditingValue>(
        valueListenable: controller,
        builder: (context, value, child) => value.text.isNotEmpty
          ? IconButton(
              icon: const Icon(Icons.clear),
              onPressed: () {
                controller.clear();
                if (onClear != null) {
                  onClear!();
                }
                onChanged('');
              },
            )
          : const SizedBox.shrink(),
      ),
      hintText: hintText,
      labelText: labelText,
      helperText: helperText,
      border: const UnderlineInputBorder(),
    ),
  );
}
