icemanbsi / searchable_dropdown

MIT License
107 stars 162 forks source link

Search not working when working with List<DropdownMenuItem<Model>> #81

Open darkrevenger opened 4 years ago

darkrevenger commented 4 years ago

I'm not sure why, I get the text displayed but it just doesnt work. Here is the code and some screenshots. ` formarPaises() async { List pm = new List(); paises = List<DropdownMenuItem>();

await http.get(url + '/paises',
    headers: {
      'Accept': 'application/json'
    })
    .then((http.Response res) async {
  Map<String, dynamic> jsonr = json.decode(res.body);

  if (res.statusCode < 200 || res.statusCode > 400) {
    Fluttertoast.showToast(
        msg: "Error al obtener los paises",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 2,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
  } else {
    jsonr["data"].forEach((dd) {
      pm.add(new PaisModel.map(dd));
    });
  }
}).then((a) async {
  setState(() {
    paisModel = null;
    paises = paisDropDownItems(pm);
  });
});

}`

List<DropdownMenuItem<PaisModel>> paisDropDownItems(List<PaisModel> list) { List<DropdownMenuItem<PaisModel>> items = List(); for (PaisModel paisModel in list) { items.add( DropdownMenuItem( value: paisModel, child: Text(paisModel.Descripcion), ), ); } return items; }

Those two are to form the searchlist and to show it its this:

Card( elevation: 3, child: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all( Radius.circular(5.0), ), ), child: SearchableDropdown.single( iconEnabledColor: Colors.blue, iconDisabledColor: Colors.blue, displayClearIcon: false, underline: Container(), selectedValueWidgetFn: (t) { return Container( transform: Matrix4.translationValues(0,0,0), alignment: Alignment.centerLeft, child: (Text(paisModel.Descripcion))); }, hint: "Pais *", searchHint: "Pais *", value: paisModel, items: paises, onChanged: (v) { setState(() { paisModel = v; formarProvincias(v.PaisID); changeFocus(provinciaIDFocusNode); }); }, isExpanded: true, ), ), ),

The result is the next image but when I search image Here its another result with the same example but another api. image image What am I missing here?

darkrevenger commented 4 years ago

Maybe its cuz I'm using a old version of flutter? (Wich I need for this proyect :( )

WaheedHussainHaans commented 4 years ago

This can happen when Your drop down value and child are not the same

darkrevenger commented 4 years ago

This can happen when Your drop down value and child are not the same

I mean of course they are not the same, the value is the entire model, with the ID, description, etc, etc, and the child has to be a text or I'm wrong?

irzum commented 4 years ago

@darkrevenger am facing the same issue did you get the solution? search is not working image image

irzum commented 4 years ago

I found it you have put text in value field also so when you will search it will search according to text in my case what i did image search works with "value" field image

WaheedHussainHaans commented 4 years ago

I found it you have put text in value field also so when you will search it will search according to text in my case what i did image search works with "value" field image

Value in my case is the string you are providing in text widget

irzum commented 4 years ago

@WaheedHussainHaans its okay don't pass the Text widget, later i updated to string which was Value : name + "," + id I put both name n id so that it will work for search and i ll get the id also for later purpose.

mrazaimtiaz commented 3 years ago

if you are using class to forget to add toString override

@override String toString() { return this.name; }