調(diào)出鍵盤:
void callKeyboard() {
SystemChannels.textInput.invokeMethod<void>('TextInput.show');
}
監(jiān)聽按鍵:文章來源:http://www.zghlxwxcb.cn/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(""),
)
完整代碼文章來源地址http://www.zghlxwxcb.cn/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),
)
],
),
),
),
);
}
}
到了這里,關(guān)于flutter 調(diào)出鍵盤和監(jiān)聽輸入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!