Skip to content

Commit

Permalink
Merge pull request #139 from maheshmnj/fix-138
Browse files Browse the repository at this point in the history
OnSuggestionTapped returns SearchFieldItem when selected using keyboard
  • Loading branch information
maheshj01 authored May 23, 2024
2 parents 63bcec8 + 5d991d1 commit ce6df6f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#### [1.0.1] - March 22, 2024
#### [1.0.2] - May 23, 2024

- Fix: onSuggestionTap returns empty searchKey usng keyboard [Issue #138](https://github.com/maheshmnj/searchfield/issues/138)

#### [1.0.1] - March 22, 2024

- emptyWidget was incorrectly displayed [Fix Issue #132](https://github.com/maheshmnj/searchfield/issues/132)
- adds animationDuration to customize the list animation duration
- Scroll to bottom and top of list using alt+down and alt+up keys

#### [1.0.0] - March 20, 2024
#### [1.0.0] - March 20, 2024

- ListView is always kept in state to maintain scrolloffset [Issue #122](https://github.com/maheshmnj/searchfield/issues/122)
- Shift+Tab should respect sequence of SearchField in a form with SuggestionState.hidden [Issue #125](https://github.com/maheshmnj/searchfield/issues/125)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/key_intents.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PreviousIntent extends Intent {

// action to select the suggestion
class SelectionIntent<T> extends Intent {
final SearchFieldListItem<T> selectedItem;
final SearchFieldListItem<T>? selectedItem;
const SelectionIntent(this.selectedItem);
}

Expand Down
11 changes: 7 additions & 4 deletions lib/src/searchfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ class _SearchFieldState<T> extends State<SearchField<T>> {
void handleSelectKeyPress(SelectionIntent<T> intent) {
if (selected == null) return;
_searchFocus!.unfocus();
onSuggestionTapped(intent.selectedItem);
if (intent.selectedItem != null) {
onSuggestionTapped(intent.selectedItem!);
} else {
onSuggestionTapped(widget.suggestions[selected!]);
}
}

void handleUnFocusKeyPress(UnFocusIntent intent) {
Expand Down Expand Up @@ -786,9 +790,8 @@ class _SearchFieldState<T> extends State<SearchField<T>> {
LogicalKeySet(LogicalKeyboardKey.arrowDown): const NextIntent(false),
LogicalKeySet(LogicalKeyboardKey.arrowUp):
const PreviousIntent(false),
LogicalKeySet(LogicalKeyboardKey.enter): SelectionIntent<T>(
widget.initialValue ?? SearchFieldListItem(''),
),
LogicalKeySet(LogicalKeyboardKey.enter):
SelectionIntent<T>(widget.initialValue),
},
child: Actions(
actions: actions,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: searchfield
description: A highly customizable, simple and easy to use flutter Widget to add a searchfield to your Flutter Application. This Widget allows you to search and select from list of suggestions.
version: 1.0.1
version: 1.0.2
homepage: https://github.com/maheshmnj/searchfield
repository: https://github.com/maheshmnj/searchfield
issue_tracker: https://github.com/maheshmnj/searchfield/issues
Expand Down
34 changes: 34 additions & 0 deletions test/searchfield_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,40 @@ void main() {
});
});

// testWidgets(
// 'Selecting suggestion with keyboard should return the searchKey in `onSuggestionTapped`',
// (widgetTester) async {
// final controller = TextEditingController();
// final countries = data.map(Country.fromMap).toList();
// final suggestions =
// countries.map((e) => SearchFieldListItem<Country>(e.name)).toList();
// String? searchKey;
// await widgetTester.pumpWidget(_boilerplate(
// child: SearchField(
// key: const Key('searchfield'),
// suggestions: suggestions,
// controller: controller,
// suggestionState: Suggestion.expand,
// onSuggestionTap: (tapped) {
// print(tapped.searchKey);
// searchKey = tapped.searchKey;
// },
// )));

// final listFinder = find.byType(ListView);
// final textField = find.byType(TextFormField);
// expect(textField, findsOneWidget);
// expect(listFinder, findsNothing);
// await widgetTester.tap(textField);
// await widgetTester.enterText(textField, '');
// await widgetTester.pumpAndSettle();
// expect(listFinder, findsOneWidget);
// await simulateKeyDownEvent(LogicalKeyboardKey.arrowDown);
// await simulateKeyDownEvent(LogicalKeyboardKey.enter);
// await widgetTester.pumpAndSettle();
// expect(searchKey, countries[0]);
// });

// testWidgets('searched suggestions can be selected by pressing enter',
// (widgetTester) async {
// final controller = TextEditingController();
Expand Down

0 comments on commit ce6df6f

Please sign in to comment.