【问题标题】:My textfield hides behind keyboard in BottomNavigationBar when I click to edit it当我单击编辑它时,我的文本字段隐藏在 BottomNavigationBar 的键盘后面
【发布时间】:2020-07-12 23:28:41
【问题描述】:

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

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

这是底部导航条代码:

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),
          ),
        ),
      ),
    ],
  ),
);

这是主体(脚手架)我从以下位置调用它:

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

【问题讨论】:

  • 可以分享完整的代码吗?
  • 这是完整的代码兄弟。我已共享用于解决我所面临的特定问题的所有文件。

标签: android user-interface flutter dart hidden-field


【解决方案1】:

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

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,必须完成它。 对我的问题做出贡献的每个人都是正确的。我只需要相应地加入拼图。

我不得不删除底部导航文件并将代码包含在容器内的主体中。

【讨论】:

  • 您好,我有类似这样的代码,您不会觉得滞后,我的意思是即使在配置文件模式下也需要 5-7 秒才能到达键盘顶部
【解决方案2】:

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

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(),
    ],
  ),
);

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • bottomNaavigationBar 不能包装在 SingleChildScrollView 小部件中。我尝试将主容器包装在 SingleChildScrollView 中,但它不起作用。
    【解决方案4】:

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

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

    【讨论】:

    • 您不应该使用 BottomNavigationBar 将文本字段放置在页面底部,而应使用 Stack 将文本字段放置在底部。
    • 我现在将文本字段放在底部并删除了 BottomNavigationBar 但它似乎仍然不起作用。这就是我所做的。脚手架(正文:SingleChildScrollView(.......ContainerListItems(),堆栈(孩子: [容器(颜色:Colors.white,边距:EdgeInsets.fromLTRB(40,80,0,0),孩子: TextField(style: TextStyle(.......),....), //singlechildscrollview
    【解决方案5】:

    只需将每个小部件都放在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,
          ],
        ),
      ),
    );    
    

    【讨论】:

    • 您可以通过在某处上传来分享您的代码吗?完整代码
    • 我可以向您发送一封电子邮件,其中包含我正在尝试更改的文件(源代码),即:这个特定的屏幕代码...您可以这样做吗?
    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2012-12-07
    • 2020-10-15
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多