如何从另一个类中清除文本编辑控制器 [英] how to clear text editing controller from another class

查看:86
本文介绍了如何从另一个类中清除文本编辑控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个类,按钮和文本字段

so i have two class, button and text field

这是带有控制器小部件的文本字段

this is text field with controller widget

这是清洁我的控制器的按钮动作。TextEditing

this is button action to clean my controllerTextEditing

推荐答案

您可以使用 GlobalKey 完成此操作。将 TextFormField 作为子元素放入 Form

You can use a GlobalKey to accomplish this . Put the TextFormField as a child to Form

GlobalKey<FormState> _key = GlobalKey<FormState>();

class MyForm extends StatelessWidget
{
MyForm({Key key}) : super(key:key);

@override
Widget build(BuildContext context) {
return Form(
key: _key,
autovalidate: true,
child: // All the form fields will go here
);
 }
}

之后,您可以使用

_key.currentState.reset()

在另一个类中,它将重置每个类表单内的字段返回其 initialValue

in another class ,which will reset each field inside the form back to its initialValue

这篇关于如何从另一个类中清除文本编辑控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆