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

            ],
          ),
        ),
      ),
    );
  }
}
相关推荐
ssshooter5 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
Live000006 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉6 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
忆江南7 小时前
iOS 深度解析
flutter·ios
球球pick小樱花7 小时前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
喝水的长颈鹿7 小时前
【大白话前端 02】网页从解析到绘制的全流程
前端·javascript
明君879977 小时前
Flutter 实现 AI 聊天页面 —— 记一次 Markdown 数学公式显示的踩坑之旅
前端·flutter
用户14536981458787 小时前
VersionCheck.js - 让前端版本更新变得简单优雅
前端·javascript
codingWhat7 小时前
整理「祖传」代码,就是在开发脚手架?
前端·javascript·node.js
码路飞7 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python