如何在Flutter中更改TextFormField输入的文本颜色

时间:2018-10-02 07:09:19

标签: dart flutter

在uni上为Flutter应用程序做UI,我只希望在TextFormField中键入的文本为白色。似乎不必要地困难。我尝试使用谷歌搜索等等,但是看不到明显的答案。

  new Theme(
    // this colors the underline
    data: theme.copyWith(
      primaryColor: Colors.white,
      hintColor: Colors.transparent,

    ),
    child: new Padding(
      padding: const EdgeInsets.fromLTRB(32.0, 40.0, 32.0, 4.0),
      child: TextFormField(

          key: Key('username'),
          keyboardType: TextInputType.text,
          controller: usernameController,
          decoration: InputDecoration(

              fillColor: Colors.black.withOpacity(0.6),
              filled: true,
              border: new OutlineInputBorder(

                borderRadius: const BorderRadius.all(

                  const Radius.circular(8.0),
                ),
                borderSide: new BorderSide(
                  color: Colors.transparent,
                  width: 1.0,
                ),
              ),
              labelText: 'Username',
              labelStyle:
                  new TextStyle(color: Colors.white, fontSize: 16.0)),
          style:
              TextStyle(fontSize: 20.0, color: textTheme.button.color),
          validator: validateUserName,
          onSaved: (val) => this.loginFields._username = val),
    ),
  ),

7 个答案:

答案 0 :(得分:8)

这可以做到:

TextFormField(
    style: TextStyle(
        color: Colors.white,
    ),
)

答案 1 :(得分:2)

Puedes磨损style: TextStyle

body: Center(
    child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
            Form(
                key: _formKey,
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                        TextFormField(
                            TextFormField(
                                controller: field,
                                style: TextStyle(fontSize: 18, color: Colors.red),
                                decoration: const InputDecoration(
                                    contentPadding: const EdgeInsets.only(
                                        left: 15,
                                        top: 8,
                                        right: 15,
                                        bottom: 0
                                    ),
                                    hintText: 'name',
                                ),
                                validator: (value) {
                                    if (value.isEmpty) {
                                        return 'Please enter some text';
                                    }
                                    return null;
                                },
                            ),
                        )
                    ]
                )
            )
        ]
    )
)

答案 2 :(得分:1)

您正在此行TextStyle(fontSize: 20.0, color: textTheme.button.color)中更改输入文本的颜色,因此要设置为white,只需使用Colors.white常量而不是textTheme.button.color

有关文本样式here的更多信息。

答案 3 :(得分:1)

概述:textFormField样式设置为定义为MediumStyle的TextStyle。样式影响在文本框中显示的字符。而labelStyle会影响inputdecoration的字体显示。

TextFormField(
            style: MediumStyle,
            keyboardType: TextInputType.emailAddress,
            focusNode: _employeeEmailFocus,
            decoration: InputDecoration(labelText: "Employee Email", labelStyle:MediumBoldStyle),
            validator: (val)=> null,
            onSaved:(value)=> this._employeeEmail=value,
            onFieldSubmitted: (term){
                _fieldFocusChange(context,_employeeEmailFocus,_passwordFocus);
            },

            ),

答案 4 :(得分:0)

Padding(
  padding: EdgeInsets.all(10),
  child: TextFormField(
  cursorColor: Color(0XFFFFCC00)//Cursor color change
  style: TextStyle(
    color: Color(0XFFFFCC00),
    decorationColor: Color(0XFFFFCC00),//Font color change
    backgroundColor: Color(0XFFFFCC00),//TextFormField title background color change
   ),
),

答案 5 :(得分:0)

您可以使用它来更改所有内容

TextFormField(
                //controller: _phoneController,
                cursorColor: Colors.black,
                keyboardType: TextInputType.text,
                style: TextStyle(
                  color: Colors.black
                ),
                decoration: new InputDecoration(
                  hintStyle: TextStyle(
                    color: Colors.white
                  ),
                    border: InputBorder.none,
                    //contentPadding:
                    //EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                    hintText: "New Playlist"),
              ),

答案 6 :(得分:-1)

为textformfield添加一个输入装饰类  这是我的想法

    decoration: InputDecoration(
              prefixStyle: new TextStyle(

              ),