简体   繁体   English

如何更改 flutter 中文本字段的内部颜色?

[英]How to change the inside color of a Textfield in flutter?

I want to change the Color of an textfield in flutter.我想更改 flutter 中文本字段的颜色。 Means the area within the InputBorder.表示 InputBorder 内的区域。 I tried to wrap the textfield in container, and change the color of the container, but this seems a fool's step to solve the problem.我试图将文本字段包装在容器中,并更改容器的颜色,但这似乎是解决问题的愚蠢步骤。

I tried the fillColor option, but nothing changed.我尝试了 fillColor 选项,但没有任何改变。

图片说明情况

Here is my Code -->这是我的代码-->

Container(
  padding: EdgeInsets.fromLTRB(
    10,
    screenHeight * 0.016415869,
    10,
    0,
  ),
  height: screenHeight * 0.082079343,
  child: TextField(
    cursorColor: Color(0xFF7675E0),
    textAlign: TextAlign.left,
    decoration: InputDecoration(
      fillColor: Colors.black, //Nothing Worked
      contentPadding: EdgeInsets.fromLTRB(
        15,
        screenHeight * 0.002735978,
        2,
        screenHeight * 0.002735978,
      ),
      hintText: "Search",
      hintStyle: GoogleFonts.sniglet(
        fontSize: screenHeight * 0.020519836,
        color: Color(0xFFC6C8CA),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(5.0),
        borderSide: BorderSide(
          color: Colors.grey[200].withOpacity(0.2),
        ),
      ),
      prefixIcon: Icon(Icons.search),
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(
          color: Colors.grey[200].withOpacity(1),
        ),
      ),
    ),
  ),
),

Thanks in Advance:)提前致谢:)

Try out this:-

decoration: InputDecoration(
                  filled: true,
                  fillColor: Colors.grey,

If you want to set the color to your text field then there is one boolean variable which you need to set true so your color will be added to your text field如果要将颜色设置为文本字段,则需要将一个 boolean 变量设置为 true,以便将颜色添加到文本字段中

             Container(
              child: TextField(
                cursorColor: Color(0xFF7675E0),
                textAlign: TextAlign.left,
                decoration: InputDecoration(
                  fillColor: Colors.black, // you can change color of textfield 
                  filled: true, // this should be true if you want to set color to textfield
                  hintText: "Search",
                  prefixIcon: Icon(Icons.search),
                ),
              ),
            ),

Basically you can leave your code as you provided us.基本上你可以留下你提供给我们的代码。 The important missing value is重要的缺失值是

filled: true

With this value applied you can style the background color easy.应用此值后,您可以轻松设置背景颜色的样式。 Reference to the doc: https://api.flutter.dev/flutter/material/TextField-class.html参考文档: https://api.flutter.dev/flutter/material/TextField-class.html

don't use Container for TextField decoration, there are more property into TextField不要使用 Container 进行 TextField 装饰,TextField 中有更多属性

TextField use文本字段使用

decoration: InputDecoration(
    filled: true,
    fillColor: Colors.white10,
    border: new OutlineInputBorder(
      borderRadius: new BorderRadius.all(
        new Radius.circular(14.0),
      ),
    ),


TextField(
  controller: usernameController,
  keyboardType: TextInputType.emailAddress,
  style: TextStyle(color: Colors.white),
  decoration: InputDecoration(
    filled: true,
    fillColor: Colors.white10,
    border: new OutlineInputBorder(
      borderRadius: new BorderRadius.all(
        new Radius.circular(14.0),
      ),
    ),
    hintText: 'Username',
    hintStyle: TextStyle(color: Colors.white),
    contentPadding: const EdgeInsets.all(24),
    prefixIcon: Icon(Icons.person, color: Colors.white),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.white10),
      borderRadius: BorderRadius.circular(14),
    ),
    enabledBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.white10),
      borderRadius: BorderRadius.circular(14),
    ),
  ),
),

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

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