简体   繁体   English

如何在 flutter 中的 TextFormField 中添加文本和图标

[英]how add text and icon inside TextFormField in flutter

i want to make the text and icon inside TextFormField this is my code我想在 TextFormField 中制作文本和图标这是我的代码

enter image description here在此处输入图像描述

_FormField() {
    return TextFormField(
      validator: (value) {
        if (value!.isEmpty) {
          print("test");
        }
      },
      decoration: InputDecoration(
        fillColor: kPrimaryLightColor,
        filled: true,
        focusColor: kPrimaryLightColor,
        border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(30),
            borderSide: BorderSide.none),
        //hintText: 'Email'
        //hintStyle: TextStyle(color: Colors.black38),
        labelText: 'Email',
        labelStyle: TextStyle(color: kPrimaryColor),
        //icon: Icon(Icons.email),
        icon: Icon(Icons.email_outlined),
      ),
    );
  }

i want it like that我想要那样

enter image description here在此处输入图像描述

You can use prefixIcon and suffixIcon of inputDecoration to add icons inside the field.您可以使用prefixIconinputDecorationsuffixIcon在字段内添加图标。 If you want the field label only within the field and not as a label above it, specify only hintText and leave out LabelText .如果您希望字段 label 仅位于字段内而不是字段上方的 label,请仅指定hintText并省略LabelText Check the following code, you can wrap the icons inside a Padding if you need more space.检查以下代码,如果需要更多空间,可以将图标包装在Padding中。

TextFormField(
      validator: (value) {
        if (value!.isEmpty) {
          print("test");
        }
      },
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.email_outlined),
        fillColor: Colors.grey,
        filled: true,
        focusColor: Colors.blue,
        border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(30),
            borderSide: BorderSide.none),
        hintText: 'Email',
        suffixIcon: Icon(Icons.email_outlined),
      ),
    )

your can use prefixIcon and suffixIcon which takes widget.您可以使用带小部件的 prefixIcon 和 suffixIcon。 In which you can give Icon to the prefixIcon and to the suffixIcon you can give IconButton in which you can give the logic to hide/show password.您可以在其中将 Icon 赋予 prefixIcon 和 suffixIcon,您可以将 IconButton 赋予其中您可以提供隐藏/显示密码的逻辑。 To implement the logic declare a boolean variable which is used for if the password is to be shown or hidden.要实现逻辑,请声明一个 boolean 变量,用于显示或隐藏密码。 the logic must be like this.逻辑必须是这样的。

bool showPassword=false;
_FormField() {
    return TextFormField(
       obscureText: !showPassword,
      validator: (value) {
        if (value!.isEmpty) {
          print("test");
        }
      },
      decoration: InputDecoration(
suffixIcon:IconButton(icon:showPassword?Icon(Icons.lock):Icon(Icons.lock_open),
onPressed:(){
setState(() {
      showPassword=!showPassword;
   });
}
)
  ),
    );
  }

The code is not formatted well sorry for that I write it manual here代码格式不正确很抱歉,我在这里写了它的手册

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

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