[英]How to change the color of validator text in flutter?
我在 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,
);
你可以在上面看到整個代碼。 我對這段工作代碼有疑問,那就是我無法更改密碼驗證器的文本顏色,我的意思是當用戶按下提交按鈕時,如果密碼字段的長度不夠大。 我應該如何改變相同的顏色?
您可以在下面復制粘貼運行完整代碼
您可以使用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'),
)
],
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.