import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:rkm/widgets/text_default.dart';
import '../config/color_palette.dart';
import '../config/const_variable.dart';
import 'input_search_text.dart';

class DropDownButtonComPesquisa extends StatelessWidget {
  var onChanged;
  String? select;
  String title;
  double fontSize;
  List list;
  double width;
  double heightDinamic;
  double widthContainer;

  DropDownButtonComPesquisa({
    required this.onChanged,
    required this.select,
    required this.title,
    required this.list,
    required this.width,
    this.heightDinamic = 0.2,
    this.fontSize = 15,
    this.widthContainer = 300,
});

  @override
  Widget build(BuildContext context) {

    double height = MediaQuery.of(context).size.height;

    return
      Container(
        width: width,
        height: height*heightDinamic,
        margin: ConstVariable().marginHightInput,
        color: ColorPalette.white,
        child: ListView(
          shrinkWrap: true,
          children: [
            TypeAheadField(
              controller: TextEditingController(),
              hideOnEmpty: false,
              suggestionsCallback: (pattern) {
                if (list != null) {
                  // Use toList() para converter o resultado do where em uma lista
                  return list.where((value) => value.toLowerCase().contains(pattern.toLowerCase())).toList();
                } else {
                  return Future.value([]); // Retorna uma lista vazia como um Future
                }
              },
              emptyBuilder:  (context) {
                return ListTile(
                  title: Text('Nenhum resultado encontrado'),
                );
              },
              itemBuilder: (context, dynamic suggestion) {
                return ListTile(
                  title: Text(suggestion),
                );
              },
              onSelected: onChanged,
            ),
          ],
        ),
      );
  }
}
