Skip to content

Commit

Permalink
fix: Loading when no suggestions (openfoodfacts#4669)
Browse files Browse the repository at this point in the history
Loading spinner was showing, even if there were no loading or
suggestions available.

Fixes openfoodfacts#4669
  • Loading branch information
WildOrangutan committed Sep 30, 2023
1 parent da78240 commit acebdd8
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class _SimpleInputTextFieldState extends State<SimpleInputTextField> {
if (_manager == null || search.length < widget.minLengthForSuggestions) {
_suggestions[search] = _SearchResults.empty();
} else {
_setLoading(true);
try {
_suggestions[search] =
_SearchResults(await _manager!.getSuggestions(search));
Expand All @@ -87,7 +88,7 @@ class _SimpleInputTextFieldState extends State<SimpleInputTextField> {
}

if (_suggestions[search]?.isEmpty == true && search == _searchInput) {
_hideLoading();
_setLoading(false);
}

if (_searchInput != search &&
Expand Down Expand Up @@ -123,7 +124,6 @@ class _SimpleInputTextFieldState extends State<SimpleInputTextField> {
VoidCallback onFieldSubmitted) =>
TextField(
controller: widget.controller,
onChanged: (_) => setState(() => _loading = true),
decoration: InputDecoration(
filled: true,
border: const OutlineInputBorder(
Expand Down Expand Up @@ -170,7 +170,7 @@ class _SimpleInputTextFieldState extends State<SimpleInputTextField> {
}

if (input == _searchInput) {
_hideLoading();
_setLoading(false);
}

return AutocompleteOptions<String>(
Expand Down Expand Up @@ -199,10 +199,10 @@ class _SimpleInputTextFieldState extends State<SimpleInputTextField> {

String get _searchInput => widget.controller.text.trim();

void _hideLoading() {
if (_loading) {
void _setLoading(bool loading) {
if (_loading != loading) {
WidgetsBinding.instance.addPostFrameCallback(
(_) => setState(() => _loading = false),
(_) => setState(() => _loading = loading),
);
}
}
Expand Down

0 comments on commit acebdd8

Please sign in to comment.