TimeToCode
TimeToCode

Reputation: 1838

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.

Upvotes: 0

Views: 654

Answers (2)

Hamza Azam
Hamza Azam

Reputation: 332

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

Upvotes: 1

Md Sadab Wasim
Md Sadab Wasim

Reputation: 379

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

Upvotes: 2

Related Questions