flutter:如何更新文本小部件的值
flutter: how to update the text widget value
我想在点击按钮时更新文本小部件的值。
这是我的代码
Widget text(){
if(workinghours=="0" || gettimeinworkinghours=="0"){
if(gettimeinworkinghours=="0"){
totalWorkingHours();
setState(() { //here i am using setstate
return Text(gettimeoutworkinghours,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily));
});
}
else if(workinghours=="0"){
totalWorkingHours();
return Text(totalTime,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily)); }
}
else{
return Text(workinghours,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily));
}
return Text(gettimeoutworkinghours,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily));
}
我想将文本小部件值更新为 gettimeoutworkinghours
值,当我按 ctrl+s(保存)时它正在工作,它更新值但我想自动更新它,我使用 setState fo这个但是那个不起作用,我在 initState() 中调用了 text() 但它不起作用,请帮助如何做到这一点。
- 确保您在有状态的小部件中。
- 按钮 onPressed 回调使用 setState(()=>text())
- 它应该更新文本小部件。
所以你在 initState() 中调用了 text()。 initState() 仅在第一次加载小部件时调用一次。 setState 不再调用 initState()
我想在点击按钮时更新文本小部件的值。
这是我的代码
Widget text(){
if(workinghours=="0" || gettimeinworkinghours=="0"){
if(gettimeinworkinghours=="0"){
totalWorkingHours();
setState(() { //here i am using setstate
return Text(gettimeoutworkinghours,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily));
});
}
else if(workinghours=="0"){
totalWorkingHours();
return Text(totalTime,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily)); }
}
else{
return Text(workinghours,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily));
}
return Text(gettimeoutworkinghours,style: TextStyle(
color: fontcolor,
fontSize: remainingtextfontsize,
fontFamily: fontFamily));
}
我想将文本小部件值更新为 gettimeoutworkinghours
值,当我按 ctrl+s(保存)时它正在工作,它更新值但我想自动更新它,我使用 setState fo这个但是那个不起作用,我在 initState() 中调用了 text() 但它不起作用,请帮助如何做到这一点。
- 确保您在有状态的小部件中。
- 按钮 onPressed 回调使用 setState(()=>text())
- 它应该更新文本小部件。
所以你在 initState() 中调用了 text()。 initState() 仅在第一次加载小部件时调用一次。 setState 不再调用 initState()