Flutter 飞球人名单
我有一个动态列表,由我在另一个屏幕上选择的项目组成,在每个项目中,我生成一个带有项目名称的标签,并在Flutter 飞球人名单,flutter,dart,key,global,Flutter,Dart,Key,Global,我有一个动态列表,由我在另一个屏幕上选择的项目组成,在每个项目中,我生成一个带有项目名称的标签,并在TextField旁边为该项目添加更多信息 控制器: controller: TextEditingController.fromValue( TextEditingValue( selection: new TextSelection.collapsed( offset: items[index].length), ), ), Text(items[index
TextField
旁边为该项目添加更多信息
控制器:
controller: TextEditingController.fromValue(
TextEditingValue(
selection: new TextSelection.collapsed(
offset: items[index].length),
),
),
Text(items[index].trim())
标签:
controller: TextEditingController.fromValue(
TextEditingValue(
selection: new TextSelection.collapsed(
offset: items[index].length),
),
),
Text(items[index].trim())
在textfield(可以是1个textfield或1000个textfield)中,我需要获取写入其中的值,并在单击save按钮时将其保存在列表中。为此,我创建了一个带有globalkey的表单,这样在进行验证后它会保存下来,但我只有一个globakey,我可以有几个项
final _formKey = GlobalKey<FormState>();
还有我的钥匙:
final _formKey = GlobalKey<FormState>();
对于所有表单字段,只有一个formKey就足够了。请确保
form()
是所有TextFormField
例如:
Container(child: Form(
key: _key,
child: Column(
children: [
TextFormField(),
TextFormField(),
TextFormField(),
TextFormField(),
..... // N number of form fields with validation
TextButton(onPressed: (){
if (_form.currentState.validate()){
// validates all the field
}
}, child: Text('validate')),
],
),
)
编辑:
您正在使用同一密钥创建多个表单
我已经修改了你的代码,应该可以了
showFormDialogWeigth(BuildContext context) async {
weigthItems.clear();
items = isChecked.toList();
return showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text(
'INFORME O PESO',
style: TextStyle(
fontSize: 18,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
content: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
height: 250,
width: 280,
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return ListTile(
title: Row(
children: <Widget>[
Expanded(
child: Text(
items[index].trim(),
style: TextStyle(
fontSize: 14,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
),
Expanded(
child: TextFormField(
validator: (value) {
if (int.parse(value) > 999999) {
return 'valor incorreto';
}
return null;
},
decoration: InputDecoration(
labelText: unitOfMeasurement,
labelStyle: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
keyboardType: TextInputType.number,
inputFormatters: [
TextInputMask(
mask: '\ !9+.99',
placeholder: '0',
maxPlaceHolders: 3,
reverse: true)
],
controller: TextEditingController.fromValue(
TextEditingValue(
selection: new TextSelection.collapsed(
offset: items[index].length),
),
),
onSaved: (String val){
weigthItems.add(val.trim());
print(val);
},
// onChanged: (value) async {
// _debouncer.run(() {
// weigthItems.add(value.trim());
// print(weigthItems);
// });
// },
),
),
],
),
subtitle: Row(
children: <Widget>[
Expanded(
child: Text(
timeOrTurns.toString(),
style: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(
hintText: "Max 600",
hintStyle: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
keyboardType: TextInputType.number,
controller: TextEditingController.fromValue(
TextEditingValue(
selection: new TextSelection.collapsed(
offset: items[index].length),
),
),
onChanged: (value) async {
if (int.parse(value) < 600) {
_debouncer.run(
() {
timeItems.add(value);
print(timeItems);
},
);
}
},
),
),
],
),
);
},
itemCount: items.length,
),
),
Container(
width: 200,
height: 30,
child: TextField(
maxLength: 2,
keyboardType: TextInputType.number,
controller: totalTime,
decoration: InputDecoration(
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: "TEMPO TOTAL DE MISTURA",
hintStyle: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey)),
),
),
],
),
),
actions: <Widget>[
FlatButton(
onPressed: () async {
Navigator.pop(context);
},
child: Text(
'VOLTAR',
style: TextStyle(color: Colors.green),
),
),
FlatButton(
onPressed: () async {
if(_formKey.currentState.validate()){
_formKey.currentState.save();
_receipt.weigthIngredients = weigthItems.toString().trim();
}
_receipt.unitOfMeasurement = unitOfMeasurement.toString();
_receipt.timeOrTurns = timeOrTurns.toString();
_receipt.valueMix = timeItems.toString();
_receipt.totalMix = totalTime.text;
var result = await _receiptService.saveReceipt(_receipt);
if (result > 0) {
getAllReceipt();
print(timeItems);
print(weigthItems);
print(totalTime);
Navigator.pushNamed(context, AppRoutes.RECEIPTS);
}
},
child: Text(
'SALVAR',
style: TextStyle(color: Colors.green),
),
),
],
);
},
);
},
);
}
showFormDialogWeigth(BuildContext上下文)异步{
weigthItems.clear();
items=isChecked.toList();
返回显示对话框(
上下文:上下文,
生成器:(上下文){
返回状态生成器(
生成器:(上下文,设置状态){
返回警报对话框(
标题:正文(
“通知比索”,
样式:TextStyle(
尺码:18,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold,
颜色:颜色。灰色),
),
内容:表格(
键:_formKey,
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
容器(
身高:250,
宽度:280,
子项:ListView.builder(
收缩膜:对,
itemBuilder:(上下文,索引){
返回列表块(
标题:世界其他地区(
儿童:[
扩大(
子:文本(
items[索引].trim(),
样式:TextStyle(
尺寸:14,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold,
颜色:颜色。灰色),
),
),
扩大(
子项:TextFormField(
验证器:(值){
if(int.parse(value)>999999){
返回“不相关的英勇”;
}
返回null;
},
装饰:输入装饰(
labelText:测量单位,
标签样式:文本样式(
尺寸:10,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold,
颜色:颜色。灰色),
),
键盘类型:TextInputType.number,
输入格式化程序:[
文本输入掩码(
掩码:'\!9+.99',
占位符:“0”,
最大占位符:3,
相反:正确)
],
控制器:TextEditingController.fromValue(
文本编辑值(
选择:新建TextSelection.com(
偏移量:项目[索引].长度),
),
),
onSaved:(字符串val){
weigthItems.add(val.trim());
打印(val);
},
//onChanged:(值)异步{
//_debouncer.run(){
//weigthItems.add(value.trim());
//印刷品(重量);
// });
// },
),
),
],
),
字幕:世界其他地区(
儿童:[
扩大(
子:文本(
timeOrTurns.toString(),
样式:TextStyle(
尺寸:10,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold,
颜色:颜色。灰色),
),
),
扩大(
子项:TextFormField(
装饰:输入装饰(
hintText:“最大600”,
hintStyle:TextStyle(
尺寸:10,
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold,
颜色:颜色。灰色),
),
showFormDialogWeigth(BuildContext context) async {
weigthItems.clear();
items = isChecked.toList();
return showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text(
'INFORME O PESO',
style: TextStyle(
fontSize: 18,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
height: 250,
width: 280,
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return ListTile(
title: Row(
children: <Widget>[
Expanded(
child: Text(
items[index].trim(),
style: TextStyle(
fontSize: 14,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
),
Form(
key: _formKey,
child: Expanded(
child: TextFormField(
validator: (value) {
if (int.parse(value) > 999999) {
return 'valor incorreto';
}
},
decoration: InputDecoration(
labelText: unitOfMeasurement,
labelStyle: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
keyboardType: TextInputType.number,
inputFormatters: [
TextInputMask(
mask: '\ !9+.99',
placeholder: '0',
maxPlaceHolders: 3,
reverse: true)
],
controller: TextEditingController.fromValue(
TextEditingValue(
selection: new TextSelection.collapsed(
offset: items[index].length),
),
),
onSaved: (String val){
weigthItems.add(val.trim());
print(val);
},
// onChanged: (value) async {
// _debouncer.run(() {
// weigthItems.add(value.trim());
// print(weigthItems);
// });
// },
),
),
),
],
),
subtitle: Row(
children: <Widget>[
Expanded(
child: Text(
timeOrTurns.toString(),
style: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(
hintText: "Max 600",
hintStyle: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
),
keyboardType: TextInputType.number,
controller: TextEditingController.fromValue(
TextEditingValue(
selection: new TextSelection.collapsed(
offset: items[index].length),
),
),
onChanged: (value) async {
if (int.parse(value) < 600) {
_debouncer.run(
() {
timeItems.add(value);
print(timeItems);
},
);
}
},
),
),
],
),
);
},
itemCount: items.length,
),
),
Container(
width: 200,
height: 30,
child: TextField(
maxLength: 2,
keyboardType: TextInputType.number,
controller: totalTime,
decoration: InputDecoration(
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: "TEMPO TOTAL DE MISTURA",
hintStyle: TextStyle(
fontSize: 10,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey)),
),
),
],
),
actions: <Widget>[
FlatButton(
onPressed: () async {
Navigator.pop(context);
},
child: Text(
'VOLTAR',
style: TextStyle(color: Colors.green),
),
),
FlatButton(
onPressed: () async {
if(_formKey.currentState.validate()){
_formKey.currentState.save();
_receipt.weigthIngredients = weigthItems.toString().trim();
}
_receipt.unitOfMeasurement = unitOfMeasurement.toString();
_receipt.timeOrTurns = timeOrTurns.toString();
_receipt.valueMix = timeItems.toString();
_receipt.totalMix = totalTime.text;
var result = await _receiptService.saveReceipt(_receipt);
if (result > 0) {
getAllReceipt();
print(timeItems);
print(weigthItems);
print(totalTime);
Navigator.pushNamed(context, AppRoutes.RECEIPTS);
}
},
child: Text(
'SALVAR',
style: TextStyle(color: Colors.green),
),
),
],
);
},
);
},
);
}