flutter 调出键盘和监听输入

调出键盘:

dart 复制代码
  void callKeyboard() {
    SystemChannels.textInput.invokeMethod<void>('TextInput.show');
  }

监听按键:

dart 复制代码
RawKeyboardListener(
                autofocus: true,
                onKey: (event) {
                  if (event.runtimeType == RawKeyDownEvent) {
                    if(event.data is RawKeyEventDataAndroid){
                      RawKeyEventDataAndroid datga = event.data as RawKeyEventDataAndroid;
                      ///获取按键键值 keycode
                      // _value = datga.keyCode.toString();
                      print('flutter down: '+datga.keyCode.toString());
                    }
                  }
                },
                focusNode: FocusNode(),
                child:  Text(""),
              )

完整代码

dart 复制代码
import 'package:flutter/material.dart';

import 'package:flutter/services.dart';


void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  void callKeyboard() {
    SystemChannels.textInput.invokeMethod<void>('TextInput.show');
  }

  @override
  Widget build(BuildContext context) {


    const title = 'Horizontal List';

    String _value = "test";

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: const Text(title),
        ),
        body: Container(
          margin: const EdgeInsets.symmetric(vertical: 20),
          height: 200,
          child: ListView(
            // This next line does the trick.
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              Container(
                width: 160,
                color: Colors.red,
              ),
              Container(
                width: 160,
                color: Colors.blue,
              ),
              Container(
                width: 160,
                color: Colors.green,
              ),
              Container(
                width: 160,
                color: Colors.yellow,
              ),
              Container(
                width: 160,
                color: Colors.orange,
                child: OutlinedButton(
                  child: Text("normal"),
                  onPressed: callKeyboard,
                )
              ),
              RawKeyboardListener(
                autofocus: true,
                onKey: (event) {
                  if (event.runtimeType == RawKeyDownEvent) {
                    if(event.data is RawKeyEventDataAndroid){
                      RawKeyEventDataAndroid datga = event.data as RawKeyEventDataAndroid;
                      ///获取按键键值 keycode
                      // _value = datga.keyCode.toString();
                      print('flutter down: '+datga.keyCode.toString());
                    }
                  }
                },
                focusNode: FocusNode(),
                child:  Text(_value),
              )

            ],
          ),
        ),
      ),
    );
  }
}
相关推荐
怕浪猫1 小时前
第一章 JSX 增强特性与函数组件入门
前端·javascript·react.js
2501_946242932 小时前
MPV-EASY Player (MPV播放器) v0.41.0.1
数据库·经验分享·云计算·计算机外设·github·电脑·csdn开发云
前端小L2 小时前
贪心算法专题(十):维度权衡的艺术——「根据身高重建队列」
javascript·算法·贪心算法
Fortunate Chen3 小时前
类与对象(下)
java·javascript·jvm
低保和光头哪个先来5 小时前
场景6:对浏览器内核的理解
开发语言·前端·javascript·vue.js·前端框架
ji_shuke6 小时前
canvas绘制拖拽箭头
开发语言·javascript·ecmascript
2501_946244786 小时前
Flutter & OpenHarmony OA系统设置页面组件开发指南
开发语言·javascript·flutter
cz追天之路6 小时前
华为机考 ------ 识别有效的IP地址和掩码并进行分类统计
javascript·华为·typescript·node.js·ecmascript·less·css3
l1t7 小时前
DeepSeek总结的算法 X 与舞蹈链文章
前端·javascript·算法