简体   繁体   English

Flutter - 如何在表单保存/提交后清除 TextFormField

[英]Flutter - How to clear TextFormField after form save/submit

With a Flutter form that uses TextFormField, how do I clear the text field after the form have been saved/submitted?对于使用 TextFormField 的 Flutter 表单,如何在保存/提交表单后清除文本字段?

Do I have to use a Text controller?我必须使用短信 controller 吗?

First define controller :首先定义controller

TextEditingController controller = TextEditingController();

then use it like this:然后像这样使用它:

TextFormField(
   controller: controller,
   onFieldSubmitted: (value) {
     controller.clear();
   },
),

here in onFieldSubmitted you can get the value and save it in somewhere else and then clear the controller and by that TextFormField will get clear.onFieldSubmitted中,您可以获得该value并将其保存在其他地方,然后清除controller ,这样TextFormField就会清除。

If you are using a Form widget, add a Key to it, and after submitting the form you can do: _formKey.currentState?.reset();如果您使用的是 Form 小部件,请向其添加一个 Key,然后在提交表单后您可以执行以下操作: _formKey.currentState?.reset(); This will reset the status of the TextFormFields that are contained in your Form.这将重置包含在您的表单中的 TextFormFields 的状态。

Create a TextEditingcontroller创建一个TextEditingcontroller

 final myController = TextEditingController();

dispose of it after use使用后丢弃


  @override
  void dispose() {
    // Clean up the controller when the widget is removed from the widget tree.
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: myController,
      onFieldSubmitted: (value) {
        //do smth with value
        // if you want to clear form field 
        myController.clear();
      },
    );
  }

you can clear a TextFormField after form save/submit by calling the clear method on the TextEditingController associated with the TextFormField .您可以在表单保存/提交后通过调用与TextFormField关联的TextEditingController上的clear方法来清除TextFormField

class _MyFormState extends State<MyForm> {
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();
  final form = Form.of(context);

  @override
  Widget build(BuildContext context) {
    return Form(
      child: Column(
        children: <Widget>[
          TextFormField(
            controller: _emailController,
            decoration: InputDecoration(labelText: 'Email'),
          ),
          TextFormField(
            controller: _passwordController,
            decoration: InputDecoration(labelText: 'Password'),
            obscureText: true,
          ),
          FlatButton(
            onPressed: () {
                if(form !=null && form.validate()){
                // Perform form save/submit logic here
                 _emailController.clear();
                 _passwordController.clear();
               }
            },
            child: Text('Save'),
          ),
        ],
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM