我在flutter应用程序中有一个密码文本字段,该字段具有与之关联的验证程序,可以正常工作。下面是相同的代码
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,
);
您可以在整个代码上方看到。 我的工作代码有问题,也就是说我无法更改密码验证器的文本颜色,这意味着用户按下提交按钮时,如果密码字段的长度不足。 我应该如何更改相同的颜色?
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以使用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'),
)
],
),
);
}
}