fix: 'NoResultsWidget' coming up for a fraction of a second on the first query

This commit is contained in:
ashilkn 2023-12-26 14:39:21 +05:30
parent 682459e800
commit 608af962b4
3 changed files with 21 additions and 15 deletions

View file

@ -1,9 +1,15 @@
import "package:flutter/material.dart";
enum SearchState {
empty,
searching,
notEmpty,
}
class IndexOfStackNotifier with ChangeNotifier {
int _index = 0;
bool _isSearchQueryEmpty = true;
bool _isSearchResultsEmpty = true;
SearchState _searchState = SearchState.empty;
static IndexOfStackNotifier? _instance;
@ -16,20 +22,20 @@ class IndexOfStackNotifier with ChangeNotifier {
setIndex();
}
set isSearchResultsEmpty(bool value) {
_isSearchResultsEmpty = value;
set searchState(SearchState value) {
_searchState = value;
setIndex();
}
setIndex() {
if (_isSearchResultsEmpty) {
if (_isSearchQueryEmpty) {
_index = 0;
} else {
_index = 2;
}
if (_isSearchQueryEmpty) {
_index = 0;
} else {
_index = 1;
if (_searchState == SearchState.empty) {
_index = 2;
} else {
_index = 1;
}
}
notifyListeners();
}

View file

@ -44,6 +44,7 @@ class _SearchSuggestionsWidgetState extends State<SearchSuggestionsWidget> {
void initState() {
super.initState();
SearchWidgetState.searchResultsStreamNotifier.addListener(() {
IndexOfStackNotifier().searchState = SearchState.searching;
final resultsStream = SearchWidgetState.searchResultsStreamNotifier.value;
searchResultWidgets.clear();
@ -58,7 +59,7 @@ class _SearchSuggestionsWidgetState extends State<SearchSuggestionsWidget> {
//search bar is cleared, and the event fired by the stream will be an
//empty list. Can optimize rebuilds if there are performance issues in future.
if (searchResults.isNotEmpty) {
IndexOfStackNotifier().isSearchResultsEmpty = false;
IndexOfStackNotifier().searchState = SearchState.notEmpty;
}
queueOfSearchResults.add(searchResults);
},
@ -67,7 +68,7 @@ class _SearchSuggestionsWidgetState extends State<SearchSuggestionsWidget> {
const Duration(milliseconds: _surfaceNewResultsInterval + 20),
() {
if (searchResultWidgets.isEmpty) {
IndexOfStackNotifier().isSearchResultsEmpty = true;
IndexOfStackNotifier().searchState = SearchState.empty;
}
});
},

View file

@ -93,11 +93,10 @@ class SearchWidgetState extends State<SearchWidget> {
}
Future<void> textControllerListener() async {
final query = textController.text;
IndexOfStackNotifier().isSearchQueryEmpty = query.isEmpty;
_debouncer.run(() async {
if (mounted) {
final query = textController.text;
IndexOfStackNotifier().isSearchQueryEmpty = query.isEmpty;
searchResultsStreamNotifier.value =
_getSearchResultsStream(context, query);
}