【发布时间】:2021-10-02 02:30:16
【问题描述】:
我想在点击按钮时更新文本小部件的值。
这是我的代码
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 对此但那是不工作,我在 initState() 中调用 text() 但它不工作,请帮助如何做到这一点。
【问题讨论】:
-
当你创建一个新的 Flutter 项目时,它会创建一个完全符合你要求的代码。你能在那里检查它是如何完成的吗?
-
是的,我现在检查了一下,它使用 setstate(),我在使用 setstate 的地方更新了我的代码,但这也不起作用!请检查我是否以正确的方式使用 setstate
-
谢谢!它有效,我在错误的地方调用了 setstate( text() ),纠正它并且它有效!