Skip to content

Instantly share code, notes, and snippets.

@icatalud
Last active September 8, 2019 03:56
Show Gist options
  • Save icatalud/9beec3f728882caa672a5df043c50dc4 to your computer and use it in GitHub Desktop.
Save icatalud/9beec3f728882caa672a5df043c50dc4 to your computer and use it in GitHub Desktop.
Fetches an image and displays it
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