调出键盘:

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

监听按键:

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

完整代码

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');
  }

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

            ],
          ),
        ),
      ),
    );
  }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐