随笔 - 210  文章 - 0 评论 - 0 阅读 - 61148

使用 Focus 和 onKey

复制代码
class CategoryView extends GetView<CategoryController> {
  const CategoryView({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
      final FocusNode _focusNode = FocusNode();

    return Scaffold(
      appBar: AppBar(
        title: const Text('CategoryView'),
        centerTitle: true,
      ),
      body:
          Focus(
            autofocus: true,
            onKey: (FocusNode focusNode, RawKeyEvent event) {
              if (event is RawKeyDownEvent) {
                print('Key Pressed: ${event.logicalKey.keyLabel}');
                // 在这里处理键盘事件
              }
              return KeyEventResult.handled;
            },
            child: Container(
              width: double.infinity,
              height: double.infinity,
              alignment: Alignment.center,
              child: Text('Tap anywhere to focus and start typing'),
            ),
            focusNode: _focusNode,
          ),
      );
    
  }
}
复制代码

 

posted on   鲤斌  阅读(186)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示