How can I can save state of each form field when the form is scrolled off screen? I lose the values I typed in the TextFormFields when I scroll up and the fields are off the screen.
I have heard the suggestion of saving it in controller and then assigning to initialvalue of TextFormField as seen in the code but it doesn't work for me. What am I missing?
TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
title: new Text('Enter Vehicle'),
),
body: new Padding(
padding: const EdgeInsets.all(16.0),
child: new Form(
key: formKey,
child: new ListView(children: <Widget>[
new TextFormField(
decoration: new InputDecoration(labelText: 'Vehicle Number'),
controller: _controller,
initialValue: _controller.text,
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Tag'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Make'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Model'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Year'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Vehicle Type'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Location'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Fuel'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Other'),
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Your password'),
validator: (val) =>
val.length < 6 ? 'Password too short.' : null,
onSaved: (val) => _password = val,
obscureText: true,
),
new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
),
],)
)
),
);
}