Last active
September 8, 2019 03:56
-
-
Save icatalud/9beec3f728882caa672a5df043c50dc4 to your computer and use it in GitHub Desktop.
Fetches an image and displays it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:floop/floop.dart'; | |
import 'package:http/http.dart' as http; | |
void main() { | |
fetchAndUpdateImage(); | |
runApp(MaterialApp(title: 'Fetch image', home: ImageDisplay())); | |
} | |
fetchAndUpdateImage([String url = 'https://picsum.photos/300/200']) async { | |
final response = await http.get(url); | |
floop['image'] = Image.memory(response.bodyBytes); | |
} | |
class ImageDisplay extends StatelessWidget with Floop { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Align( | |
child: floop['image'] ?? | |
Text( | |
'Loading...', | |
textScaleFactor: 2, | |
)), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.refresh), | |
onPressed: () { | |
floop['image'] = null; // set to null while awaiting response | |
await fetchAndUpdateImage(); | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment