列表中只显示最后输入的数据,每当我在表单中输入新值时,新数据都会更新,但前一个也会更新
Only last input data is showing in the lists, Whenever I input the new value in the form, the new data is updated but prev one is updated too
列表中只显示最后输入的数据,每当我在表单中输入新值时,新数据都会更新,但之前的数据也会更新。
这是输出在列表中的显示方式:
这里是文件的代码model.dart:
class Model {
String firstName = "";
String lastName = "";
String email = "";
String password = "";
Model({this.firstName, this.lastName, this.email, this.password});
}
这里是我的主文件,我在其中将数据插入表单,并在同一屏幕上的列表中显示输出:
class DialogForm extends StatefulWidget {
List<Model> models = <Model>[];
Model tempModel = Model();
final newModel = Model();
DialogForm();
@override
State<DialogForm> createState() => _ResultState();
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Model>('model', tempModel));
}
}
class _ResultState extends State<DialogForm> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return (Scaffold(
appBar: AppBar(title: Text('Successful')),
body: Container(
margin: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
child: ListView.builder(
itemCount: widget.models.length,
itemBuilder: (context, index) => Card(
elevation: 6,
margin: EdgeInsets.all(10),
child: ListTile(
title: Text(widget.models[index].firstName),
subtitle: Text(widget.models[index].lastName),
trailing: Text(widget.models[index].email),
)
)
)
),
Align(
child: RaisedButton(
child: Text('Click Me!'),
onPressed: (){
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
right: -40,
top: -40,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: CircleAvatar(
child: Icon(Icons.close),
backgroundColor: Colors.red,
),
),
),
Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'First Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your first name';
}
return null;
},
onSaved: (String value) {
widget.tempModel.firstName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Last Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your last name';
}
return null;
},
onSaved: (String value) {
print(value);
widget.tempModel.lastName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Enter email',
isEmail: true,
// validator: (String value) {
// if (!validator.isEmail(value)) {
// return 'Please enter a valid email';
// }
// return null;
// },
onSaved: (String value) {
widget.tempModel.email = value;
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text("Submit"),
onPressed: () {
// var m = widget.tempModel;
// print(m.firstName + "" + m.lastName + "" + m.email);
// return;
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
// var m = widget.tempModel;
// print(m.firstName + "" + m.lastName + "" + m.email);
widget.models.add(widget.tempModel);
setState(() {
widget.models = widget.models;
// widget.tempModel.add(new widget.models());
}
);
Navigator.of(context).pop();
}
},
),
)
],
)
),
],
),
);
}
);
}
),
alignment: Alignment.bottomRight,
),
],
),
),
)
);
}
}
class MyTextFormField extends StatelessWidget {
final String hintText;
final Function validator;
final Function onSaved;
final bool isPassword;
final bool isEmail;
MyTextFormField({
this.hintText,
this.validator,
this.onSaved,
this.isPassword = false,
this.isEmail = false,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
hintText: hintText,
contentPadding: EdgeInsets.all(15.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[200],
),
obscureText: isPassword ? true : false,
validator: validator,
onSaved: onSaved,
keyboardType: isEmail ? TextInputType.emailAddress : TextInputType.text,
),
);
}
}
首先,我认为你应该移动以下变量:
List<Model> models = <Model>[];
Model tempModel = Model();
final newModel = Model();
进入状态 class 因为他们在那里更有意义,除了 newModel
,但我会在一秒钟内解决 newModel
。
您从不使用newModel
。 曾经 为什么不摆脱它???
好的,现在,您可以像这样为您的模型创建一个工厂构造函数:
class Model {
String firstName = "";
String lastName = "";
String email = "";
String password = "";
Model({this.firstName, this.lastName, this.email, this.password});
factory Model.fromModel(Model model) {
return Model(firstName: model.firstName, lastName: model.lastName, email: model.email, password: model.password);
}
}
添加新模型时:
models.add(Model.fromModel(tempModel));
希望这些更改可以解决您的问题
编辑
要从列表中删除项目,您可以执行与此类似的操作:
ListTile(
title: Row(
children: [
TextButton(
child: Icon(Icons.delete),
onPressed: () {
models.removeAt(index);
setState(() => models = models):
},
Text(models[index].firstName),
]
),
subtitle: Text(models[index].lastName),
trailing: Text(models[index].email),
)
列表中只显示最后输入的数据,每当我在表单中输入新值时,新数据都会更新,但之前的数据也会更新。
这是输出在列表中的显示方式:
这里是文件的代码model.dart:
class Model {
String firstName = "";
String lastName = "";
String email = "";
String password = "";
Model({this.firstName, this.lastName, this.email, this.password});
}
这里是我的主文件,我在其中将数据插入表单,并在同一屏幕上的列表中显示输出:
class DialogForm extends StatefulWidget {
List<Model> models = <Model>[];
Model tempModel = Model();
final newModel = Model();
DialogForm();
@override
State<DialogForm> createState() => _ResultState();
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Model>('model', tempModel));
}
}
class _ResultState extends State<DialogForm> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return (Scaffold(
appBar: AppBar(title: Text('Successful')),
body: Container(
margin: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
child: ListView.builder(
itemCount: widget.models.length,
itemBuilder: (context, index) => Card(
elevation: 6,
margin: EdgeInsets.all(10),
child: ListTile(
title: Text(widget.models[index].firstName),
subtitle: Text(widget.models[index].lastName),
trailing: Text(widget.models[index].email),
)
)
)
),
Align(
child: RaisedButton(
child: Text('Click Me!'),
onPressed: (){
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
right: -40,
top: -40,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: CircleAvatar(
child: Icon(Icons.close),
backgroundColor: Colors.red,
),
),
),
Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'First Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your first name';
}
return null;
},
onSaved: (String value) {
widget.tempModel.firstName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Last Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your last name';
}
return null;
},
onSaved: (String value) {
print(value);
widget.tempModel.lastName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Enter email',
isEmail: true,
// validator: (String value) {
// if (!validator.isEmail(value)) {
// return 'Please enter a valid email';
// }
// return null;
// },
onSaved: (String value) {
widget.tempModel.email = value;
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text("Submit"),
onPressed: () {
// var m = widget.tempModel;
// print(m.firstName + "" + m.lastName + "" + m.email);
// return;
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
// var m = widget.tempModel;
// print(m.firstName + "" + m.lastName + "" + m.email);
widget.models.add(widget.tempModel);
setState(() {
widget.models = widget.models;
// widget.tempModel.add(new widget.models());
}
);
Navigator.of(context).pop();
}
},
),
)
],
)
),
],
),
);
}
);
}
),
alignment: Alignment.bottomRight,
),
],
),
),
)
);
}
}
class MyTextFormField extends StatelessWidget {
final String hintText;
final Function validator;
final Function onSaved;
final bool isPassword;
final bool isEmail;
MyTextFormField({
this.hintText,
this.validator,
this.onSaved,
this.isPassword = false,
this.isEmail = false,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
hintText: hintText,
contentPadding: EdgeInsets.all(15.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[200],
),
obscureText: isPassword ? true : false,
validator: validator,
onSaved: onSaved,
keyboardType: isEmail ? TextInputType.emailAddress : TextInputType.text,
),
);
}
}
首先,我认为你应该移动以下变量:
List<Model> models = <Model>[];
Model tempModel = Model();
final newModel = Model();
进入状态 class 因为他们在那里更有意义,除了 newModel
,但我会在一秒钟内解决 newModel
。
您从不使用newModel
。 曾经 为什么不摆脱它???
好的,现在,您可以像这样为您的模型创建一个工厂构造函数:
class Model {
String firstName = "";
String lastName = "";
String email = "";
String password = "";
Model({this.firstName, this.lastName, this.email, this.password});
factory Model.fromModel(Model model) {
return Model(firstName: model.firstName, lastName: model.lastName, email: model.email, password: model.password);
}
}
添加新模型时:
models.add(Model.fromModel(tempModel));
希望这些更改可以解决您的问题
编辑
要从列表中删除项目,您可以执行与此类似的操作:
ListTile(
title: Row(
children: [
TextButton(
child: Icon(Icons.delete),
onPressed: () {
models.removeAt(index);
setState(() => models = models):
},
Text(models[index].firstName),
]
),
subtitle: Text(models[index].lastName),
trailing: Text(models[index].email),
)