![example](https://i.stack.imgur.com/tL0uS.gif)
Используйте, например, GlobalKey<FormState>
для доступа к состояниям формы и reset()
к полям.Имея это в виду, вы можете либо:
- передать в конструктор второго экрана первый ключ поля формы и сбросить оба одновременно (когда ваши данные будут обработаны на сервере или около того).)
class Screen1 extends StatefulWidget {
_Screen1State createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> {
final _formKeyScreen1 = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First screen'),
),
body: Center(
child: Form(
key: _formKeyScreen1,
child: Column(
children: <Widget>[
TextFormField(),
TextFormField(),
TextFormField(),
RaisedButton(
child: Text('Navigate to second screen...'),
onPressed: () => Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => Screen2(_formKeyScreen1),
)),
)
],
),
),
),
);
}
}
class Screen2 extends StatefulWidget {
final GlobalKey<FormState> firstScreenFormKey;
Screen2(this.firstScreenFormKey);
_Screen2State createState() => _Screen2State();
}
class _Screen2State extends State<Screen2> {
final _formKeyScreen2 = GlobalKey<FormState>();
void _processData() {
// Process your data and upload to server
_formKeyScreen2.currentState?.reset();
widget.firstScreenFormKey?.currentState?.reset();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second screen'),
),
body: Center(
child: Form(
key: _formKeyScreen2,
child: Column(
children: <Widget>[
TextFormField(),
TextFormField(),
TextFormField(),
RaisedButton(
child: Text('Submit data'),
onPressed: () => _processData(),
)
],
),
),
),
);
}
}
или вы можете сбросить первую экранную форму, когда второй экран выскочит из стека, что, по мнению IMO, должно быть достаточно, поскольку я считаю, что вы не получаете доступ к данным первой экранной формы из второй после обработки, поэтому, еслитолько для целей презентации сброса при возврате должно быть достаточно.В этом случае вы закончите со следующим
class Screen1 extends StatefulWidget {
_Screen1State createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> {
final _formKeyScreen1 = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First screen'),
),
body: Center(
child: Form(
key: _formKeyScreen1,
child: Column(
children: <Widget>[
TextFormField(),
TextFormField(),
TextFormField(),
RaisedButton(
child: Text('Navigate to second screen...'),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(
builder: (BuildContext context) => Screen2(),
))
.then((_) => _formKeyScreen1.currentState.reset());
},
)
],
),
),
),
);
}
}
class Screen2 extends StatefulWidget {
_Screen2State createState() => _Screen2State();
}
class _Screen2State extends State<Screen2> {
final _formKeyScreen2 = GlobalKey<FormState>();
void _processData() {
// Process your data and upload to server
_formKeyScreen2.currentState?.reset();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second screen'),
),
body: Center(
child: Form(
key: _formKeyScreen2,
child: Column(
children: <Widget>[
TextFormField(),
TextFormField(),
TextFormField(),
RaisedButton(
child: Text('Submit data'),
onPressed: () => _processData(),
)
],
),
),
),
);
}
}