串列中只顯示最后一個輸入資料,每當我在表單中輸入新值時,新資料都會更新,但前一個資料也會更新。這是輸出在串列中的顯示方式:
這是檔案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,
),
);
}
}
uj5u.com熱心網友回復:
首先,我認為您應該移動以下變數:
List<Model> models = <Model>[];
Model tempModel = Model();
final newModel = Model();
進入 state 類,因為他們在那里更有意義,除了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),
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382757.html
下一篇:如何過濾物件串列,如搜索?
