【发布时间】:2021-06-25 03:03:08
【问题描述】:
我正在 Windows 上开发一个桌面应用程序,并试图在另一个文本字段上按下 ENTER 键时将焦点移到一个文本字段。
我能够使用RawKeyboardListener 检测到已按下键,但焦点未更改为新字段。我怎样才能让它工作?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late FocusNode _txtNode;
@override
void initState() {
super.initState();
_txtNode = FocusNode();
}
@override
void dispose() {
_txtNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Stack(
children: [
Container(
height: 400,
width: 500,
color: Colors.grey[350],
child: Column(
children: [
Container(
height: 100,
child: RawKeyboardListener(
focusNode: FocusNode(),
onKey: (event){
if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Enter')) {
print("pressed ENTER");
_txtNode.requestFocus();
}
},
child: TextField(readOnly: true,))
),
TextField(
),
TextField(
focusNode: _txtNode,
),
],
),
),
]
),
),
),
);
}
}
【问题讨论】:
标签: flutter dart flutter-desktop