Skip to content

Flutter TextField focus state is not correct when keyboard closed from platform side. #123523

@luckysmg

Description

@luckysmg

Now our unfocus signal always sends from framework side. But when platform side close the keyboard, we should sync this state to framework side and unfoucs the FocusNode to let UI state correct.

Now on flutter master channel:

Demo code
void main() {
  runApp(const MyApp());
}

class KeyboardPage extends StatefulWidget {
  const KeyboardPage({Key? key}) : super(key: key);

  @override
  State<KeyboardPage> createState() => _KeyboardPageState();
}

class _KeyboardPageState extends State<KeyboardPage> {
  FocusNode node = FocusNode();

  @override
  void initState() {
    super.initState();
    node.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            node.hasFocus ? 'focus' : 'unfocus',
            style: const TextStyle(fontSize: 26, color: Colors.black),
          ),
          CupertinoTextField(
            focusNode: node,
            decoration: const BoxDecoration(color: Colors.red),
          )
        ],
      ),
    );
  }
}
iPhone iPad Android
Screenrecorder-2023-03-27-15-17-18-568.mp4

TODO PRs:

Flutter framework PR:

iOS engine PR:

Android engine PR

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: text inputEntering text in a text field or keyboard related problemsplatform-androidAndroid applications specificallyplatform-iosiOS applications specifically

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions