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),
              )

            ],
          ),
        ),
      ),
    );
  }
}
相关推荐
BD_Marathon44 分钟前
Vue3_响应式数据的处理方式
前端·javascript·vue.js
武玄天宗1 小时前
第四章、用flutter创建登录界面
flutter
嚣张丶小麦兜1 小时前
Vue常用工具库
前端·javascript·vue.js
搬砖的kk1 小时前
基于Flutter开发应用如何快速适配HarmonyOS
flutter·华为·harmonyos
昼-枕2 小时前
Flutter深度解析:如何构建高性能、跨平台的移动应用
flutter
小飞侠在吗2 小时前
Vue customRef
前端·javascript·vue.js
指尖跳动的光2 小时前
判断页签是否为活跃状态
前端·javascript·vue.js
音浪豆豆_Rachel3 小时前
Flutter 与原生通信的桥梁:深入解析 Pigeon 与后台线程通信
flutter·harmonyos
嚣张丶小麦兜4 小时前
认识vite
前端·javascript·vue.js
音浪豆豆_Rachel4 小时前
Flutter跨平台通信的智能配置:Pigeon注解配置与鸿蒙生态深度集成
flutter·华为·harmonyos