I am fairly new to flutter, so I'm sorry if this is a simple question. Anyways, I have a screen on my app which is a stateful widget. To my understanding, a stateful widget immediately changes state when the setState() function is called. In doing so, the build() function is immediately called.
However, when my code runs the function below (called onPressed for a button) it runs the entire function and then changes the state.
void buttonPressed() {
if (formKey.currentState.validate()) {
setState(() {
isLoading = true;
});
sleep(Duration(seconds: 3));
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => NextScreen(),
));
}
}
What I am trying to do is implement a traditional loading icon that spins for 3 seconds (in this case) before moving on to the next screen. However, what actually happens when I run this is that the app completely halts for 3 seconds, shows the loading icon for a fraction of a second, and then moves to NextScreen().
I tried working around this (in case it was an issue with the call stack) by putting the sleep() and Navigator.pushReplacement() lines in a separate method. This did not work either.
Any ideas why this is happening and/or how I could fix it. Any suggestions help, thanks!