调出键盘:
void callKeyboard() {
SystemChannels.textInput.invokeMethod<void>('TextInput.show');
}
监听按键:文章来源:https://www.toymoban.com/news/detail-695003.html
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(""),
)
完整代码文章来源地址https://www.toymoban.com/news/detail-695003.html
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),
)
],
),
),
),
);
}
}
到了这里,关于flutter 调出键盘和监听输入的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!