skip to Main Content

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.

2

Answers


  1. Use autofocus: true,

     TextField(autofocus: true,
    
    Login or Signup to reply.
  2. 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,
          )),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search