ArcticZeroo / flutter-search-bar

(mostly) Automatic search-enabled appBar for flutter
BSD 3-Clause "New" or "Revised" License
266 stars 69 forks source link

onClosed() is not fired when go "back" on navigation bar or by gesture #45

Open troncomputers opened 3 years ago

troncomputers commented 3 years ago

Hi! Would you guys be able to look at this. Inside scaffold's body I have a FutureBuilder and I'm filtering data in onChanged() function.

  _ProductsScreenState() {
    searchBar = SearchBar(
        setState: setState,
        buildDefaultAppBar: buildAppBar,
        inBar: true,
        hintText: "Wyszukaj...",
        onSubmitted: print,
        onChanged: (value) async {
          if (!["", null, false, 0].contains(value)) {
            var products = await productsService.getProducts();
            var filteredProducts =
                products.where((element) => element.ean!.contains(value) || element.name.contains(value) || element.code.contains(value)).toList();
            setState(() {
              _products = Future.value(filteredProducts);
            });
          } else {
            _products = productsService.getProducts();
          }
        },
        onCleared: () {
          _products = productsService.getProducts();
        },
        onClosed: () {
          print("onClosed");
          _products = productsService.getProducts();
        });
  }

Function onClosed() is fired only when I tap on "back" icon provided by this plugin.

ArcticZeroo commented 3 years ago

I cannot reproduce this, at least using the example in the latest pr #46 . Are you perhaps recreating the state too often, which would cause a new search bar to be created?

troncomputers commented 3 years ago

I've changed things a little bit. onChanged() is not async anymore. As you can see I'm setting the state on every key pressed

        onChanged: (value) {
          if (!["", null, false, 0].contains(value)) {
            var customers = customersService.getCustomersFromBox();
            var filteredCustomers = customers
                .where((element) =>
                    element.code.toLowerCase().contains(value.toLowerCase()) ||
                    (element.name1 + " " + element.name2 + " " + element.name3).toLowerCase().contains(value.toLowerCase()) ||
                    element.vatNumber!.toLowerCase().contains(value.toLowerCase()))
                .toList();
            setState(() {
              _customers = Future.value(filteredCustomers);
            });
          }
        },

Is that the issue?