Flutter:打开抽屉时键盘弹出

7 flutter

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

我不明白它为什么这样做......也许是因为它重新运行构建方法或者我不知道的东西。您可以在下面找到代码。

每一点点信息都受到高度赞赏。

感谢大家。

这是 Screen.dart

...

build(context) {

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

...
Run Code Online (Sandbox Code Playgroud)

和 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"),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
...
Run Code Online (Sandbox Code Playgroud)

Pet*_*r R 8

我不知道你在哪里关闭键盘,但在提交表单后关闭键盘后我遇到了同样的问题。这解决了我的问题。

在这里查看这个答案: https ://github.com/flutter/flutter/issues/54277

哪里代替:

      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
      },
Run Code Online (Sandbox Code Playgroud)

代码应该是:

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

Run Code Online (Sandbox Code Playgroud)