当导航器上下文发生变化时,颤动重置表单键



这是一个新问题,所以引用这个问题可能会产生误导。如果是,请更正。
我有一个颤动表单,我想在按下按钮(而不是InkWell(时重置它。像这样的

Form(
key: formKey,
child: Column(
children: <Widget>[
TextFormField( ),
SizedBox(height: 15),
TextFormField( ),
SizedBox(height: 15),
DateTimePicker( ),
SizedBox(height: 15),
InkWell(
onTap: () {
// navigate to another page, leaving the form as it is, 
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SharePage(  ),
),
);
}
},
child: Container(...)
),
TextButton(
child : Text('Reset Form'),
onpressed : (){
//doesn't reset the form when the InkWell above is used to navigate to another page and back
formKey.currentState.reset()
}
)
],
),
),

调用formKey.currentState.reset时工作正常。但是,当我按下InkWell转到SharePage并从那里返回时,formKey.currentStatereset方法不会将表单重置为其初始状态,而是将表单重置至转到SharePage之前的状态。即使来自SharePage,我如何将表单重置为完整的初始状态?

您是否尝试更新

InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SharePage(  ),
),
);
}
},
child: Container(...)
),

像这样,

InkWell(
onTap: () {
/// add following line
formKey.reset
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SharePage(  ),
),
);
}
},
child: Container(...)
),

--更新--

从屏幕上的任何位置使用重置功能

TextButton(
child : Text('Reset Form'),
onPressed: () {
formKey.currentState.reset();
},)

最新更新