I have a problem on iOS/web (I'm testing on an iPhone XR running iOS 15.4.1)
I have a TextField and a button and I want
- that the TextField gets focus initially and the keyboard shows up automatically
- that, after pressing the button, the TextField keeps the focus and the keyboard remains on screen.
Right now I have this
class _MyWidgetState extends State<MyWidget> {
final FocusNode focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
focusNode: focusNode,
autofocus: true,
decoration: InputDecoration(
hintText: "name2",
),
),
OutlinedButton(onPressed: (){
focusNode.requestFocus();
}, child: Text("Done"))
]
);
}
}
but the keyboard does not appear initially (though the TextField looks focused). It does appear after clicking once in the TextField but it disappears again after clicking on the "Done" button (which probably makes sense since I guess the "Done" button gets focus here)
I have basically two questions:
- why doesn't the keyboard appear initially?
- how can I keep it visible after pressing the "Done" button. I can "solve" this part by adding, in the initState, something like below, but I'm not sure this is the cleanest way to do this.
focusNode.addListener(() { if (!focusNode.hasFocus) { FocusScope.of(context).requestFocus(focusNode); } });