5

I have this example:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(30),
          child: GestureDetector(
            onTap: () {
              print('Hide keyboard!!!');
              FocusScope.of(context).unfocus();
            },
            child: Column(
              children: [
                Text(DateTime.now().toIso8601String()),
                TextFormField()
              ],
            ),
          ),
        ),
      ),
    );
  }

When the keyboard appears or is hidden it causes the widget to rebuild. Why does this happen?

1 Answers1

6

Actually, I couldn't find the reason behind the rebuild after using

FocusScope.of(context).unfocus();

But This one will help you to stop rebuild the widget.

FocusManager.instance.primaryFocus.unfocus();

It's working on my application.

Tushar Roy
  • 1,018
  • 10
  • 18
  • 1
    This is working for me with `TextFormField` as well ... using `FocusScope.of(context).unfocus();` I would see random widget tree redraws and could even sometimes get into a state where it wasn't possible to re-focus the widget. – Ben Roberts Jan 13 '23 at 16:12