微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

颤振文本表单字段如何在 TextFormField

如何解决颤振文本表单字段如何在 TextFormField

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

文本框

我想要这样

I want like this

但它是这样显示

But it shows like this

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

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

解决方法

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

TextEditingController _passwordController = TextEditingController();
if(condition)
{
 //success call
}
else
{
setState((){
  _passwordController.text="Password Does not match
 });
}
,

您应该通过以下方式验证您的表单

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'),],);
  }
}
,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。