简体   繁体   English

当键盘在 Flutter 文本字段中可见时,键盘顶部有很多空白区域

[英]Lot of blank space on top of keyboard when keyboard is visible in Flutter TextField

This is my code:这是我的代码:

build(BuildContext context) {
  return new Scaffold(
    body: new SafeArea(
    child: new ListView.builder(
        itemBuilder: (itemBuilder),
      itemCount: (1),
      padding: kMaterialListPadding,
    ),
  )
 );
}
itemBuilder(BuildContext context, int index) {
    return new TextFormField(
        decoration: new InputDecoration(
          border: const OutlineInputBorder(),
          hintText: "What's on your mind?",
          helperText: "5-500 characters",
        ),
        maxLines: 3,
    );
}

When I tap on the text field, keyboard opens but lot of blank space appears on top of keyboard as you can see in the picture (border of textfield is cut).当我点击文本字段时,键盘会打开,但键盘顶部会出现很多空白区域,如图所示(文本字段的边框被剪切)。 在此处输入图像描述

It happens because of the ListView.它的发生是因为 ListView。 If I add just the text field to the body, appearance is fine.如果我只将文本字段添加到正文中,则外观很好。

The reason for lot of wasted space was because there was a Scaffold inside a Scaffold. 造成大量浪费空间的原因是因为脚手架内有一个脚手架。 Each scaffold adding space for keyboard. 每个脚手架为键盘增加空间。 Removing one solved the problem. 删除一个解决了问题。

Scaffold has a property resizeToAvoidBottomPadding. Scaffold有一个属性resizeToAvoidBottomPadding。 Set this property false 将此属性设置为false

For those who see white space after closing the keyboard, it can be caused by MediaQuery, where in my case I used MediaQuery around the base scaffold to fix the problem of using RangeSlider inside a drawer(using the RangeSlider will show the behavior of closing the drawer not changing the value of the slider ), where I used this:对于那些在关闭键盘后看到空白的人,这可能是由 MediaQuery 引起的,在我的例子中,我在基本脚手架周围使用了 MediaQuery 来解决在抽屉内使用 RangeSlider 的问题(使用 RangeSlider 将显示关闭的行为抽屉没有改变 slider 的值),我用这个:

 MediaQuery(
  data: MediaQueryData.fromWindow(window).copyWith(
      gestureSettings: const DeviceGestureSettings(touchSlop: kTouchSlop)),
  child: Scaffold(...)

But this however causes the white space that we saw after dismissing the keyboard, also it causes that when you open the keyboard on form, the screen won't scroll anymore.但这会导致我们在关闭键盘后看到的空白,也导致当您在表单上打开键盘时,屏幕将不再滚动。

So I add the media query solution only on the Scaffold where I used the drawer...所以我只在使用抽屉的脚手架上添加媒体查询解决方案......

FYI, for those who uses Getx, I used it with bottom app bar, where we use:仅供参考,对于那些使用 Getx 的人,我将它与底部应用栏一起使用,我们在其中使用:

Scaffold(
      body: Obx(
        () => IndexedStack(
          children: controller.menuPages,
          index: controller.navMenuIndex(),
        ),
      ),

and then after the main tabs, you use another Scaffold (I used the MediaQuery on this one )然后在主选项卡之后,使用另一个 Scaffold(我在这个上使用了 MediaQuery)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM