Flutter 弹出“颤振”时清除文本字段中的文本

Flutter 弹出“颤振”时清除文本字段中的文本,flutter,uitextfield,Flutter,Uitextfield,我有一个SearchText文本字段。由于一切正常,我想知道当我回到同一页时,如何清除文本字段中的文本。现在,当我从页面返回时,搜索文本仍保留在那里 条件:我正在将值从字段传递到另一个页面。所以搜索文本应该有一些文本 到目前为止,我所做的是: 尝试在推送发生后将文本设置为null。我必须按按钮转到另一页 onPressed: (){ Navigator.push(context, new MaterialPageRoute( builder: (context) => Se

我有一个SearchText文本字段。由于一切正常,我想知道当我回到同一页时,如何清除文本字段中的文本。现在,当我从页面返回时,搜索文本仍保留在那里

条件:我正在将值从字段传递到另一个页面。所以搜索文本应该有一些文本

到目前为止,我所做的是:

尝试在推送发生后将文本设置为null。我必须按按钮转到另一页

onPressed: (){
   Navigator.push(context, new MaterialPageRoute(
     builder: (context) => SearchPage(searchText: this.search.text)
   ));
   setState((){this.search.text = '';});
}
出现问题:将数据推送到另一页之前,搜索文本变为空。这是我不想要的

尝试在我的initState中将文本设置为null,但pop只是替换移除堆栈,而不是重做页面。所以页面不调用initState 我也尝试过这样做:

Navigator.of(context).pop('NoText': '');
但是我不知道在主页上该怎么做,或者更新搜索文本

我不想再次推送,因为它会将同一页再次添加到堆栈中

第1页->第2页按后退->第1页搜索文本=

任何帮助都将不胜感激。谢谢:

将TextEditingController添加到您的TextField并调用controller.clear,只有在按下其他页面后才能清除

这可以通过在onPressed函数中使用wait来实现,也可以使用.then回调来实现,如果您希望避免使用onPressed的“functionsync”

范例-

//Initialize a controller inside your State class
TextEditingController _controller = TextEditingController();

//Set the _controller on you TextField
TextField(
  controller: _controller,
  //Rest of your code
)

//Clear the controller after pushing the new page
onPressed: () {
   Navigator.push(context, new MaterialPageRoute(
     builder: (context) => SearchPage(searchText: this.search.text)
   )).then((value) {
      //This makes sure the textfield is cleared after page is pushed.
      _controller.clear();
   });
}
让我知道这是否有帮助

将TextEditingController添加到您的TextField,并在按下另一页后调用controller.clear

这可以通过在onPressed函数中使用wait来实现,也可以使用.then回调来实现,如果您希望避免使用onPressed的“functionsync”

范例-

//Initialize a controller inside your State class
TextEditingController _controller = TextEditingController();

//Set the _controller on you TextField
TextField(
  controller: _controller,
  //Rest of your code
)

//Clear the controller after pushing the new page
onPressed: () {
   Navigator.push(context, new MaterialPageRoute(
     builder: (context) => SearchPage(searchText: this.search.text)
   )).then((value) {
      //This makes sure the textfield is cleared after page is pushed.
      _controller.clear();
   });
}

让我知道这是否有帮助

这是销毁pop上任何小部件的最好、最简单的方法,而且工作起来没有任何麻烦

每次我们弹出任何屏幕时都会调用dispose方法。有关更多信息:


这是销毁pop上的任何小部件的最佳且简单的方法,而且它工作起来没有任何麻烦

每次我们弹出任何屏幕时都会调用dispose方法。有关更多信息:


.然后为我锻炼。以前它的工作原理是一样的。谢谢你,西德哈特伟大的很高兴我能帮上忙。然后为我锻炼。以前它的工作原理是一样的。谢谢你,西德哈特伟大的很高兴我能帮忙: