您可以在下面复制粘贴运行完整代码您可以根据您的请求
代码片段
使用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'),
)
],
),
);
}
}