From b1540324e5e2a699162e0e2b6a35a7016d9cd0b6 Mon Sep 17 00:00:00 2001 From: Luis Date: Wed, 29 May 2024 01:20:18 +0100 Subject: [PATCH] Fix `SearchAnchor` suggestions not refreshing after long API call (#148767) Fixes https://github.com/flutter/flutter/issues/148765. ## Fix Demo https://github.com/flutter/flutter/assets/28322469/ad76ac8b-5e84-4fee-b04c-fbc9fa919450 --- .../lib/src/material/search_anchor.dart | 8 +++- .../test/material/search_anchor_test.dart | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/search_anchor.dart b/packages/flutter/lib/src/material/search_anchor.dart index cd3ca13e37..94a36af30e 100644 --- a/packages/flutter/lib/src/material/search_anchor.dart +++ b/packages/flutter/lib/src/material/search_anchor.dart @@ -792,9 +792,15 @@ class _ViewContentState extends State<_ViewContent> { _timer?.cancel(); _timer = Timer(Duration.zero, () async { searchValue = _controller.text; - result = await widget.suggestionsBuilder(context, _controller); + final Iterable suggestions = + await widget.suggestionsBuilder(context, _controller); _timer?.cancel(); _timer = null; + if (mounted) { + setState(() { + result = suggestions; + }); + } }); } } diff --git a/packages/flutter/test/material/search_anchor_test.dart b/packages/flutter/test/material/search_anchor_test.dart index 76ed139616..9f1c115b90 100644 --- a/packages/flutter/test/material/search_anchor_test.dart +++ b/packages/flutter/test/material/search_anchor_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:ui'; import 'package:flutter/foundation.dart'; @@ -3229,6 +3230,50 @@ void main() { expect(suggestionsBuilderCalledCount, 2); }); + + testWidgets('Suggestions gets refreshed after long API call', (WidgetTester tester) async { + Timer? debounceTimer; + const Duration apiCallDuration = Duration(seconds: 1); + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: Center( + child: SearchAnchor( + builder: (BuildContext context, SearchController controller) { + return const Icon(Icons.search); + }, + suggestionsBuilder: (BuildContext context, SearchController controller) async { + final Completer> completer = Completer>(); + debounceTimer?.cancel(); + debounceTimer = Timer(apiCallDuration, () { + completer.complete(List.generate(10, (int index) => 'Item - $index')); + }); + final List options = await completer.future; + + final List suggestions = List.generate(options.length, (int index) { + final String item = options[index]; + return ListTile( + title: Text(item), + ); + }); + return suggestions; + }, + ), + ), + ), + )); + await tester.tap(find.byIcon(Icons.search)); // Open search view. + await tester.pumpAndSettle(); + + // Simulate the keyboard opening resizing the view. + tester.view.viewInsets = const FakeViewPadding(bottom: 500.0); + addTearDown(tester.view.reset); + + // Show the keyboard. + await tester.showKeyboard(find.byType(TextField)); + await tester.pumpAndSettle(apiCallDuration); + + expect(find.text('Item - 1'), findsOneWidget); + }); } Future checkSearchBarDefaults(WidgetTester tester, ColorScheme colorScheme, Material material) async {