I have one StatefulWidget
in Flutter with button, which navigates me to another StatefulWidget
using Navigator.push()
. On second widget I'm changing global state (some user preferences). When I get back from second widget to first, using Navigator.pop()
the first widget is in old state, but I want to force it's reload. Any idea how to do this? I have one idea but it looks ugly:
- pop to remove second widget (current one)
- pop again to remove first widget (previous one)
- push first widget (it should force redraw)
Best Answer
There's a couple of things you could do here. @Mahi's answer while correct could be a little more succinct and actually use push rather than showDialog as the OP was asking about. This is an example that uses
Navigator.push
:However, there's another way to do this that might fit your use-case well. If you're using the
global
as something that affects the build of your first page, you could use an InheritedWidget to define your global user preferences, and each time they are changed your FirstPage will rebuild. This even works within a stateless widget as shown below (but should work in a stateful widget as well).An example of inheritedWidget in flutter is the app's Theme, although they define it within a widget instead of having it directly building as I have here.
If you use inherited widget you don't have to worry about watching for the pop of the page you pushed, which will work for basic use-cases but may end up having problems in a more complex scenario.