I'm developing a desktop app on Windows and am trying to move focus to a Textfield when the ENTER key is pressed on another textfield.
I'm able to detect the key has been pressed using RawKeyboardListener
but focus isnt changed to the new field. How can I get this working?
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,
),
],
),
),
]
),
),
),
);
}
}