I cant really find a simple way to just rebuild a widget from outside it.
I guess this might be "by design", that you're not supposed to do it this way. But desperate times (deadlines) calls for desperate measures, so is there any way to tell a widget to rebuild (for example a Builder
, or even the top most MaterialApp.builder
) from anywhere in the code?
Note that I don't want to restart the application, just rebuild a specific widget somewhere in the widget tree.
Simplified example:
MaterialApp(
builder: (context, child) {
if( securityModel.appIsLocked ) then return LockScreen();
return child;
},
home: MainScreen(),
routes: {
'/page1': (context) => Page1Screen(),
'/page2': (context) => Page2Screen(),
}
)
class SecurityModel(
bool appIsLocked = false;
void lockApp(){
appIsLocked = true;
/*******
Force MaterialApp.builder() to rebuild, to replace the entire app with `LockScreen()`
*******/
}
)
You could achieve this by wrapping your widget in a StreamBuilder widget.
Then create a Stream on a global scope that you use as a parameter for your Streambuilder widget.
You can then simply use the myGlobalStream.add(newValue)
method from anywhere to update your widget.
Let me know if you need a code sample, but it should be pretty straight-forward.