flutter 调出键盘和监听输入

调出键盘:

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

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

猜你喜欢

转载自blog.csdn.net/shelutai/article/details/132665274
今日推荐