import 'package:farm_tpf/utils/const_color.dart';
import 'package:flutter/material.dart';

class SearchWidget extends StatefulWidget {
  final Function(String) searchPressed;
  SearchWidget({required this.searchPressed});
  @override
  _SearchWidgetState createState() => _SearchWidgetState();
}

class _SearchWidgetState extends State<SearchWidget> {
  TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _searchController.addListener(() {
      final keyword = _searchController.text;
      if (keyword.isNotEmpty) {
        //search when text change
      }
    });
  }

  Widget getSearchBarUI() {
    _searchController.text = "";
    return Padding(
      padding: const EdgeInsets.only(left: 8, right: 8, top: 0, bottom: 4),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(right: 8, top: 0, bottom: 0),
              child: Container(
                child: Padding(
                  padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 4),
                  child: TextField(
                    textInputAction: TextInputAction.done,
                    controller: _searchController,
                    onChanged: (String txt) {},
                    cursorColor: AppColors.GRAY1,
                    decoration: InputDecoration(
                      suffixIcon: IconButton(
                          icon: const Icon(
                            Icons.search,
                            size: 30,
                          ),
                          onPressed: () {
                            widget.searchPressed(_searchController.text);
                          }),
                      hintText: 'Tìm thiết bị',
                      hintStyle: TextStyle(color: Colors.grey[500]),
                    ),
                    onSubmitted: widget.searchPressed,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(child: getSearchBarUI());
  }

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }
}
