颤动:打开抽屉时弹出键盘

时间:2020-09-08 14:21:00

标签: flutter

每个人 使用Flutter时遇到问题。 基本上,当我打开CustomDrawer小部件时(并非总是但非常频繁),键盘会以不需要的方式弹出。

我不知道为什么要这么做……也许是因为它重新运行了build方法或我不知道的东西。在下面,您可以找到代码。

每一点信息都会受到赞赏。

谢谢大家。

这是Screen.dart

...

build(context) {

    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      drawer: CustomDrawer(),

...

还有custom_drawer_widget.dart

...
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Column(
        children: [
          Stack(
            children: [
              Container(
                padding: EdgeInsets.all(20),
                child: Text(
                  "Hi, $username",
                  style: TextStyle(
                    fontSize: 22,
                  ),
                ),
                alignment: Alignment.bottomLeft,
                color: Colors.yellow,
                height: 300,
              ),
            ],
          ),
          Container(
            height: 60.0 * 6,
            child: Column(
              children: [
                Container(
                  height: 60,
                  child: FlatButton(
                    padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                    highlightColor: Colors.grey[350],
                    color: Colors.transparent,
                    onPressed: () {},
                    child: Row(
                      children: [
                        Icon(
                          Icons.home,
                          color: Colors.grey[600],
                        ),
                        SizedBox(width: 30),
                        Text("Homepage"),
                      ],
                    ),
                  ),
                ),
                ListTile(
                    leading: Icon(Icons.book),
                    title: Text("Diary"),
                    onTap: () {}),
                ListTile(
                    leading: Icon(Icons.chat),
                    title: Text("Chat"),
                    onTap: () {}),
                ListTile(
                    leading: Icon(Icons.credit_card),
                    title: Text("Credit Card"),
                    onTap: () {}),
                ListTile(
                    leading: Icon(Icons.exit_to_app),
                    title: Text("Sign Out"),
                    onTap: () async {
                      await FirebaseAuth.instance.signOut();
                    }),
                ListTile(
                    leading: Icon(Icons.settings),
                    title: Text("Settings"),
                    onTap: () {}),
              ],
            ),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.all(22),
              alignment: Alignment.bottomLeft,
              child: Row(
                children: [
                  Text("Version 0.1"),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
...

1 个答案:

答案 0 :(得分:2)

我没有看到您关闭键盘的确切位置,但是在提交表单后关闭键盘后我遇到了同样的问题。这解决了我的问题。

在此处查看此答案: https://github.com/flutter/flutter/issues/54277

代替:

      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
      },

代码应该是:

      onTap: () {
        final FocusScopeNode currentScope = FocusScope.of(context);
        if (!currentScope.hasPrimaryFocus && currentScope.hasFocus) {
          FocusManager.instance.primaryFocus.unfocus();
        }
      },