1

如何在 TextFormField 中粘贴验证文本。默认情况下它在下面

文本字段

我想要这样 我想要这样

但它显示这样

但它显示这样

我查看了各种来源,但没有找到合适的答案

有没有办法在文本框中显示验证文本

4

3 回答 3

1

如果某个文本字段的验证失败,您可以从 TextEditingController 更新文本,也可以从“onTap”属性中的控制器中删除文本。

TextEditingController _passwordController = TextEditingController();
if(condition)
{
 //success call
}
else
{
setState((){
  _passwordController.text="Password Does not match
 });
}
于 2021-06-06T20:01:40.140 回答
0

有一些问题。我试过但文本字段的验证器属性不支持。

于 2021-06-25T07:29:04.553 回答
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'),
            ),
          ),
        ],
      ),
    );
  }
}
于 2021-06-06T20:13:16.813 回答