从B页返回时,如何刷新A页上的小部件? [英] How can i refresh the widget on the the page A, when i return from page B?
问题描述
我有一个有状态的Page A,其中包含一个小部件,该小部件存在于单独的有状态类中.
I have a stateful Page A that contains a widget which is present in a seperate stateful class.
该小部件在其initState方法中从sharedPreferences中获取一个数字并显示出来,当我单击该小部件时,它会路由到Page B,Page B包含一个用于更改sharedPreferences中数字的函数.
The widget fetches a number from sharedPreferences in its initState method and show it and when i click on the widget, it routes to Page B, Page B contains a function that change the number in the sharedPreferences.
当我返回时,我看不到更改,但是当我重新打开应用程序或切换到其他选项卡并返回时,我会看到.
when i go back i dont see the changes but when i reopen the app or switch to a different tab and come back, i do.
推荐答案
在A页中,当您转到B页时,
In page A, when you go to page B use,
Navigator.pushNamed(context, "/pageB").then((value) {
if (value == true) {
setState(() {
// refresh page 1 here, you may want to reload your SharedPreferences here according to your needs
});
}
});
在B页中,当您想返回到A页时,请使用
In page B, when you want to go back to page A, use
Navigator.pop(context, true);
编辑:如果您没有命名路线,则可能需要在第一种情况下使用关注.
Edit: If you don't have a named route, you may want to use following in 1st situation.
Navigator.push(context, MaterialPageRoute(builder: (context) => PageB())).then((value) {
if (value == true) {
setState(() {
// refresh page 1 here, you may want to reload your SharedPreferences here according to your needs
});
}
});
这篇关于从B页返回时,如何刷新A页上的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!