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

时间:2021-06-06 19:31:46

标签: flutter dart flutter-layout textfield textformfield

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

文本框

我想要这样 I want like this

但它是这样显示的

But it shows like this

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

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

3 个答案:

答案 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)

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