简体   繁体   English

Flutter:如何制作带有提示文本但没有下划线的文本字段?

[英]Flutter: how to make a TextField with HintText but no Underline?

This is what I'm trying to make:这就是我想要做的:

在此处输入图像描述

In the Flutter docs for Text Fields ( https://flutter.io/text-input/ ) it says you can remove the underline by passing null to the decoration.在文本字段的 Flutter 文档 ( https://flutter.io/text-input/ ) 中,它说您可以通过将null传递给装饰来删除下划线。 However, that also gets rid of the hint text.但是,这也摆脱了提示文本。

I do not want any underline whether the text field is focused or not.无论文本字段是否聚焦,我都不想要任何下划线。

UPDATE: updated accepted answer to reflect changes in Flutter SDK as of April 2020.更新:更新了已接受的答案,以反映 Flutter SDK 截至 2020 年 4 月的变化。

Do it like this:这样做:

TextField(
  decoration: new InputDecoration.collapsed(
    hintText: 'Username'
  ),
),

or if you need other stuff like icon, set the border with InputBorder.none或者,如果您需要其他诸如图标之类的东西,请使用InputBorder.none设置边框

InputDecoration(
    border: InputBorder.none,
    hintText: 'Username',
  ),
),

new flutter sdk since after integration of web and desktop support you need to specify individually like this新的 flutter sdk,因为在集成了 Web 和桌面支持之后,您需要像这样单独指定

TextFormField(
    cursorColor: Colors.black,
    keyboardType: inputType,
    decoration: new InputDecoration(
        border: InputBorder.none,
        focusedBorder: InputBorder.none,
        enabledBorder: InputBorder.none,
        errorBorder: InputBorder.none,
        disabledBorder: InputBorder.none,
        contentPadding:
            EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
        hintText: "Hint here"),
  )

Here is a supplemental answer that shows some fuller code:这是一个补充答案,显示了一些更完整的代码:

在此处输入图片说明

  Container(
    decoration: BoxDecoration(
      color: Colors.tealAccent,
      borderRadius:  BorderRadius.circular(32),
    ),
    child: TextField(
      decoration: InputDecoration(
        hintStyle: TextStyle(fontSize: 17),
        hintText: 'Search your trips',
        suffixIcon: Icon(Icons.search),
        border: InputBorder.none,
        contentPadding: EdgeInsets.all(20),
      ),
    ),
  ),

Notes:注意事项:

  • The dark background (code not shown) is Colors.teal .深色背景(代码未显示)是Colors.teal
  • InputDecoration also has a filled and fillColor property, but I couldn't get them to have a corner radius, so I used a container instead. InputDecoration还具有filledfillColor属性,但我不能让他们有一个圆角半径,所以我用一个容器代替。

change the focused border to none将焦点边框更改为无

TextField(
      decoration: new InputDecoration(
          border: InputBorder.none,
          focusedBorder: InputBorder.none,
          contentPadding: EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
          hintText: 'Subject'
      ),
    ),

I found no other answer gives a border radius, you can simply do it like this, no nested Container我发现没有其他答案给出边界半径,你可以简单地这样做,没有嵌套的Container

  TextField(
    decoration: InputDecoration(
      border: OutlineInputBorder(
        borderSide: BorderSide.none,
        borderRadius: BorderRadius.circular(20),
      ),
    ),
  );

To make a Borderless TextFormField i found below solution:为了制作无边框 TextFormField,我找到了以下解决方案:

It is without using container.它不使用容器。

TextFormField(
      decoration: InputDecoration(
          border: OutlineInputBorder(
                         borderRadius: BorderRadius.circular(15.0),
                         borderSide: BorderSide.none,
                            ),

           labelText: "Student email/id",
           floatingLabelStyle: const TextStyle(
                                    height: 4,
                                    color: Color.fromARGB(255, 160, 26, 179)),
                                
           filled: true,
           fillColor: Colors.grey[200],
           prefixIcon: const Icon(Icons.person),
                ),
           ),

Sample Normally:样品通常: 通常看起来像这样

While having input error:输入错误时: 出错时

While user input:用户输入时: 在输入数据时

You can use TextFormField widget of Flutter Form as your requirement.您可以根据需要使用Flutter Form 的TextFormField小部件。

TextFormField(
     maxLines: 1,
     decoration: InputDecoration(
          prefixIcon: const Icon(
              Icons.search,
              color: Colors.grey,
          ),
      hintText: 'Search your trips',
      border: OutlineInputBorder(
         borderRadius: BorderRadius.all(Radius.circular(10.0)),
      ),
    ),
 ),

TextField widget has a property decoration which has a sub property border: InputBorder.none .This property would Remove TextField Text Input Bottom Underline in Flutter app. TextField 小部件有一个属性装饰,它有一个子属性border: InputBorder.none 。此属性将删除 Flutter 应用程序中的TextField文本输入底部下划线。 So you can set the border property of the decoration of the TextField to InputBorder.none , see here for an example:所以你可以将 TextField decorationborder属性设置为InputBorder.none ,看这里的例子:

border: InputBorder.none : Hide bottom underline from Text Input widget. border: InputBorder.none :隐藏文本输入小部件的底部下划线。

 Container(
        width: 280,
        padding: EdgeInsets.all(8.0),
        child : TextField(
                autocorrect: true,
                decoration: InputDecoration(
                border: InputBorder.none,
                hintText: 'Enter Some Text Here')
            )
    )
            Container(
         height: 50,
          // margin: EdgeInsets.only(top: 20),
          decoration: BoxDecoration(
              color: Colors.tealAccent,
              borderRadius: BorderRadius.circular(32)),
          child: TextFormField(
            cursorColor: Colors.black,
            // keyboardType: TextInputType.,
            decoration: InputDecoration(
              hintStyle: TextStyle(fontSize: 17),
              hintText: 'Search your trips',
              suffixIcon: Icon(Icons.search),
              border: InputBorder.none,
              contentPadding: EdgeInsets.all(18),
            ),
          ),
        ),

To remove underline border: InputBorder.none,删除下划线border: InputBorder.none,

For hint use hintText: 'Hint Text'对于提示,请使用hintText: 'Hint Text'

   TextFormField(
      InputDecoration(
        border: InputBorder.none,
        hintText: 'Hint Text',
      ),
    ),

To remove TextField underline in Flutter, you can simply use InputBorder.none.要删除 Flutter 中的 TextField 下划线,您可以简单地使用 InputBorder.none。

TextField(
  decoration: InputDecoration(
    hintText: 'Hint Text',
    border: InputBorder.none,
  ),
)

Default默认

在此处输入图像描述

 Container(
      padding: const EdgeInsets.all(20),
      child: const TextField(
        decoration: InputDecoration(
            border: UnderlineInputBorder(), hintText: "Search Your tips"),
      ),
    ),

Outline Border轮廓边框

在此处输入图像描述

 Container(
      padding: const EdgeInsets.all(20),
      child: TextField(
        decoration: InputDecoration(
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(40),
          ),
          hintText: "Search Your tips",
        ),
      ),
    ),

No Border无边界

在此处输入图像描述

 Container(
      padding: const EdgeInsets.all(20),
      child: const TextField(
        decoration: InputDecoration(
            border: InputBorder.none, hintText: "Search Your tips"),
      ),
    ),

Try the following code:试试下面的代码:

TextFormField(
  decoration: InputDecoration(
    border: OutlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.circular(30.0)),
    hintText: "Search your trips",
    hintStyle: const TextStyle(color: Colors.white, fontWeight: FontWeight.w300),
    filled: true,
    fillColor: Colors.cyan[200],
    suffixIcon: IconButton(
      onPressed: () {},
      icon: const Icon(Icons.search, color: Colors.white),
    ),
  ),
),

TextField(style: TextStyle(color: Colors.black45,fontSize: 18,decorationThickness: 0.0)) . TextField(style: TextStyle(color: Colors.black45,fontSize: 18,decorationThickness: 0.0))
It's showing without underline with decorationThickness:0.0 .它显示为没有下划线,带有decorationThickness:0.0

decoration: InputDecoration(
 border:OutLineInputBorder(
 borderSide:BorderSide.none
 bordeRadius: BordeRadius.circular(20.0)
 )
)

I was using the TextField flutter control.I got the user typed input using below methods.我正在使用TextField flutter 控件。我使用以下方法获得了用户键入的输入。

onChanged:(value){
}

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

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