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

import 'bloc/plot_bloc.dart';

class WidgetSearch extends StatefulWidget {
  final TextEditingController searchController;
  final Function(String) onPressed;

  const WidgetSearch({
    super.key,
    required this.searchController,
    required this.onPressed,
  });
  @override
  _WidgetSearchState createState() => _WidgetSearchState();
}

class _WidgetSearchState extends State<WidgetSearch> {
  @override
  void initState() {
    super.initState();
    widget.searchController.addListener(() {
      final keyword = widget.searchController.text;
      if (keyword.isNotEmpty) {
        //search when text change
      }
    });
  }

  Widget getSearchBarUI() {
    // widget.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: widget.searchController,
                    onChanged: (String txt) {},
                    cursorColor: AppColors.GRAY1,
                    decoration: InputDecoration(
                        suffixIcon: IconButton(
                          icon: Icon(
                            Icons.search,
                            size: 30,
                          ),
                          onPressed: () {
                            widget.onPressed(widget.searchController.text);
                          },
                        ),
                        hintText: 'Tìm kiếm ...',
                        hintStyle: TextStyle(color: Colors.grey[500])),
                    onSubmitted: (value) {
                      widget.onPressed(widget.searchController.text);
                    },
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

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

  @override
  void dispose() {
    widget.searchController.dispose();
    super.dispose();
  }
}
