答案 0 :(得分:1)
如果某个文本字段的验证失败,您可以从 TextEditingController 更新文本,也可以从控制器的“onTap”属性中删除文本。
TextEditingController _passwordController = TextEditingController();
if(condition)
{
//success call
}
else
{
setState((){
_passwordController.text="Password Does not match
});
}
答案 1 :(得分:0)
您应该通过以下方式验证您的表单
class MyForm extends StatefulWidget {
@override
MyFormState createState() {
return MyFormState();
}
}
// Create a corresponding State class.
// This class holds data related to the form.
class MyFormState extends State<MyForm> {
// Create a global key that uniquely identifies the Form widget
// and allows validation of the form.
//
// Note: This is a GlobalKey<FormState>,
// not a GlobalKey<MyFormState>.
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
// sendData();
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
child: Text('Submit'),
),
),
],
),
);
}
}
答案 2 :(得分:0)
有点问题。我试过了,但文本字段的验证器属性不支持。