Flutter - Error filtering results using Searchdelegate

Asked

Viewed 64 times

0

Hello!

I’m having trouble filtering results inside a Searchdelegate.

I created a "alertDialog" where the user can select the filters, and put the "Apply" button (see image below) to automatically apply the filters and display the results, but nothing happens when I click this button. inserir a descrição da imagem aqui

However, when I select the filters I want, then I type what I want and click on "search" (see image below), the filters are applied.

Note: I already know why it works when I click on "search", because I passed the parameters inside buildResults, where it responds when you click on this button "search".

inserir a descrição da imagem aqui

My Searchdelegate code is this one:

class CustomSearchDelegate extends SearchDelegate<String> {

  List<DropdownMenuItem<String>> _listaItensDropCategorias;
  List<DropdownMenuItem<String>> _listaItensDropEstados;


  String _itemSelecionadoCategoria;
  String _itemSelecionadoEstado;

  _carregarItensDropdown(){

    //Categorias
    _listaItensDropCategorias = Configuracoes.getCategorias();

    //Estados
    _listaItensDropEstados = Configuracoes.getEstados();

  }



  _abrirDialog(BuildContext context) async {
    _carregarItensDropdown();
    return showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext context){
          return StatefulBuilder(
            builder: (BuildContext context,StateSetter setState){
              return AlertDialog(
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(top: 10, bottom: 20),
                      child: Text("Filtrar", style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 27),),
                    ),
                    Padding(
                      padding: EdgeInsets.only(bottom: 5),
                      child: Row(
                        children: <Widget>[
                          Text("Categoria", style: TextStyle(color: Colors.grey, fontSize: 20),),
                          Expanded(
                            child: Wrap(
                              alignment: WrapAlignment.end,
                              children: <Widget>[
                                DropdownButton(
                                  value: _itemSelecionadoCategoria,
                                  items: _listaItensDropCategorias,
                                  style: TextStyle(
                                      fontSize: 20,
                                      color: Colors.black
                                  ),
                                  onChanged: (categoria){
                                    setState(() {
                                      _itemSelecionadoCategoria = categoria;
                                    });
                                  },
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(bottom: 5),
                      child: Row(
                        children: <Widget>[
                          Text("Estado", style: TextStyle(color: Colors.grey, fontSize: 20),),
                          Expanded(
                            child: Wrap(
                              alignment: WrapAlignment.end,
                              children: <Widget>[
                                DropdownButton(
                                  value: _itemSelecionadoEstado,
                                  items: _listaItensDropEstados,
                                  style: TextStyle(
                                      fontSize: 20,
                                      color: Colors.black
                                  ),
                                  onChanged: (estado){
                                    setState(() {
                                      _itemSelecionadoEstado = estado;
                                    });
                                  },
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 30),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: Wrap(
                              alignment: WrapAlignment.end,
                              children: <Widget>[
                                Padding(
                                  padding: EdgeInsets.only(right: 30),
                                  child: GestureDetector(
                                    child: Text(
                                      "Aplicar",
                                      style: TextStyle(color: Colors.lightBlue),
                                    ),
                                    ***onTap: (){
                                      showResults(context);
                                      Navigator.pop(context);
                                    },***

                                  ),

                                ),
                                GestureDetector(
                                  child: Text(
                                    "Cancelar",
                                    style: TextStyle(color: Colors.lightBlue),
                                  ),
                                  onTap: (){
                                    Navigator.pop(context);
                                  },
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],),
              );
            },
          );
        }
    );
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = "";
        },
      ),
      IconButton(
        icon: Icon(Icons.tune),
        onPressed: () {
         _abrirDialog(context);
        },
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () {
        close(context, "");
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
      return Pesquisar(query.toUpperCase(),_itemSelecionadoCategoria,_itemSelecionadoEstado);
  }
   

  }

My question is in this "onTap" of Gesturedetector "Apply", what can I add there so that I can show the results without having to click the "Search" button? I thought adding "showResults(context);" would solve the problem, but it didn’t work.

Note.2: Search() is the file that displays the results.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.