import 'package:flutter/material.dart';

class FieldSearchBar extends StatelessWidget {
  final TextEditingController _searchController = TextEditingController();
  final FocusNode _searchFocusNode = FocusNode();
  final Function() deleteAction;
  final Function(String value) onChange;
  final String searchString;

  FieldSearchBar(
      {super.key,
      required this.deleteAction,
      required this.onChange,
      required this.searchString});

  @override
  Widget build(BuildContext context) {
    return SearchBar(
      controller: _searchController,
      focusNode: _searchFocusNode,
      elevation: WidgetStateProperty.all<double>(0.0),
      backgroundColor: WidgetStateProperty.all<Color>(Colors.white),
      side: WidgetStateProperty.all<BorderSide>(
        const BorderSide(
          color: Colors.black,
        ),
      ),
      leading: const Icon(Icons.search),
      trailing: [
        if (_searchFocusNode.hasFocus && _searchController.text.isNotEmpty)
          IconButton(
            onPressed: () {
              _searchController.clear();
              _searchFocusNode.unfocus();
              deleteAction;
            },
            icon: const Icon(
              Icons.clear,
              color: Colors.grey,
              size: 20,
            ),
          ),
      ],
      onChanged: onChange,
    );
  }
}
