当我单击编辑它时,我的文本字段隐藏在 BottomNavigationBar 的键盘后面

My textfield hides behind keyboard in BottomNavigationBar when I click to edit it

我是 Flutter 新手。我被困在一个地方,找不到确切的解决方案。

我有一个 BottomNavigationBar,其中有一个需要用户输入的文本字段。当我单击文本字段时,键盘覆盖了整个半个屏幕,文本字段隐藏在键盘后面,看不到。我想将文本字段移至键盘上方,以便为用户提供更好的 UI。

这是 BottomNavigationBar 代码:

Widget build(BuildContext context) {
return Container(
  color: Colors.grey[200],
  height: 70,
  child: Row(
    children: <Widget>[
      SizedBox(
        height: MediaQuery.of(context).viewInsets.bottom,
      ),
      Container(
        width: MediaQuery.of(context).size.width * 0.6,
        color: Colors.transparent,
        margin: EdgeInsets.fromLTRB(40, 0, 0, 0),
        child: TextField(  //this is the TextField
          style: TextStyle(
            fontSize: 30,
            fontFamily: 'Karla',
          ),
          decoration: InputDecoration.collapsed(
            hintText: 'Experimez-vous...',
            hintStyle: TextStyle(color: Colors.black),
          ),
        ),
      ),
    ],
  ),
);

这是主体(Scaffold)我调用它来自:

return Scaffold(
  body: Column(
    children: <Widget>[
      CloseButtonScreen(),
      Container(
        color: Colors.transparent,
        child: HeaderContent(),
      ),
      ContainerListItems(),
    ],
  ),
  bottomNavigationBar: BottomNavBar(), //this is where Im calling my BottomNavigationBar from
  floatingActionButton: FloatingActionBtn(),
  floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
  resizeToAvoidBottomPadding: false,
);

截图:

底部导航栏中的文本字段位于键盘后面

体验 texfield

将所有小部件放在 SingleChildScrollView 小部件中。

更新:使用 Stack 而不是 BottomNavigationBar 来显示文本字段

将其包裹在 SingleChildScrollView 中将允许您的小部件在出现键盘时向上滑动。

Scaffold(
  body: SingleChildScrollView(
   child: Column(
      children: <Widget>[
        CloseButtonScreen(),
        Container(
          color: Colors.transparent,
          child: HeaderContent(),
        ),
        ContainerListItems(),
     ],
   ),
 ),
...
);

只需将每个小部件放入 ListView 并使用 shrinkWrap 属性。示例代码如下:

return Scaffold(
  backgroundColor: Colors.white,
  body: new Container(
    child: new ListView(
      shrinkWrap: true,
      padding: EdgeInsets.only(left: 12.0, right: 12.0),
      children: <Widget>[
        widget1,
        SizedBox(height: 20.0),
        widget2,
        SizedBox(height: 20.0),
        widget3,
      ],
    ),
  ),
);    

经过大量的努力,这就是我实现它的方式。感谢大家的贡献。

return Scaffold(
  body: GestureDetector(
    onTap: () => {
      FocusScope.of(context).unfocus(),
    },
    child: SingleChildScrollView(
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          children: <Widget>[
            CloseButtonScreen(),
            Container(
              color: Colors.transparent,
              child: HeaderContent(),
            ),
            ContainerListItems(),
            Container(
              margin: EdgeInsets.only(top: 90),
              padding: EdgeInsets.only(left: 20),
              color: Colors.transparent,
              width: MediaQuery.of(context).size.width,
              child: TextFormField(
                style: TextStyle(
                  fontSize: 30,
                  fontFamily: 'Karla',
                ),
                decoration: InputDecoration.collapsed(
                  hintText: 'Experimez-vous...',
                  hintStyle: TextStyle(
                    color: Color(0xff2e3039),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    ),
  ),
);

忽略 GestureDetector() 小部件。 是的,你必须将主体包裹到 SingleChildScrollView() 中, 我正在使用 bottomNavigationBar,必须完成它。 每个人都对我的问题做出了贡献是正确的。我只需要相应地加入拼图。

我不得不删除 BottomNavigation 文件并将代码包含在容器的主体中。

你可以像这样简单地实现它:

return Scaffold(
  appBar: AppBar(title: Text('Your title')),
  body: Column(
    children: [
      Expanded(
        child: Text('Add things that will be shown in the body'),
      ),
      //here comes your text field and will be shown at the bottom of the page
      TextField(),
    ],
  ),
);