I would like to display a custom loading image right after a page loads, like the example in the first block of code below.
The loading screen should be visible until I get a response from async HTTP request, after which I want to replace / rebuild the entire body with a different widget and pass a variable from the getData()
to the newly built body.
Please, don’t suggest to use the CircularProgressIndicator
– I want to use a custom loading image.
The body after page load:
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
new Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('oading.gif'),
),
),
),
),
);
}
The http
request:
void getData() async {
final response = await http.get(Uri.parse(URL));
if (response.statusCode == 200) {
messageText = "Success";
} else {
messageText = "Something went wrong";
}
}
This is the resulting page body that I’d like to show after the getData()
function loads:
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Container(
child: Text(messageText),
),
),
);
}
2
Answers
Something close to the below:
You can also use a future builder instead.
You can use
FutureBuilder
to wait for your HTTP request async function to complete, and render the UI based on the results.