Flutter - Keyboard Show and Hide Causes Build Calls

Based on Mazin Ibrahim answer, I managed to solve the keyboard build problem:

Define all your navigator widgets as final in case using routes.

In case of using routes, I managed to define all my navigation widgets as final fields inside the routes class.

That's stops of recreating the same widget more than once, especially that my widgets are all Stateless.


Keep in mind you should always write your build methods as if they're being called 60 times a second. So they should be (a) fast and (b) idempotent.


First is rebuilt because its position in the navigation stack changed.

As for your keyboard issue, this post tackled it, you just have to make a slight change to your code:

Change this :

  RaisedButton(
      onPressed: () {
         Navigator.push(context, MaterialPageRoute(builder: (context) => Second()));
      },
      child: Text('Open Second'),
  ),

  RaisedButton(
      onPressed: () {
         Navigator.push(context, MaterialPageRoute(builder: (context) => First()));
      },
      child: Text('Open First'),
  ),

To:

  RaisedButton(
      onPressed: () {
          final page = Second();
          Navigator.push(context,MaterialPageRoute(builder: (context) => page ));
      }, 
      child: Text('Open Second'),
  ),

  RaisedButton(
      onPressed: () {
          final page = First();
          Navigator.push(context,MaterialPageRoute(builder: (context) => page ));
      }, 
      child: Text('Open First'),
  ),

Tags:

Dart

Flutter