我編寫了三頁應用程序的簡單代碼,在第三頁中,當它使用“SetState”時,它返回到第二頁,但僅在第三頁中發生,當我在第二頁中使用“setState”時,它工作正常。我還嘗試使用“Navigator.of.push”在頁面之間導航,但 setState 只是沒有執行任何操作,這里是我的代碼示例String check="go to next page";String check2="state1";Widget page3(){ return return FlatButton( child:Text(check2) shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), side: BorderSide(color: Colors.blue[100])), color: Colors.white, onPressed: (){ setState((){check2="state3"}); }); }Widget Profile(){ return FlatButton( child:Text(check) shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), side: BorderSide(color: Colors.blue[100])), color: Colors.white, onPressed: (){ setState((){check2="state3"}); Navigator.push( context, MaterialPageRoute(builder: (context) => page3()), ) });}Widget Biuld(BuildContext context){ return return Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), body: FlatButton( child:Text("press to start") shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), side: BorderSide(color: Colors.blue[100])), color: Colors.white, onPressed: (){ Navigator.push( context, MaterialPageRoute(builder: (context) => profile()), ); })); }我得到的結果是,當按下第一個按鈕時,它會轉到第2頁,當我按下第二個按鈕時,它會更改第二個按鈕文本并轉到第三頁,但是當我按下第三頁上的按鈕時,它會返回到第二頁。使用 navgitor.of 時,它不會返回到第二頁,但也不會更改文本
flutter setState 在使用 Navigator.push 時彈出頁面
嗶嗶one
2023-09-14 20:19:53