如何使BottomNavigationBar停留在键盘颤动的顶部

时间:2018-11-18 18:17:03

标签: dart flutter

我正在尝试制作一个简单的聊天应用程序,因此我创建了一个脚手架,我的身体将成为消息,而我的bottomNavigationBar将成为我的键入字段和发送图标。

我添加了一个文本字段,但是在键入导航栏时,键盘将其隐藏。

这是我的BottomNavigationBar的代码:

bottomNavigationBar: new Container(
          height: ScreenSize.height/12,
          /*color: Colors.red,*/

          child: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,

            children: <Widget>[
              new Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  new Container(
                    child: new Icon(Icons.send),

                    width:ScreenSize.width/6,
                  ),
                ],
              ),
              new Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  Material(
                    child: new Container(
                      child: new TextField(
                        autofocus: false,
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.all(9.0),
                          border: InputBorder.none,
                          hintText: 'Please enter a search term',
                        ),
                      ),
                      width:ScreenSize.width*4/6,
                    ),
                      elevation: 4.0,
                    /*borderRadius: new BorderRadius.all(new Radius.circular(45.0)),*/
                    clipBehavior: Clip.antiAlias,
                    type: MaterialType.card,
                  )
                ],
              ),
              new Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  new Container(
                    child: Text('HELLO C1'),
                    color: Colors.green,
                    width:ScreenSize.width/6,
                  ),
                ],
              )




            ],
          ),
        ),

这是聚焦时的样子:

enter image description here

5 个答案:

答案 0 :(得分:1)

如果在脚手架的身体上使用Stack,而不是bottomNavigationBar,则导航将向上推到键盘上方。即使您使用“排名”固定到底部:

Positioned(
   bottom: 0.0,
   left: 0.0,
   right: 0.0,
   child: MyNav(),
),

答案 1 :(得分:1)

实际上只是解决了同样的问题。鉴于我正在重构的代码,这就像一个魅力。偷看 github 链接,查看他的更改并申请。再简单不过了:https://github.com/GitJournal/GitJournal/commit/f946fe487a18b2cb8cb1d488026af5c64a8f2f78.

上面链接的内容,以防链接失效:

(-)BottomAppBar buildEditorBottonBar(
(+)Widget buildEditorBottonBar(
  BuildContext context,
  Editor editor,
  EditorState editorState,
BottomAppBar buildEditorBottonBar(
    folderName = "Root Folder";
  }

  *REPLACE* return BottomAppBar(
    elevation: 0.0,
    color: Theme.of(context).scaffoldBackgroundColor,
    child: Row(
      children: <Widget>[
        FlatButton.icon(
          icon: Icon(Icons.folder),
          label: Text(folderName),
          onPressed: () {
            var note = editorState.getNote();
            editor.moveNoteToFolderSelected(note);
          },
        )
      ],
      mainAxisAlignment: MainAxisAlignment.center,

  *WITH THE WRAPPER* return StickyBottomAppBar(
    child: BottomAppBar(
      elevation: 0.0,
      color: Theme.of(context).scaffoldBackgroundColor,
      child: Row(
        children: <Widget>[
          FlatButton.icon(
            icon: Icon(Icons.folder),
            label: Text(folderName),
            onPressed: () {
              var note = editorState.getNote();
              editor.moveNoteToFolderSelected(note);
            },
          )
        ],
        mainAxisAlignment: MainAxisAlignment.center,
      ),
    ),
  );
}

class StickyBottomAppBar extends StatelessWidget {
  final BottomAppBar child;
  StickyBottomAppBar({@required this.child});

  @override
  Widget build(BuildContext context) {
    return Transform.translate(
      offset: Offset(0.0, -1 * MediaQuery.of(context).viewInsets.bottom),
      child: child,
    );
  }
}

答案 2 :(得分:0)

我试图做同样的事情,并且遇到了这个非常简单的解决方案:

https://www.reddit.com/r/FlutterDev/comments/8ao6ty/how_to_make_bottom_appbar_that_sticks_to_the_top/

这种想法将来可能对某人有用

答案 3 :(得分:-1)

如果你需要某种按钮;你可以这样做:

Scaffold(
            bottomNavigationBar: bottomNavigationBar,
            floatingActionButton: ExampleButton(
              text: 'Hello',
            ),
            body: body,
          ),

您可以使用 Scaffold 中的参数对浮动操作按钮应用进一步的自定义。

答案 4 :(得分:-2)

如果您真的需要使用脚手架的底部导航栏来放置小部件而不是将其放在堆栈上,那么有一种简单的方法可以做到这一点。只需用另一个脚手架包裹您的脚手架,它应该可以解决问题。

 return Scaffold(
      body: Scaffold(
        bottomNavigationBar: yourBottomNavigationBarWidget(),
        body: yourBody(),

这效果最好,尤其是当您的小部件的高度动态变化时(因为用户输入的文本可能会引入多行)并且您希望正文相应地调整大小。正如许多人所建议的那样,堆栈中的主体需要底部填充才能在文本字段上可见,并且需要根据用户类型动态更改,当文本字段中和周围有多个小部件时,这很难处理。< /p>