0

我在颤振应用程序中有密码文本字段,它有一个与之关联的验证器,它工作正常。下面是相同的代码

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,
);

您可以在上面看到整个代码。我对此工作代码有疑问,即我无法更改密码验证器的文本颜色,我的意思是当用户按下提交按钮并且密码字段的长度不够大时。我应该如何更改相同的颜色?

4

1 回答 1

1

您可以在下面复制粘贴运行完整代码您可以根据您的请求 代码片段
使用errorStyle和设置TextStyle

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

工作演示

在此处输入图像描述

完整代码

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'),
          )
        ],
      ),
    );
  }
}
于 2020-07-29T08:35:38.333 回答