简体   繁体   English

Flutter TextField 后缀添加奇怪的填充

[英]Flutter TextField suffix adds weird padding

I want a multiline TextView with a suffix icon in the top right corner.我想要一个多行 TextView,右上角有一个后缀图标。 I tried it with the suffixIcon like this, but the icon is vertically centered:我用这样的suffixIcon尝试过,但图标是垂直居中的:

    TextField(
      decoration: InputDecoration(
        suffixIcon: suffixIcon,
      ),
      maxLines: 5,
      minLines: 5,
    ),

在此处输入图像描述

When I use suffix like this, it is in the top right corner, as supposed, but it adds a weird padding to the top of the text:当我像这样使用suffix时,它在右上角,正如所料,但它在文本顶部添加了一个奇怪的填充:

    TextField(
      decoration: InputDecoration(
        suffix: suffix,
      ),
      maxLines: 5,
      minLines: 5,
    ),

在此处输入图像描述

It is just a small difference, but when you compare the two images, you can see that at the second image, the text is moved slightly to the bottom.这只是一个很小的差异,但是当您比较两个图像时,您可以看到在第二个图像处,文本略微移动到底部。

Any ideas how I can fix this?有什么想法可以解决这个问题吗?

The text field widget comes with some default padding, as part of it's decoration property.文本字段小部件带有一些默认填充,作为其装饰属性的一部分。 It can be removed like this:它可以像这样删除:

TextField(
          decoration: InputDecoration(
            contentPadding: EdgeInsets.zero
          ),
          ... 
        )

There is an issue with the suffix position in the Flutter. Flutter 中的后缀 position 存在问题。 You can read more about here .您可以在此处阅读更多信息。

However, it is not the best way but you can use Stack and Positioned widget to set the position of the suffix icon inside your TextField widget like this:但是,这不是最好的方法,但您可以使用 Stack and Positioned 小部件在 TextField 小部件中设置后缀图标的 position,如下所示:

Stack(
  children: const [
    Positioned(
      right: 5,
      top: 5,
      child: Icon(Icons.close),
    ),
    Positioned.fill(
      child: TextField(
        minLines: 5,
        maxLines: 5,
        decoration: InputDecoration(
          filled: true,
          contentPadding: EdgeInsets.only(
            top: 10,
            bottom: 10,
            left: 10,
            right: 24,
          ),
        ),
      ),
    ),
  ],
)

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

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