如何移除TextField下方的下划线?

41

这是代码。我想删除文本下方的黑色下划线,目前TextField处于编辑模式:

TextField(
      autofocus: true,
      decoration: InputDecoration.collapsed(
        hintText: "Search",
        border: InputBorder.none,
      ),
      maxLines: 1,
    )


7
让我们看看这是否有帮助:TextField(..., style: TextStyle(decoration: TextDecoration.none)) 使用这个代码可以去除文本框中文字的下划线装饰。 - George Zvonov
文本下划线可能是您的自动更正,将焦点从字段移开并检查是否存在下划线。 - Dev Man
没有成功,乔治和不朽的家伙。如果我通过点击返回按钮来移除焦点,那么下划线就会消失,但是即使我正在输入,我也不想要下划线。 - Rahul Lohra
2
@RahulLohra,这不是你可以控制的事情,而是你的键盘所做的(某种功能)。我要求你在输入完后将焦点移开,以确认这一事实。这不是文本字段的问题,而是你的键盘拼写检查器让你知道你输入的单词是否正确。如果你输入了错误的内容,比如“wasdsasd”,那么黑线就会变成红色下划线。相反,禁用自动更正,请参见:https://github.com/flutter/flutter/issues/22828#issuecomment-428093243 - Dev Man
1
可能是如何在Flutter的TextField中禁用预测文本?的重复问题。 - Mazin Ibrahim
显示剩余2条评论
10个回答

26

尝试为您的TextField小部件提供TextStyle。您的TextField正在获取默认主题的TextStyle。

TextField(
      autofocus: true,
      style: TextStyle(color: Colors.white, fontSize: 30),
      decoration: InputDecoration.collapsed(
        hintText: "Search",
        border: InputBorder.none,
      ),
      maxLines: 1,
    )

在TextField小部件的源代码中,它声明:

  /// If null, defaults to the `subhead` text style from the current [Theme].
  final TextStyle style;

15

你应该在decoration属性中使用TextDecoration.none

Text(
  'your txt',
  style: TextStyle( decoration: TextDecoration.none),
)

8

我怀疑这与预测文本有关。当您按下空格键结束输入单词时,下划线会消失;当您开始输入下一个单词时,下划线又会出现。如此处所建议的那样,尝试设置TextInputType.visiblePassword; - 这对我有效。


7

只是代码……

TextFormField(
  decoration: InputDecoration(
                 hintText: 'comment..',
                 focusedBorder: InputBorder.none,
                 enabledBorder: InputBorder.none,
                 contentPadding: EdgeInsetsDirectional.only(start: 10.0),
                  ),
              )

like this image


6

样式:文本样式( 装饰厚度:0)


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
Provide the following code inside the TextField widget and change the color according to the background color of a TextField. If the background is white provide white color which will make the underline invisible.



style: TextStyle(
              fontSize: 16,
              letterSpacing: 1,
              decoration: TextDecoration.none,
              decorationStyle: TextDecorationStyle.dotted,
              decorationColor: Colors.white),

0

0

-1

@Immortal Dude说得没错,这不是文本字段的问题。因为当您在文本字段中键入文本后单击其他地方时,下划线会自动从文本中移除。

您只需要设置键盘类型即可:

keyboardType: TextInputType.visiblePassword,

-1

您需要添加“decoration: TextDecoration.none”,就像这样:

  Text(
    "Don't forget",
    style: TextStyle(
       decoration: TextDecoration.none
    )
  )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接