1

我正在使用 Flutter 开发 Android TV 应用程序。我能够运行该应用程序(现在是示例应用程序)。但我无法使用 d pad 选择按钮选择任何内容。

我发现我必须使用这样的东西才能实现这一点

捷径(

    shortcuts: {
      LogicalKeySet(LogicalKeyboardKey.select):
          const Intent(ActivateAction.key)
    },
    child: MaterialApp())

但它在代码中给了我一个错误。我究竟做错了什么?

任何帮助表示赞赏。谢谢你。

4

2 回答 2

8

添加此代码肯定有助于 Android TV OK/Select 按钮,并且可以扩展为其他映射。为 Flutter 1.24-dev 构建并完美运行

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Shortcuts(
      shortcuts: <LogicalKeySet, Intent>{
        LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
      },
      child: MaterialApp( 
于 2020-10-26T21:45:22.473 回答
1

你可以使用 RawKeyboardListener 而不是 Shortcuts,我使用了以下代码:

 body: RawKeyboardListener(
        autofocus: true,
        focusNode: _focusNode,
        onKey: _onKey,
        child:Center())
 

    void _onKey(RawKeyEvent e) {
    controls();

    if (e.runtimeType.toString() == 'RawKeyDownEvent') {
      switch (e.logicalKey.debugName) {
        case 'Media Play Pause':
        case 'Select':
          setState(() {
            if (_videoViewController.value.isPlaying) {
              _videoViewController.pause();
            } else {
              _videoViewController.play();
            }
          });
          break;
      }
    }`

现在,如果您想要快进或快退,只需使用带有特定大小写的 RawKeyboardListener 来处理它。

您也可以使用 D-PAD 键执行此类操作。

于 2021-02-27T08:48:40.420 回答