aaronkelton
aaronkelton

Reputation: 409

Samsung autofill password breaks GestureDetector unfocus due to AutofillHints.password

I'm using the password autofill feature on my Android Samsung Galaxy A14, but after the keyboard updates the field, I lose my GestureDetector's onTap unfocus ability. Here's a video walkthrough demonstrating the problem, and the relevant snippet from a minimal reproduction (truncated for brevity):

GestureDetector(
  onTap: () => FocusManager.instance.primaryFocus?.unfocus(), // <-- unfocus
  child: Material(
    child: CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(middle: Text('First Route')),
      child: Column(
        children: [
          const TextField(
            autofillHints: [AutofillHints.password], // <-- this is culprit
          ),
          CupertinoButton(
            child: const Text('Open route'),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (_) => const SecondRoute()),
              );
            },
          ),
        ],
      ),
    ),
  ),
);

I'm expecting to be able to tap anywhere outside the text field, or tap the button to navigate to a new page, in order to dismiss the keyboard. But neither happen when they immediately follow a password text field autofill event. How might I either reenable the GestureDetector's onTap unfocus ability, or dismiss the keyboard after tapping the button and navigating to a new screen?

I have already tried using a focus node and text editing controller to dismiss the keyboard.

Note: for ease of testing, I'd recommend using an emulator or real device that has autofill data readily available.

Upvotes: 0

Views: 57

Answers (1)

aaronkelton
aaronkelton

Reputation: 409

I found a related answer with a systems level call:

SystemChannels.textInput.invokeMethod('TextInput.hide');

I added it to the onPressed field before navigating:

onPressed: () {
  SystemChannels.textInput.invokeMethod('TextInput.hide');
  Navigator.push(
    context,
    MaterialPageRoute(builder: (_) => const SecondRoute()),
  );
},

Upvotes: 0

Related Questions