简体   繁体   English

颤振:如何更新文本小部件值

[英]flutter: how to update the text widget value

I want to update the value of text widget on click on button.我想在单击按钮时更新文本小部件的值。

here is my code这是我的代码


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));

  }

i want to update the text widget value to gettimeoutworkinghours value, it is working when i press ctrl+s (save), it updates the value but i want to update it automatically, i used setState fo this but that is not working, i call the text() in initState() but it is not working, kindly please help how to do this.我想将文本小部件值更新为gettimeoutworkinghours值,当我按 ctrl+s(保存)时它正在工作,它会更新值但我想自动更新它,我使用了 setState 但这不起作用,我打电话initState() 中的 text() 但它不起作用,请帮助如何做到这一点。

  1. Make sure you're in a stateful widget.确保您处于有状态的小部件中。
  2. on button onPressed callback use setState(()=>text())按钮 onPressed 回调使用 setState(()=>text())
  3. It should update the text widget.它应该更新文本小部件。

So you are calling the text() in initState().所以你在 initState() 中调用 text()。 initState() is only called one time when the widget is loaded for the first time. initState() 仅在第一次加载小部件时调用一次。 setState doesn't invoke initState() again setState 不会再次调用 initState()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM