使用 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 2024-07-15 15:35  鲤斌  阅读(122)  评论(0编辑  收藏  举报