How to navigate to new page after state change?
I had an app that require login first. Only after login, the app component are fully created. So I wrote something like this:
Main app
class AppComponentState extends State<AppComponent> implements CredentialProvider {
Credential credential;
@override
Widget build(BuildContext context) {
if (credential == null) {
return new MaterialApp(
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => new LoginPage(this),
},
);
} else {
return new MaterialApp(
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => new Desktop(credential),
...
},
);
}
}
@override
void setCredential(Credential s) {
setState(() {
credential = s;
});
}
}
Credential provider interface
abstract CredentialProvider {
void setCredential(Credential c);
}
The login button listener in LoginPage
, credential is set and route to the new page.
@override
Widget build(BuildContext context) {
void _handleSubmitted() {
...
credentialProvider.setCredential(c); // this change main widget state and hence new pages are created
// call setTimeout ?
Navigator.pushNamed(context, "/"); // this should go to new page, because credential is set.
}
...
}
It seem like, I have to wait current digest loop to finish before invoking Navigator.pushNamed(context, "/");
. Is there any good way to do in flutter digest cycle like setTimeout
in javascript?
It is a bit weird, conditional creation of MaterialApp
. Is there better pattern in flutter?
onGenerateRoute
could be a solution to solve multipleMaterialApp
? It has its own problem though? – Choir