From 4ade7c2c6e2db844c5c644addba1bbb150fa2ea3 Mon Sep 17 00:00:00 2001 From: Sonu Sharma Date: Mon, 27 May 2024 21:45:09 +0530 Subject: [PATCH] Fix: search filter issue after its being empty Ref:-https://github.com/TheAlphamerc/filterlist/issues/49 --- lib/src/filter_list_widget.dart | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/src/filter_list_widget.dart b/lib/src/filter_list_widget.dart index 02ffd05..cd4d09b 100644 --- a/lib/src/filter_list_widget.dart +++ b/lib/src/filter_list_widget.dart @@ -187,10 +187,28 @@ class FilterListWidget extends StatelessWidget { hideCloseIcon: hideCloseIcon, headerCloseIcon: headerCloseIcon, onSearch: (String value) { + final stateList = FilterState.of(context).items; + + // Reset filter list if search box is empty if (value.isEmpty) { FilterState.of(context).items = listData; return; } + // Reassign items to filter list when it is empty but local list has data + else if (stateList != null && + stateList.isEmpty && + listData != null && + listData!.isNotEmpty) { + final isFoundInLocalState = + listData!.any((item) => onItemSearch(item, value)); + + if (isFoundInLocalState) { + FilterState.of(context).items = listData! + .where((item) => onItemSearch(item, value)) + .toList(); + return; + } + } FilterState.of(context) .filter((item) => onItemSearch(item, value)); },