【发布时间】:2020-07-29 04:01:04
【问题描述】:
我要做的是在取消表单提交后清除条目。我正在尝试使用 _formKey.currentState.reset() 但这些字段没有被清理。我应该有一种逐场清洁的方法吗?下面我将使用用户名和权重字段显示我的代码,但我决定不放入代码中的其他字段(DropdownButton 和 Radio)以免太长:
class UserDetailForm extends StatefulWidget {
final User user;
const UserDetailForm(this.user);
@override
_UserDetailFormState createState() => _UserDetailFormState();
}
class _UserDetailFormState extends State<UserDetailForm> {
final UserController controller = UserController();
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Form(
key: _formKey,
autovalidate: true,
child: Column(
children: <Widget>[
TextFormField(
initialValue: widget.user.name,
decoration: const InputDecoration(labelText: 'Name *'),
validator: (value) {
if (value.isEmpty) {
return 'Insert your name';
}
return null;
},
onChanged: (value) {
setState(() => widget.user.name = value);
}),
TextFormField(
initialValue: widget.user.weight,
decoration: const InputDecoration(
labelText: 'Weight',
),
onChanged: (value) {
setState(() => widget.user.weight = value);
}),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MaterialButton(
child: Text("Cancel"),
onPressed: () {
_formKey.currentState.reset(); //NOT WORKING, SHOULD CLEAN THE FORM
Navigator.of(context).pop();
},
),
MaterialButton(
child: Text("Save"),
onPressed: () async {
if (_formKey.currentState.validate()) {
controller.updateUser(widget.user);
Navigator.of(context).pop();
}
},
),
],
),
],
),
),
),
);
}
}
#Edit 1 - 使用TextEditingController(仅用于权重)问题依旧,取消表单提交后的条目没有被清理
class UserDetailForm extends StatefulWidget {
final User user;
const UserDetailForm(this.user);
@override
_UserDetailFormState createState() => _UserDetailFormState();
}
class _UserDetailFormState extends State<UserDetailForm> {
final UserController controller = UserController();
final _formKey = GlobalKey<FormState>();
TextEditingController weightController;
initState() {
weightController = TextEditingController(text: widget.user.weight);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Form(
key: _formKey,
autovalidate: true,
child: Column(
children: <Widget>[
TextFormField(
initialValue: widget.user.name,
decoration: const InputDecoration(labelText: 'Name *'),
validator: (value) {
if (value.isEmpty) {
return 'Insert your name';
}
return null;
},
onChanged: (value) {
setState(() => widget.user.name = value);
}),
TextFormField(
controller: weightController,
decoration: const InputDecoration(
labelText: 'Weight',
),
onChanged: (value) {
setState(() => widget.user.weight = value);
}),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MaterialButton(
child: Text("Cancel"),
onPressed: () {
_formKey.currentState.reset();
weightController.text = "";
Navigator.of(context).pop();
},
),
MaterialButton(
child: Text("Save"),
onPressed: () async {
if (_formKey.currentState.validate()) {
controller.updateUser(widget.user);
Navigator.of(context).pop();
}
},
),
],
),
],
),
),
),
);
}
}
【问题讨论】:
标签: android firebase flutter dart