Last active
September 8, 2019 03:59
-
-
Save icatalud/6da4b65c95edf2fa75c10536d3262e23 to your computer and use it in GitHub Desktop.
Full example of fetching and fading in an image using Flutter Loop (Floop).
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())); | |
} | |
var _fetching = false; | |
fetchAndUpdateImage([String url = 'https://picsum.photos/300/200']) async { | |
if (_fetching) { | |
return; | |
} | |
_fetching = true; | |
final response = await http.get(url); | |
floop['image'] = TransitionImage(Image.memory(response.bodyBytes)); | |
_fetching = false; | |
} | |
class TransitionImage extends FloopWidget { | |
final Image image; | |
const TransitionImage(this.image); | |
@override | |
Widget build(BuildContext context) { | |
return Opacity(opacity: transition(1500), child: image); | |
} | |
} | |
class ImageDisplay extends StatelessWidget with Floop { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: floop['image'] == null | |
? Center( | |
child: Text( | |
'Loading...', | |
textScaleFactor: 2, | |
), | |
) | |
: Align( | |
alignment: Alignment(0, transition(2000, delayMillis: 800) - 1), | |
child: floop['image']), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.refresh), | |
onPressed: () async { | |
floop['image'] = null; | |
await fetchAndUpdateImage(); | |
// Restarting context transitions after the new image has loaded | |
// causes the new image to also transition from top to center. | |
Transitions.restart(context: context); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment