I have a Flutter app targeting Android TV devices (as well as phones), so I had to implement some special logic to handle the remote.
I wrapped buttons that need to be clicked by a remote with a Focus
widget:
Widget myButton(BuildContext context) {
Focus(
onKeyEvent: (node, event) {
return manageKeyboard(event, context);
},
child: IconButton(
// ... button properties here
),
);
}
I wrote manageKeyboard
method like this:
KeyEventResult manageKeyboard(KeyEvent event, BuildContext context) {
if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
if (event is KeyUpEvent) {
doSomething(context, true);
}
return KeyEventResult.handled;
}
}
This works reasonably well. Problem is if I try to make managedKeyboard
asynchrounous in order to await doSomething
, I get below error in onKeyEvent
:
The argument type 'Future<KeyEventResult> Function(FocusNode, KeyEvent)' can't be assigned to the parameter type 'KeyEventResult Function(FocusNode, KeyEvent)?'.
How can I make onKeyEvent
asynchrounous?
2
Answers
You cant assign a future callback to ‘onKeyEvent’.
Instead, You may register a callback to ‘manageKeyboard’ as follows,
And keep ‘manageKeyboard’ method as follows,