EN VI

Flutter : Wait for keyboard dismissed then call navigator.pop

2022-12-11 20:39:15

In screen B, there is a TextField with leading back icon on AppBar, when the user taps the leading "back" icon to return to screen A while the keyboard is open, it closes the keyboard and returns to screen A as expected, but there is a small glitch in screen A, it looks like the keyboard is not completely dismissed. its look like small vibration, disturbance on UI.

I tried to make a little adjustment with this code :

onPressed: () async {
  FocusScope.of(context).unfocus();
  await Future.delayed(const Duration(milliseconds: 300));
  Navigator.maybePop(context);
},

And the glitches on screen A are gone. but this method makes use of the context between async gaps which is not recommended by dart, any other way to solve this? Thank you in advance.

Solution:

Okay at the end of screen B try creating this class:

class AlwaysDisabledFocusNode extends FocusNode {
  @override
  bool get hasFocus => false;
}

then in your function try this:

onPressed: () async {
  AlwaysDisabledFocusNode();
  await Future.delayed(const Duration(milliseconds: 300));
if(mounted) return;
  Navigator.maybePop(context);
},
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login