如果使用FLutter开发TV电视端的APP,如果正确的根据按钮指令去电视上操作呢?
下面就是关于监听遥控器的有些相关代码:


import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class HomeIndexPage extends StatefulWidget {
  const HomeIndexPage({super.key});

  @override
  State<HomeIndexPage> createState() => _HomeIndexPageState();
}

class _HomeIndexPageState extends State<HomeIndexPage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return RawKeyboardListener(
      focusNode: FocusNode(),
      onKey: (value) {
        //debugPrint("遥控器:${value.data}");
        if (value.data is RawKeyEventDataAndroid) {
          var key = value.data as RawKeyEventDataAndroid;
          if (key.flags == 520) {
            //上19 下20 左21 右22 // 8是遥控器按下,520是遥控器 弹起
            if (key.keyCode == 19) {
              debugPrint("遥控器:${value.data}");
              // controller.previous();
            } else if (key.keyCode == 20) {
              debugPrint("遥控器:${value.data}");
              // controller.next();
            } else if (key.keyCode == 21) {
              debugPrint("遥控器:${value.data}");
              // controller.showMenu();
            } else if (key.keyCode == 22) {
              debugPrint("遥控器:${value.data}");
              // controller.showMenu(show: false);
            } else if (key.keyCode == 23) {
              debugPrint("遥控器:${value.data}");
              // controller.ok();
              _scaffoldKey.currentState?.openDrawer();
            }
          }
        }
      },
      child: Scaffold(
        key: _scaffoldKey,
        drawer:Drawer(
          child: Text('This is Drawer'),
        ),
        body: Center(
          child: Column(
            children: [
              Text("YouthTV"),
              TextButton(
                  onPressed: () {
                    _scaffoldKey.currentState?.openDrawer();
                  },
                  child: Text("打开侧栏")
              ),

            ],
          ),
        ),
      )


    );
  }
}

运行代码后,可以通过点击TV遥控器的确认键、上、下、左、右 等键来操作电视APP。

标签: Flutter, Flutter开发TV, Flutter监听遥控器按键

添加新评论