简体   繁体   English

如何更改 flutter 中验证器文本的颜色?

[英]How to change the color of validator text in flutter?

I have password textfield in flutter application, which has a validator associated with it, which is working fine.我在 flutter 应用程序中有密码文本字段,它有一个与之关联的验证器,工作正常。 Below is the code for the same下面是相同的代码

String pwdValidator(String value) {

  if (value.length < 6) {
    return 'Password must be longer than 6 characters';
  } else {
    return null;
  }
}

final passwordField = TextFormField(
  decoration: InputDecoration(
    labelText: 'Password',

    prefixIcon: Icon(
      LineIcons.lock,
      color: Colors.white,
    ),
    enabledBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.white),
    ),
    focusedBorder: UnderlineInputBorder(
      borderSide: BorderSide(color: Colors.white),
    ),
  ),
  keyboardType: TextInputType.text,
  obscureText: true,
  validator: pwdValidator,
  controller: pwdInputController,
);

You can see above the whole of the code.你可以在上面看到整个代码。 I have issue with this working code and that is I am not able to change the text color of the validator for the password,I mean when the user press the submit button and if password field is not sufficient size in length.我对这段工作代码有疑问,那就是我无法更改密码验证器的文本颜色,我的意思是当用户按下提交按钮时,如果密码字段的长度不够大。 how am i supposed to change the color for the same?我应该如何改变相同的颜色?

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use errorStyle and set TextStyle per your request您可以使用errorStyle并根据您的要求设置TextStyle
code snippet代码片段

TextFormField(
            decoration: InputDecoration(
              labelText: 'Password',
              errorStyle: TextStyle(color: Colors.orange),

working demo工作演示

在此处输入图像描述

full code完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: MyStatelessWidget(),
      ),
    );
  }
}

/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatefulWidget {
  MyStatelessWidget({Key key}) : super(key: key);

  @override
  _MyStatelessWidgetState createState() => _MyStatelessWidgetState();
}

class _MyStatelessWidgetState extends State<MyStatelessWidget> {
  final _formKey = GlobalKey<FormState>();

  String pwdValidator(String value) {
    if (value.length < 6) {
      return 'Password must be longer than 6 characters';
    } else {
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: [
          TextFormField(
            decoration: InputDecoration(
              labelText: 'Password',
              errorStyle: TextStyle(color: Colors.orange),
              prefixIcon: Icon(
                Icons.description,
                color: Colors.white,
              ),
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.white),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.white),
              ),
            ),
            keyboardType: TextInputType.text,
            obscureText: true,
            validator: pwdValidator,
            //controller: pwdInputController,
          ),
          RaisedButton(
            onPressed: () {
              // Validate returns true if the form is valid, otherwise false.
              if (_formKey.currentState.validate()) {}
            },
            child: Text('Submit'),
          )
        ],
      ),
    );
  }
}

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

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