簡體   English   中英

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

[英]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 但這不起作用,我打電話initState() 中的 text() 但它不起作用,請幫助如何做到這一點。

  1. 確保您處於有狀態的小部件中。
  2. 按鈕 onPressed 回調使用 setState(()=>text())
  3. 它應該更新文本小部件。

所以你在 initState() 中調用 text()。 initState() 僅在第一次加載小部件時調用一次。 setState 不會再次調用 initState()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM