When I tap on a button I want to navigate to the second screen Focus on TextField(there is only one) and raise a keyboard.
I successfully focused when I taped on a widget on the same screen using FocusNode.
CodePudding user response:
Use autofocus: true,
TextField(autofocus: true,
CodePudding user response:
First Page :
class firstPage extends StatefulWidget {
firstPage({Key? key}) : super(key: key);
@override
State<firstPage> createState() => _firstPageState();
}
class _firstPageState extends State<firstPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("First Page")),
body: Center(
child: ElevatedButton(
child: Text("go to second page"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(
focusOnTextField: false,
)),
);
}),
));
}
}
Second page :
class SecondPage extends StatefulWidget {
SecondPage({Key? key, this.focusOnTextField}) : super(key: key);
bool? focusOnTextField;
@override
State<SecondPage> createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Second Page")),
body: Center(
child: TextField(
autofocus: widget.focusOnTextField ?? false,
)),
);
}
}