简体   繁体   中英

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.

  1. Make sure you're in a stateful widget.
  2. on button onPressed callback use setState(()=>text())
  3. It should update the text widget.

So you are calling the text() in initState(). initState() is only called one time when the widget is loaded for the first time. setState doesn't invoke initState() again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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