Flutter:如何在单击编辑按钮时从列表数据中填充表单并保存?
Flutter: How to populate form from a list data on click edit button and save it?
我有一个列表,它在开头是硬编码的。当我在表单中输入时,表单数据将保存到列表中。单击更新图标时,我想以相同索引的形式获取数据。当前画面是这样的
我想要在点击“编辑”按钮后得到这个输出。有什么办法可以做到这一点吗?
这是我的代码。
import 'package:flutter/material.dart';
import 'package:table/model.dart';
class Episode5 extends StatefulWidget {
@override
_Episode5State createState() => _Episode5State();
}
class _Episode5State extends State<Episode5> {
TextEditingController nameController = TextEditingController();
TextEditingController emailController = TextEditingController();
final form = GlobalKey<FormState>();
static var _focusNode = new FocusNode();
User user = User();
List<User> userList = [
User(name: "a", email: "a"),
User(name: "d", email: "b"),
User(name: "c", email: "c")
];
@override
Widget build(BuildContext context) {
Widget bodyData() => DataTable(
onSelectAll: (b) {},
sortColumnIndex: 0,
sortAscending: true,
columns: <DataColumn>[
DataColumn(
label: Text("Name"),
numeric: false,
tooltip: "To Display name"),
DataColumn(
label: Text("Email"),
numeric: false,
tooltip: "To Display Email"),
DataColumn(
label: Text("Update"),
numeric: false,
tooltip: "To Display Email"),
],
rows: userList
.map(
(name) => DataRow(
cells: [
DataCell(
Text(name.name),
),
DataCell(
Text(name.email),
),
DataCell(
Icon(
Icons.edit,
color: Colors.black,
),
),
],
),
)
.toList(),
);
return Scaffold(
appBar: AppBar(
title: Text("Data add to List Table using Form"),
),
body: Container(
child: Column(
children: <Widget>[
bodyData(),
Padding(
padding: EdgeInsets.all(10.0),
child: Form(
key: form,
child: Container(
child: Column(
children: <Widget>[
TextFormField(
controller: nameController,
focusNode: _focusNode,
keyboardType: TextInputType.text,
autocorrect: false,
onSaved: (String value) {
user.name = value;
},
maxLines: 1,
validator: (value) {
if (value.isEmpty) {
return 'This field is required';
}
return null;
},
decoration: new InputDecoration(
labelText: 'Name',
hintText: 'Name',
labelStyle: new TextStyle(
decorationStyle: TextDecorationStyle.solid),
),
),
SizedBox(
height: 10,
),
TextFormField(
controller: emailController,
keyboardType: TextInputType.text,
autocorrect: false,
maxLines: 1,
validator: (value) {
if (value.isEmpty) {
return 'This field is required';
}
return null;
},
onSaved: (String value) {
user.email = value;
},
decoration: new InputDecoration(
labelText: 'Email',
hintText: 'Email',
labelStyle: new TextStyle(
decorationStyle: TextDecorationStyle.solid)),
),
SizedBox(
height: 10,
),
Column(
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextButton(
child: Text("Add"),
onPressed: () {
if (validate() == true) {
form.currentState.save();
addUserToList(
user.name,
user.email,
);
clearForm();
}
},
),
],
),
),
],
),
],
),
),
),
),
],
),
),
);
}
void addUserToList(name, email) {
userList.add(User(name: name, email: email));
}
clearForm() {
nameController.clear();
emailController.clear();
}
bool validate() {
var valid = form.currentState.validate();
if (valid) form.currentState.save();
return valid;
}
}
您只需通过传入相应的 User
来更新 TextEditingController
文本。
将此功能添加到您的有状态小部件。
void _updateTextControllers(User user) {
setState(() {
nameController.text = user.name;
emailController.text = user.email;
});
}
然后你的图标变成 IconButton
并且它从 userList
传入用户
rows: userList
.map(
(name) => DataRow(
cells: [
DataCell(
Text(name.name),
),
DataCell(
Text(name.email),
),
DataCell(
IconButton(
onPressed: () => _updateTextControllers(name), // new function here
icon: Icon(
Icons.edit,
color: Colors.black,
),
),
),
],
),
)
.toList(),
我假设您最终想要动态添加 User
行而不是对其进行硬编码,在这种情况下我建议您实施状态管理解决方案,即。 GetX、Provider、Riverpod、Bloc 等...来处理这个问题。但就目前而言,这适用于您所拥有的。
我有一个列表,它在开头是硬编码的。当我在表单中输入时,表单数据将保存到列表中。单击更新图标时,我想以相同索引的形式获取数据。当前画面是这样的
我想要在点击“编辑”按钮后得到这个输出。有什么办法可以做到这一点吗?
这是我的代码。
import 'package:flutter/material.dart';
import 'package:table/model.dart';
class Episode5 extends StatefulWidget {
@override
_Episode5State createState() => _Episode5State();
}
class _Episode5State extends State<Episode5> {
TextEditingController nameController = TextEditingController();
TextEditingController emailController = TextEditingController();
final form = GlobalKey<FormState>();
static var _focusNode = new FocusNode();
User user = User();
List<User> userList = [
User(name: "a", email: "a"),
User(name: "d", email: "b"),
User(name: "c", email: "c")
];
@override
Widget build(BuildContext context) {
Widget bodyData() => DataTable(
onSelectAll: (b) {},
sortColumnIndex: 0,
sortAscending: true,
columns: <DataColumn>[
DataColumn(
label: Text("Name"),
numeric: false,
tooltip: "To Display name"),
DataColumn(
label: Text("Email"),
numeric: false,
tooltip: "To Display Email"),
DataColumn(
label: Text("Update"),
numeric: false,
tooltip: "To Display Email"),
],
rows: userList
.map(
(name) => DataRow(
cells: [
DataCell(
Text(name.name),
),
DataCell(
Text(name.email),
),
DataCell(
Icon(
Icons.edit,
color: Colors.black,
),
),
],
),
)
.toList(),
);
return Scaffold(
appBar: AppBar(
title: Text("Data add to List Table using Form"),
),
body: Container(
child: Column(
children: <Widget>[
bodyData(),
Padding(
padding: EdgeInsets.all(10.0),
child: Form(
key: form,
child: Container(
child: Column(
children: <Widget>[
TextFormField(
controller: nameController,
focusNode: _focusNode,
keyboardType: TextInputType.text,
autocorrect: false,
onSaved: (String value) {
user.name = value;
},
maxLines: 1,
validator: (value) {
if (value.isEmpty) {
return 'This field is required';
}
return null;
},
decoration: new InputDecoration(
labelText: 'Name',
hintText: 'Name',
labelStyle: new TextStyle(
decorationStyle: TextDecorationStyle.solid),
),
),
SizedBox(
height: 10,
),
TextFormField(
controller: emailController,
keyboardType: TextInputType.text,
autocorrect: false,
maxLines: 1,
validator: (value) {
if (value.isEmpty) {
return 'This field is required';
}
return null;
},
onSaved: (String value) {
user.email = value;
},
decoration: new InputDecoration(
labelText: 'Email',
hintText: 'Email',
labelStyle: new TextStyle(
decorationStyle: TextDecorationStyle.solid)),
),
SizedBox(
height: 10,
),
Column(
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextButton(
child: Text("Add"),
onPressed: () {
if (validate() == true) {
form.currentState.save();
addUserToList(
user.name,
user.email,
);
clearForm();
}
},
),
],
),
),
],
),
],
),
),
),
),
],
),
),
);
}
void addUserToList(name, email) {
userList.add(User(name: name, email: email));
}
clearForm() {
nameController.clear();
emailController.clear();
}
bool validate() {
var valid = form.currentState.validate();
if (valid) form.currentState.save();
return valid;
}
}
您只需通过传入相应的 User
来更新 TextEditingController
文本。
将此功能添加到您的有状态小部件。
void _updateTextControllers(User user) {
setState(() {
nameController.text = user.name;
emailController.text = user.email;
});
}
然后你的图标变成 IconButton
并且它从 userList
rows: userList
.map(
(name) => DataRow(
cells: [
DataCell(
Text(name.name),
),
DataCell(
Text(name.email),
),
DataCell(
IconButton(
onPressed: () => _updateTextControllers(name), // new function here
icon: Icon(
Icons.edit,
color: Colors.black,
),
),
),
],
),
)
.toList(),
我假设您最终想要动态添加 User
行而不是对其进行硬编码,在这种情况下我建议您实施状态管理解决方案,即。 GetX、Provider、Riverpod、Bloc 等...来处理这个问题。但就目前而言,这适用于您所拥有的。