Skip to content

Commit

Permalink
Merge pull request #206 from muhammad-hassan-shakeel/master
Browse files Browse the repository at this point in the history
Thank you for your contribution
  • Loading branch information
maheshj01 authored Jan 28, 2025
2 parents 31a74e4 + 89dd700 commit e2a0554
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/src/searchfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ class _SearchFieldState<T> extends State<SearchField<T>> {
// if a item was already selected
if (highlightIndex >= 0) {
highlightIndex = widget.suggestions.indexWhere(
(element) => element == oldWidget.suggestions[highlightIndex]);
(element) => element == oldWidget.suggestions[highlightIndex - 1]);
}
}
if (oldWidget.scrollbarDecoration != widget.scrollbarDecoration) {
Expand All @@ -721,7 +721,6 @@ class _SearchFieldState<T> extends State<SearchField<T>> {
if (oldWidget.selectedValue != widget.selectedValue) {
// highlightIndex = widget.suggestions
// .indexWhere((element) => element == widget.selectedValue);

searchController!.text = widget.selectedValue?.searchKey ?? '';
}
if (oldWidget.searchInputDecoration != widget.searchInputDecoration) {
Expand Down Expand Up @@ -752,6 +751,7 @@ class _SearchFieldState<T> extends State<SearchField<T>> {
filteredResult.addAll(widget.suggestions);
// hide the suggestions
suggestionStream.sink.add(null);
searchController!.text = item.searchKey;
if (widget.onSuggestionTap != null) {
widget.onSuggestionTap!(item);
}
Expand Down
45 changes: 45 additions & 0 deletions test/searchfield_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,51 @@ void main() {
});

group('Suggestions should respect suggestionDirection', () {
testWidgets(
'you should be able to select a suggestion with SuggestionDirection.up',
(WidgetTester tester) async {
SearchFieldListItem<String>? selectedValue = null;
final boilerPlate = _boilerplate(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
SearchField(
suggestionDirection: SuggestionDirection.up,
key: const Key('searchfield'),
suggestions: ['ABC', 'DEF', 'GHI', 'JKL']
.map(SearchFieldListItem<String>.new)
.toList(),
selectedValue: selectedValue,
onSuggestionTap: (SearchFieldListItem<String> x) {
selectedValue = x;
},
),
],
),
);
await tester.pumpWidget(boilerPlate);
await tester.pumpAndSettle();
final listFinder = find.byType(ListView);
final textField = find.byType(TextFormField);
expect(textField, findsOneWidget);
expect(listFinder, findsNothing);
await tester.tap(textField);
await tester.enterText(textField, '');
await tester.pumpAndSettle();
expect(listFinder, findsOneWidget);
// tap 2nd item
await tester.ensureVisible(find.text('DEF'));
final secondItem = find.text('DEF').first;
expect(secondItem, findsOneWidget);
await tester.ensureVisible(find.text('DEF'));
await tester.tap(secondItem);
await tester.pumpAndSettle();
expect(selectedValue!.searchKey, equals('DEF'));
expect(
(textField.evaluate().first.widget as TextFormField).controller?.text,
'DEF');
});

testWidgets(
'suggestions should respect suggestionDirection: SuggestionDirection.up',
(WidgetTester tester) async {
Expand Down

0 comments on commit e2a0554

Please sign in to comment.