我想构建一个可以快速访问硬键按钮的应用程序。我该如何实施?

时间:2019-07-29 10:01:08

标签: flutter

例如,当我按下音量调高或调低按钮时,它将显示小吃栏“音量调高”。

1 个答案:

答案 0 :(得分:0)

阅读RawKeyEvents:https://api.flutter.dev/flutter/widgets/RawKeyboardListener-class.html

您可以根据需要修改此代码,当从音量按钮(例如音量按钮)以及物理键盘(已通过蓝牙键盘测试)的按钮中截获事件时,将打印此代码

class MinimalExample extends StatelessWidget {
  final FocusNode focusNode = FocusNode();

  rawKeyInfo(RawKeyEvent e){
    print("Raw input: ${e.physicalKey.usbHidUsage}");
    print(e.character);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          RawKeyboardListener(
            focusNode: focusNode,
            onKey: (rawKeyEvent) => rawKeyInfo(rawKeyEvent),
            child: TextField(
              onChanged: (str) => null, //print("TextField has: $str"),
            ),
          ),
        ],
      ),
    );
  }
}