gpt4 book ai didi

flutter - TextFormField 在改变焦点时失去值(value)

转载 作者:IT王子 更新时间:2023-10-29 07:12:09 26 4
gpt4 key购买 nike

我正在尝试移动到其他 TextFormField 但每当我从第一个 TextFormField 失去焦点时文本变成空的,我搜索这个问题但我没有找到任何解决方案直到现在。

  var _formKey = GlobalKey<FormState>();
Note note;

TextEditingController titleController=TextEditingController();
TextEditingController descriptionController=TextEditingController();

@override
Widget build(BuildContext context) {
TextStyle textStyle=Theme.of(context).textTheme.title;

titleController.text=note.title;
descriptionController.text=note.description;

// TODO: implement build
return WillPopScope(
onWillPop: (){
moveToLastScreen();
},
child:Scaffold(
appBar: AppBar(
title: Text("appBarTitle"),
leading: IconButton(icon:Icon(Icons.arrow_back),onPressed: (){
moveToLastScreen();
},),
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.only(top: 15.0,left: 15.0,right: 10.0),
child: ListView(
children: <Widget>[
//1st element
Padding(
padding: EdgeInsets.only(top: 15.0,bottom: 15.0,),
child: TextFormField(
validator: (String value){
if(value.isEmpty)
{
return "Please enter Title";
}
},
controller: titleController,
style: textStyle,
onSaved: (value){
debugPrint("Something changed in title Text field");
updateTitle();
},
/*onChanged: (value){
debugPrint("Something changed in title Text field");
updateTitle();
},*/
decoration: InputDecoration(
labelText: "Title",
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
)
),
),

),

//2nd element
Padding(
padding: EdgeInsets.only(top: 15.0,bottom: 15.0,),
child: TextFormField(
validator: (String value){ //2nd step for form with validation
if(value.isEmpty)
{
return "Please enter principle amount";
}
},
onSaved: (value){
debugPrint("Something changed in Description Text field");
updateDescription();
},
controller: descriptionController,
style: textStyle,
/*onChanged: (value){
debugPrint("Something changed in Description Text field");
updateDescription();
},*/
decoration: InputDecoration(
labelText: "Description",
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
)
),
),

),

//3th element
Padding(
padding: EdgeInsets.only(top: 15.0,bottom: 15.0),
child: Row(
children: <Widget>[
Expanded(
child: RaisedButton(
color: Theme.of(context).primaryColorDark,
textColor: Theme.of(context).primaryColorLight,
child: Text("Save",textScaleFactor: 1.5,),
onPressed: (){
setState(() {
if(_formKey.currentState.validate()) {
debugPrint("Save Pressed");
_save();
}
});
}
),
),

Container(width: 5.0,),

Expanded(
child: RaisedButton(
color: Theme.of(context).primaryColorDark,
textColor: Theme.of(context).primaryColorLight,
child: Text("Delete",textScaleFactor: 1.5,),
onPressed: (){
setState(() {
debugPrint("Delete Pressed");
_delete();
});
}
),
),
],
),
),

],
),
)),

));
}

请建议我,我是 Flutter 的新手。

最佳答案

删除 titleController.text=note.title; descriptionController.text=note.description; 从您的构建方法并将其放置在 initState 方法中。

您将丢失 textField 中的值,因为这些行会在重建时随时执行,从而替换从 textFields 获取的值并将其替换为 note.titlenote。 description 此时为空。

换句话说,删除这些行并将其添加到您的代码中。

@override
void initState() {
super.initState();
titleController.text=note.title;
descriptionController.text=note.description;
}

关于flutter - TextFormField 在改变焦点时失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com