I have a list of data that I am searching on. I want to catch the event of pressing the down arrow on the keyboard so I used widget:KeyboardListener.
There is indeed a listener for the event, but each time it prints the last entry in the list and not the entry I clicked on with the down arrow.
Here is the code:
`itemBuilder: (context, index) => Builder(builder: (context) {
final myCurrentItem=snapshot.data![index]!.searchKey;
if (selected == index) {
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
Scrollable.ensureVisible(context,
alignment: 0.1, duration: Duration(milliseconds: 300));
});
}
return TextFieldTapRegion(
onTapOutside: (x) {
if (widget.onTapOutside != null) widget.onTapOutside!(x);
_searchFocus!.unfocus();
},
child: Material(
color: widget.suggestionsDecoration == null
? Theme.of(context).colorScheme.surface
: Colors.transparent,
child: InkWell(
key: snapshot.data![index]!.key,
hoverColor:
widget.suggestionsDecoration?.hoverColor ??
Theme.of(context).hoverColor,
onTap: () =>
onSuggestionTapped(snapshot.data![index]!),
child: KeyboardListener(
focusNode: widget.focusNode!,
autofocus: true,
onKeyEvent: (value) {
if (value.logicalKey ==
LogicalKeyboardKey.arrowDown) {
print(
"${snapshot.data![index]!.searchKey} snapshot.data![index]!.searchKey");
}
},
child: Container(
key: snapshot.data![index]!.key,
width: double.infinity,
decoration: widget.suggestionItemDecoration
?.copyWith(
color: selected == index
? widget.suggestionsDecoration
?.selectionColor ??
Theme.of(context).highlightColor
: null,
border: widget
.suggestionItemDecoration?.border ??
Border(
bottom: BorderSide(
color: widget.marginColor ??
onSurfaceColor.withOpacity(0.1),
),
),
) ??
BoxDecoration(
color: selected == index
? widget.suggestionsDecoration
?.selectionColor ??
Theme.of(context).highlightColor
: null,
border: index == snapshot.data!.length - 1
? null
: Border(
bottom: BorderSide(
color: widget.marginColor ??
onSurfaceColor
.withOpacity(0.1),
),
),
),
child: Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: snapshot.data![index]!.child ??
Text(
snapshot.data![index]!.searchKey,
style: widget.suggestionStyle,
),
)),
),
),
)));
}),
),
);`